| Deutsch English Français Italiano |
|
<87plq026li.fsf@bsb.me.uk> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Ben Bacarisse <ben@bsb.me.uk>
Newsgroups: comp.lang.c
Subject: Re: on allowing "int a" definition everywhere
Date: Thu, 22 Aug 2024 11:29:45 +0100
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <87plq026li.fsf@bsb.me.uk>
References: <afdfe7c37c6ad739fd82c7ec0587b82e0963fce2@i2pn2.org>
<va2i90$3f4dg$1@dont-email.me>
<pan$8a32c$1fb86219$8ea0c6ae$7c2d1765@invalid.invalid>
<va4id0$3rc3n$1@dont-email.me>
<pan$2be2c$5ea44d54$282eec3$b0bcf030@invalid.invalid>
<87h6bchpzf.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 22 Aug 2024 12:29:45 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7d4129f127ef5c429eb9b407ef549401";
logging-data="435925"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19D2vYvm8kBRL2iAMOltXQjcjn6uXnHw9k="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:j5KmeD2l/QXyes5idwAds9+wo/Y=
sha1:LUraZVSssCFMQ1lxEc5ldy1vRiM=
X-BSB-Auth: 1.cef8e950ff6699b93370.20240822112945BST.87plq026li.fsf@bsb.me.uk
Bytes: 2010
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> The original code:
>
> if (FILE* f = fopen("file.txt", "r"))
I think the proposal would be a declaration to precede an expression:
if (FILE* f = fopen("file.txt", "r"); f)
> {
> /*...*/ fclose(f);
> }
>
> doesn't allow for taking some non-trivial action of the fopen() call
> fails, but if a declaration in an if condition is visible in the else
> clause, you could write something like:
>
> if (FILE* f = fopen("file.txt", "r")) {
> /*...*/
> fclose(f);
> }
> else {
> perror("file.txt");
> exit(EXIT_FAILURE); // or try something else
> }
What's the significance of the declared name being in scope in the else
clause here?
--
Ben.