Re: on allowing "int a" definition everywhere

Liste des GroupesRevenir à l c 
Sujet : Re: on allowing "int a" definition everywhere
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.c
Date : 21. Aug 2024, 15:01:12
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <va4oeo$3s6p8$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 21/08/2024 09:34, Bart wrote:
On 21/08/2024 12:17, Thiago Adams wrote:
On 21/08/2024 01:42, Blue-Maned_Hawk wrote:
Thiago Adams wrote:
>
initializer inside if is already in C++, and it will probably be on C2Y.
If it's for consistency with how for loops permit declarations, i would
_much_  prefer that they just outlaw that to induce consistency.
>
(Really, i'd ideally want things to just stay as they are, since
declarations in for loops are simply too useful for macros.)
>
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
 Run-time or compile-time error?
Compile time. f is not visible outside.
 It won't be a compile-time error if there is another 'f' visible in that outer scope.
 And it won't be one here either:
   if (FILE* f = fopen("file.txt", "r"))
  {
     /*...*/
     fclose(f);
     fwrite(f, ..) ;// ERROR
  }
 
For this type of error you need my static analyzer. (cake) :D
http://thradams.com/cake/playground.html?code=I3ByYWdtYSBzYWZldHkgZW5hYmxlCiNpbmNsdWRlIDxzdGRpby5oPgppbnQgbWFpbigpCnsKICAgRklMRSogX093bmVyIF9PcHQgZjsKICAgaWYgKCBmID0gZm9wZW4oImZpbGUudHh0IiwgInIiKSkKICAgewogICAgY2hhciBhW109ImFiIjsKICAgIGZjbG9zZShmKTsgICAgIAogICAgZndyaXRlKGEsIDEsIDIsIGYpOyAgICAgCiAgIH0KfQo%3D&to=-1&options=

Meanwhile, I'd have a serious problem with the extra clutter this causes. Since you are not reporting an fopen failure, I'd write this as:
    FILE* f;
    f = fopen("file.txt", "r");
   if (f) {
      // ...
      fclose(f);
   }
 However I'd be more likely check for failure first.
It is something optional to use.

Since I did a recent survery where, on a average, functions only had 3 local variables (in a selection of apps), I've been even more sceptical of block-scoped variables.
 

