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


Groups > comp.lang.c > #167778

Re: variable addresses on the stack

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: variable addresses on the stack
Date 2022-09-21 15:23 -0700
Organization None to speak of
Message-ID <87czbofjtw.fsf@nosuchdomain.example.com> (permalink)
References <54f99ed2-fe4d-4cab-944c-cb61799d64ecn@googlegroups.com> <871qtc868m.fsf@nosuchdomain.example.com> <f2dfb3b5-379f-42d8-9bdc-99051a56d7e0n@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:
> On Friday, August 19, 2022 at 2:59:21 PM UTC-3, Keith Thompson wrote:
>> Thiago Adams <thiago...@gmail.com> writes: 
>> > I am curious with the following.. 
>> > 
>> > void f(int i) { 
>> > if (i > 1) { 
>> > int j ='a'; 
>> > } 
>> > if (i < 1) { 
>> > int k ='b'; 
>> > } 
>> > } 
>> 
> [...]
>> And yes, block local variables have been supported for a long time. 
>> A 1975 C reference manual allowed declarations only at the top of 
>> a function body, but K&R1 (1978) allows declarations within any 
>> compound statement. 
>
> C99 added "mixed declarations and code"
>
> I changed the code adding variable 'm'
>
> void f(int i) { 
>  
>  if (i > 1) { 
>    int j ='a'; 
>  } 
>  
>  if (i < 1) { 
>    int k ='b'; 
>  } 
>  
>  int  m = 1;
> } 
>
> gcc with -std=c90 -pedantic 
> gives:
>
> "warning: mixing declarations and code is a C99 extension [-Wdeclaration-after-statement]"
>  int  l = 1;
>       ^
> So, block declarations is something very old, but "mixed" declarations are new 
> in C99.
>
> Very weird because adding { }  around the "mixed" declaration it just works.
> I cannot see a reason for this limitation, but I guess compilers use to accept
> mixed declarations for a long time.
>
> void f(int i) { 
>  
>  if (i > 1) { 
>    int j ='a'; 
>  } 
>  
>  if (i < 1) { 
>    int k ='b'; 
>  } 
>  {
>   int  m = 1;
>  }
> } 

There's nothing weird about it.  C89, and older versions going back at
least to K&R1 C (1978), allowed zero or more declarations followed by
zero or more statements within a block.  By adding { } you created a
block, which can begin with a declaration.  (Of course your `int j` is
not visible outside the block, and nothing refers to it, so you might
get a warning about an unused variable.)  The C99 change was to allow
declarations and statements to be mixed within a block.

It was only some very old versions of C (1975 and earlier) that allowed
declarations only within the top-level block of a function definition.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-08-19 10:22 -0700
  Re: variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-08-19 10:27 -0700
  Re: variable addresses on the stack Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-19 10:59 -0700
    Re: variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-09-21 05:09 -0700
      Re: variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-09-21 05:18 -0700
        Re: variable addresses on the stack Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-21 15:30 -0700
      Re: variable addresses on the stack David Brown <david.brown@hesbynett.no> - 2022-09-21 15:34 +0200
      Re: variable addresses on the stack Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-21 15:23 -0700
  Re: variable addresses on the stack Kaz Kylheku <480-992-1380@kylheku.com> - 2022-08-19 20:58 +0000
  Re: variable addresses on the stack Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2022-08-19 15:47 -0600
  Re: variable addresses on the stack Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-19 18:47 -0700

csiph-web