Variable declarations at the middle of a block works

I

Infinity

As far as I know, all variable declarations and definitions must
happen at the beginning of a block. However, with the gcc on my
system, variable declaration and definition at the middle of a block
also works. Why?

Example output below:

$ cat a.c
#include <stdio.h>

int main()
{
int a = 10;
printf("a = %d\n", a);
int b = 5;
printf("b = %d\n", b);
return 0;
}


inf@CYGWIN-LAPTOP ~
$ gcc -std=c89 a.c; ./a.exe
a = 10
b = 5

inf@CYGWIN-LAPTOP ~
$ gcc -std=c99 a.c; ./a.exe
a = 10
b = 5
 
S

Seebs

As far as I know, all variable declarations and definitions must
happen at the beginning of a block. However, with the gcc on my
system, variable declaration and definition at the middle of a block
also works. Why?

Because it's an extension.
inf@CYGWIN-LAPTOP ~
$ gcc -std=c89 a.c; ./a.exe
a = 10
b = 5

-std=c89 doesn't force the compiler to reject invalid code; you probably
need at least -ansi -pedantic for that.
inf@CYGWIN-LAPTOP ~
$ gcc -std=c99 a.c; ./a.exe
a = 10
b = 5

C99 allows mixed declarations and code anyway.

-s
 
I

Infinity

Because it's an extension.


-std=c89 doesn't force the compiler to reject invalid code; you probably
need at least -ansi -pedantic for that.


C99 allows mixed declarations and code anyway.

Thanks. Is there any way to find out what language (related to -x
option) or standard (related to -std option) gcc is using by default
when I ask to compile a.c without any options?
 
I

Ian Collins

Infinity said:
Thanks. Is there any way to find out what language (related to -x
option) or standard (related to -std option) gcc is using by default
when I ask to compile a.c without any options?

It use gcc! With without any options, gcc uses it's own dialect.
 
S

Seebs

Thanks. Is there any way to find out what language (related to -x
option) or standard (related to -std option) gcc is using by default
when I ask to compile a.c without any options?

The documentation. Which I'm pretty sure will say it's "GNU C".

-s
 
N

Nobody

Thanks. Is there any way to find out what language (related to -x
option) or standard (related to -std option) gcc is using by default
when I ask to compile a.c without any options?

The gcc documentation says that the default is -std=gnu89, i.e. C89 plus
GNU extensions.
 
K

Keith Thompson

Francis Glassborow said:
No it is not false. It is just that GNU C in its non-standard mode had
this feature, nonetheless the restriction was removed in C99.

It's true that the restriction is removed in C99.

It's true that gcc by default allows mixed declarations and
statements.

It's not true that gcc by default allows mixed declarations and
statements *because* the restriction is removed in C99. If you
follow the history, both can probably be trace back to a common
cause; it's likely that both the gcc extension and the C99 rule
change were inspired by C++. But even if C99 hadn't picked up that
change, gcc probably would have. (And let's not quibble about that
last "probably", please; I know gcc implemented this before 1999.)
 
F

Flash Gordon

Francis said:
Because that restriction was removed in C99.

You should have read the rest of the post. C99 was not relevant to most
of the examples, since the compiler was not in any form of C99 related
mode. Others have posted correct answers.
 
K

Kaz Kylheku

No it is not false. It is just that GNU C in its non-standard mode had

The gcc command operates in this the non-standard mode, unless requested
otherwise. When requested otherwise, it may be in any one of a number of other
modes, some of which impose the restriction, and some of which do not.
this feature, nonetheless the restriction was removed in C99.

What makes it false is the ``Because'' part, not what follows.

C99 is not the reason why gcc accepts declarations after statements; the
reasons it that gcc's default dialect for C programs is something called gnu89,
which supports a plethora of traditional, long-time gcc features.

gcc still has (not ``had'') that same non-standard mode with that
feature, and it is default behavior.

Some features of gnu89 happen to be compatible with C99. But the dialect
does not provide new c99 features, nor does it fix incompatibilities between
its extensions and things that c99 requires to be done differently, like for
instance inline functions.

I.e. this dialect did not change when C99 came out, only some of its features
are redubbed in the documentation as ``C99 features''.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top