Date Sujet#  Auteur
20 Aug 24 * on allowing "int a" definition everywhere62fir
20 Aug 24 +* Re: on allowing "int a" definition everywhere24Thiago Adams
20 Aug 24 i+* Re: on allowing "int a" definition everywhere4fir
20 Aug 24 ii+- Re: on allowing "int a" definition everywhere1fir
20 Aug 24 ii`* Re: on allowing "int a" definition everywhere2Thiago Adams
20 Aug 24 ii `- Re: on allowing "int a" definition everywhere1fir
21 Aug 24 i`* Re: on allowing "int a" definition everywhere19Blue-Maned_Hawk
21 Aug 24 i `* Re: on allowing "int a" definition everywhere18Thiago Adams
21 Aug 24 i  +* Re: on allowing "int a" definition everywhere4Bart
21 Aug 24 i  i`* Re: on allowing "int a" definition everywhere3Thiago Adams
21 Aug 24 i  i `* Re: on allowing "int a" definition everywhere2Michael S
21 Aug 24 i  i  `- Re: on allowing "int a" definition everywhere1Thiago Adams
22 Aug 24 i  `* Re: on allowing "int a" definition everywhere13Blue-Maned_Hawk
22 Aug 24 i   +* Re: on allowing "int a" definition everywhere5Keith Thompson
22 Aug 24 i   i`* Re: on allowing "int a" definition everywhere4Ben Bacarisse
22 Aug 24 i   i +- Re: on allowing "int a" definition everywhere1Keith Thompson
22 Aug 24 i   i `* Re: on allowing "int a" definition everywhere2Kaz Kylheku
23 Aug 24 i   i  `- Re: on allowing "int a" definition everywhere1Ben Bacarisse
22 Aug 24 i   `* Re: on allowing "int a" definition everywhere7Bart
22 Aug 24 i    +* Re: on allowing "int a" definition everywhere2Thiago Adams
22 Aug 24 i    i`- Re: on allowing "int a" definition everywhere1Blue-Maned_Hawk
23 Aug 24 i    `* Re: on allowing "int a" definition everywhere4fir
23 Aug 24 i     `* Re: on allowing "int a" definition everywhere3Bart
23 Aug 24 i      `* Re: on allowing "int a" definition everywhere2fir
23 Aug 24 i       `- Re: on allowing "int a" definition everywhere1fir
21 Aug 24 `* Re: on allowing "int a" definition everywhere37Lawrence D'Oliveiro
21 Aug 24  +* Re: on allowing "int a" definition everywhere2Ben Bacarisse
21 Aug 24  i`- Re: on allowing "int a" definition everywhere1Ben Bacarisse
25 Aug 24  `* Re: on allowing "int a" definition everywhere34fir
27 Aug 24   `* Re: on allowing "int a" definition everywhere33Lawrence D'Oliveiro
27 Aug 24    +* Re: on allowing "int a" definition everywhere21fir
27 Aug 24    i`* Re: on allowing "int a" definition everywhere20fir
27 Aug 24    i `* Re: on allowing "int a" definition everywhere19fir
27 Aug 24    i  +* Re: on allowing "int a" definition everywhere3fir
27 Aug 24    i  i`* Re: on allowing "int a" definition everywhere2fir
27 Aug 24    i  i `- Re: on allowing "int a" definition everywhere1fir
27 Aug 24    i  `* Re: on allowing "int a" definition everywhere15fir
27 Aug 24    i   +* Re: on allowing "int a" definition everywhere13fir
27 Aug 24    i   i+* Re: on allowing "int a" definition everywhere9Bart
27 Aug 24    i   ii+* Re: on allowing "int a" definition everywhere3fir
27 Aug 24    i   iii`* Re: on allowing "int a" definition everywhere2fir
27 Aug 24    i   iii `- Re: on allowing "int a" definition everywhere1fir
2 Sep 24    i   ii`* Re: on allowing "int a" definition everywhere5Lawrence D'Oliveiro
2 Sep 24    i   ii `* Re: on allowing "int a" definition everywhere4Bart
3 Sep 24    i   ii  `* Re: on allowing "int a" definition everywhere3Lawrence D'Oliveiro
3 Sep 24    i   ii   +- Re: on allowing "int a" definition everywhere1Kaz Kylheku
3 Sep 24    i   ii   `- Re: on allowing "int a" definition everywhere1Michael S
27 Aug 24    i   i`* Re: on allowing "int a" definition everywhere3fir
29 Aug 24    i   i `* Re: on allowing "int a" definition everywhere2fir
29 Aug 24    i   i  `- Re: on allowing "int a" definition everywhere1fir
3 Sep 24    i   `- Re: on allowing "int a" definition everywhere1Lawrence D'Oliveiro
27 Aug 24    `* Re: on allowing "int a" definition everywhere11Blue-Maned_Hawk
28 Aug 24     `* Re: on allowing "int a" definition everywhere10Tim Rentsch
28 Aug 24      `* Re: on allowing "int a" definition everywhere9Keith Thompson
28 Aug 24       +* Re: on allowing "int a" definition everywhere2Ben Bacarisse
28 Aug 24       i`- Re: on allowing "int a" definition everywhere1Tim Rentsch
28 Aug 24       +* Re: on allowing "int a" definition everywhere2Tim Rentsch
28 Aug 24       i`- Re: on allowing "int a" definition everywhere1Keith Thompson
28 Aug 24       `* Re: on allowing "int a" definition everywhere4Tim Rentsch
28 Aug 24        +- Re: on allowing "int a" definition everywhere1David Brown
28 Aug 24        +- Re: on allowing "int a" definition everywhere1James Kuyper
28 Aug 24        `- Re: on allowing "int a" definition everywhere1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal