declaration of variables inside functions in C

H

Henri Manson

Today I discovered that the following code fragment compiles under gcc 3.0
(even with the -ansi option) and doesn't compile under gcc 2.95. As far as I
know it's not allowed in C to declare variables in a block after the first C
statement.

#include <stdio.h>
main()
{
int x;

printf("Hello world\n");
int y;
return 0;
}

But propably the C standard has changed. Can somebody tell me something
about that change?
 
M

Mike Wahler

Henri Manson said:
Today I discovered that the following code fragment compiles under gcc 3.0
(even with the -ansi option) and doesn't compile under gcc 2.95. As far as I
know it's not allowed in

The 'C90' incarnation of
C to declare variables in a block after the first C
statement.

It is valid for 'C99'.
Check your configuration settings. Your compiler
is probably configured to compile 'C99' (or possibly
C++, where this construct has always been valid)

#include <stdio.h>
main()

If the compiler accepted 'inline' object declarations like you say,
I'd expect it to reject a function sans a return type.

int main()
{
int x;

printf("Hello world\n");
int y;
return 0;
}

But propably the C standard has changed.

Yes. About four years ago.
Can somebody tell me something
about that change?

Not here. That would be a very long post. :)

http://www.eskimo.com/~scs/C-faq/q11.2.html

-Mike
 
M

Micah Cowan

Henri Manson said:
Today I discovered that the following code fragment compiles under gcc 3.0
(even with the -ansi option) and doesn't compile under gcc 2.95. As far as I
know it's not allowed in C to declare variables in a block after the first C
statement.

That's correct.
#include <stdio.h>
main()
{
int x;

printf("Hello world\n");
int y;
return 0;
}

But propably the C standard has changed. Can somebody tell me something
about that change?

The C standard has changed, but that's somewhat irrelevant, as
the -ansi option for gcc still implies C90. Note, too, that -ansi
does not put gcc into ISO-conformance mode: you need -pedantic as
well. And usually it is recommended to add -W -Wall -O2 for good
measure, as well. If you add -pedantic, then gcc-3.0 will at
least issue the required diagnostic for the constraint violation.

The main thing that appears to have changed, is that gcc-3.0
added declarations intermixed with statements as an extension,
whereas gcc-2.95.* did not.

Ironically, the code above is not legal in either C90 (which
does not allow intermixed declarations and statements) nor in C99
(which does); since C99 has disallowed the implicit "int" return
type in function declarations (and, FYI, has also deprecated
empty parentheses, too). For maximum portability, try:

#include <stdio.h>
int main(void)
{
int x, y;

printf("Hello world!\n");
return 0;
}

HTH,
Micah
 
D

Dave Thompson

"Henri Manson" <[email protected]> writes:
[ mixed declaration and statements ]
The C standard has changed, but that's somewhat irrelevant, as
the -ansi option for gcc still implies C90. Note, too, that -ansi
does not put gcc into ISO-conformance mode: you need -pedantic as
well. And usually it is recommended to add -W -Wall -O2 <snip>

Right. -ansi only causes those cases where GNU extensions _conflict_
with standard C to be resolved in favor of the standard. (Including
disabling the GNU extension of not doing trigraphs. said:
The main thing that appears to have changed, is that gcc-3.0
added declarations intermixed with statements as an extension,
whereas gcc-2.95.* did not.

Ironically, the code above is not legal in either C90 (which
does not allow intermixed declarations and statements) nor in C99
(which does); since C99 has disallowed the implicit "int" return
type in function declarations

And everywhere in all declarations and definitions; also implicit
function declarations.
(and, FYI, has also deprecated
empty parentheses, too). <snip>

But this is not a change. C89 deprecated old-style/K&R1 definitions
(identifier-list followed by declarations) and declarations (empty
parens). C99 retained them as still deprecated.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top