Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387686

Re: on allowing "int a" definition everywhere

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: on allowing "int a" definition everywhere
Date 2024-08-22 02:21 -0700
Organization None to speak of
Message-ID <87h6bchpzf.fsf@nosuchdomain.example.com> (permalink)
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>

Show all headers | View raw


Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> writes:
> Thiago Adams wrote:
[...]
>> I like the ability to declare things inside if.
>> 
>> if (FILE* f = fopen("file.txt", "r"))
>> {
>>    /*...*/ fclose(f);
>> }
>> 
>> Because it makes the scope of f, associated with the pointed object
>> lifetime.
>> 
>> For instance, if you try to use f
>> 
>> if (FILE* f = fopen("file.txt", "r"))
>> {
>>    /*...*/ fclose(f);
>> }
>> fwrite(f, ..) ;// ERROR
>
> You can already do that in C23:

Are you suggesting that there's some relevant new feature in C23?
I don't believe there is.

> if (…) {
> 	FILE * f = fopen("file.txt", "r");
> 	/* … */
> 	fclose(f);
> }
> fwrite(f, …);  /* Some kind of error happens. */

I presume the "…" is meant to stand in for some arbitary code, not that
the ellipsis is part of the C23 syntax.

That's not at all the same thing.  In Thiago's code, the body
of the if statement is not executed if the fopen() call fails.
Note that fclose(f) has undefined behavior of f==NULL.

> Or, if you need it to exist before the controlling expression:
>
> for (bool x = true; x; x = false)  for (FILE * f = fopen("file.txt", "r"); 
> x; x = false)  if (…) {
> 	/* … */
> 	fclose(f);
> }
> fwrite(f, …);
>
> Therefore, your example does not work as evidence in favor of declarations 
> in if statement controlling expressions because it's already possible in 
> other ways.

I'm not going to wade through that, but *of course* you can write code
that does what Thiago's code is intended to do.  The point of allowing
declarations in if conditions is for convenience, the same reason
they're allowed in for loops starting in C99.

The original code:

    if (FILE* f = fopen("file.txt", "r"))
    {
       /*...*/ 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
    }

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-20 18:48 +0200
  Re: on allowing "int a" definition everywhere Thiago Adams <thiago.adams@gmail.com> - 2024-08-20 14:03 -0300
    Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-20 19:18 +0200
      Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-20 19:21 +0200
      Re: on allowing "int a" definition everywhere Thiago Adams <thiago.adams@gmail.com> - 2024-08-20 14:34 -0300
        Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-20 22:18 +0200
    Re: on allowing "int a" definition everywhere Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-21 04:42 +0000
      Re: on allowing "int a" definition everywhere Thiago Adams <thiago.adams@gmail.com> - 2024-08-21 08:17 -0300
        Re: on allowing "int a" definition everywhere Bart <bc@freeuk.com> - 2024-08-21 13:34 +0100
          Re: on allowing "int a" definition everywhere Thiago Adams <thiago.adams@gmail.com> - 2024-08-21 10:01 -0300
            Re: on allowing "int a" definition everywhere Michael S <already5chosen@yahoo.com> - 2024-08-21 16:54 +0300
              Re: on allowing "int a" definition everywhere Thiago Adams <thiago.adams@gmail.com> - 2024-08-21 11:21 -0300
        Re: on allowing "int a" definition everywhere Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-22 08:40 +0000
          Re: on allowing "int a" definition everywhere Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-22 02:21 -0700
            Re: on allowing "int a" definition everywhere Ben Bacarisse <ben@bsb.me.uk> - 2024-08-22 11:29 +0100
              Re: on allowing "int a" definition everywhere Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-22 11:12 -0700
              Re: on allowing "int a" definition everywhere Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-22 18:37 +0000
                Re: on allowing "int a" definition everywhere Ben Bacarisse <ben@bsb.me.uk> - 2024-08-23 00:39 +0100
          Re: on allowing "int a" definition everywhere Bart <bc@freeuk.com> - 2024-08-22 11:00 +0100
            Re: on allowing "int a" definition everywhere Thiago Adams <thiago.adams@gmail.com> - 2024-08-22 08:10 -0300
              Re: on allowing "int a" definition everywhere Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-22 17:06 +0000
            Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-23 12:47 +0200
              Re: on allowing "int a" definition everywhere Bart <bc@freeuk.com> - 2024-08-23 12:25 +0100
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-23 13:32 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-23 17:57 +0200
  Re: on allowing "int a" definition everywhere Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-21 03:30 +0000
    Re: on allowing "int a" definition everywhere Ben Bacarisse <ben@bsb.me.uk> - 2024-08-21 09:19 +0100
      Re: on allowing "int a" definition everywhere Ben Bacarisse <ben@bsb.me.uk> - 2024-08-21 10:11 +0100
    Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-25 12:40 +0200
      Re: on allowing "int a" definition everywhere Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-27 06:52 +0000
        Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 10:46 +0200
          Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 11:03 +0200
            Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 11:08 +0200
              Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 11:47 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 11:52 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 13:56 +0200
              Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 20:04 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 20:44 +0200
                Re: on allowing "int a" definition everywhere Bart <bc@freeuk.com> - 2024-08-27 19:59 +0100
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 21:36 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 22:02 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 23:42 +0200
                Re: on allowing "int a" definition everywhere Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-02 03:32 +0000
                Re: on allowing "int a" definition everywhere Bart <bc@freeuk.com> - 2024-09-02 10:53 +0100
                Re: on allowing "int a" definition everywhere Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-03 04:54 +0000
                Re: on allowing "int a" definition everywhere Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-03 06:12 +0000
                Re: on allowing "int a" definition everywhere Michael S <already5chosen@yahoo.com> - 2024-09-03 12:20 +0300
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-27 21:09 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-29 09:24 +0200
                Re: on allowing "int a" definition everywhere fir <fir@grunge.pl> - 2024-08-29 09:37 +0200
                Re: on allowing "int a" definition everywhere Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-03 04:56 +0000
        Re: on allowing "int a" definition everywhere Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-27 12:28 +0000
          Re: on allowing "int a" definition everywhere Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-27 18:45 -0700
            Re: on allowing "int a" definition everywhere Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-27 19:23 -0700
              Re: on allowing "int a" definition everywhere Ben Bacarisse <ben@bsb.me.uk> - 2024-08-28 15:28 +0100
                Re: on allowing "int a" definition everywhere Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-28 08:37 -0700
              Re: on allowing "int a" definition everywhere Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-28 08:22 -0700
                Re: on allowing "int a" definition everywhere Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-28 13:06 -0700
              Re: on allowing "int a" definition everywhere Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-28 09:31 -0700
                Re: on allowing "int a" definition everywhere David Brown <david.brown@hesbynett.no> - 2024-08-28 19:39 +0200
                Re: on allowing "int a" definition everywhere James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-28 14:34 -0400
                Re: on allowing "int a" definition everywhere Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-28 13:06 -0700

csiph-web