Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse 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: <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 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.