Keith said:
The line
"x;"
if x has already been declared is an expression statement that
evaluates and discards the value of x. This would be perfectly valid
if x were declared at file scope.
I don't believe deleting a file-scope declaration of x would transform
the line to a declaration of x with an implicit int type. I don't
remember the C90/C95 rule well enough to be certain that it doesn't,
but I'm fairly sure that the presence or absence of an object
declaration affects the parsing of later code; it can only affect
whether such code is semantically invalid because it does or does not
refer to an undeclared object.
There are differences between C90, C95, and C99. This just isn't one
of them.
Out of curiosity, why are you asking about this? C90 and C95 allow
implicit int, but that doesn't mean you have to use it. Declaring
"int x;" is unambiguously valid in all versions of C, and is better
than "x;" whether "x;" is legal or not.
The code:
#include <stdio.h>
int main()
{
auto x;
x= 9;
printf("%d\n", x);
printf("%lu %lu\n", sizeof(x), sizeof(int));
return 0;
}
under VC++2008 Express worked as it should in C95 mode. Here is the
output of the compiler:
------ Rebuild All started: Project: demo, Configuration: Debug Win32
------
Deleting intermediate and output files for project 'demo', configuration
'Debug|Win32'
Compiling...
demo.c
Linking...
LINK : C:\Documents and Settings\Owner\My Documents\Visual Studio
2008\Projects\demo\Debug\demo.exe not found or not built by the last
incremental link; performing full link
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\Owner\My
Documents\Visual Studio 2008\Projects\demo\demo\Debug\BuildLog.htm"
demo - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
It outputs:
9
4 4
Press any key to continue . . .
Under gcc version 4.1.2
C90 mode:
[john@localhost src]$ gcc -std=iso9899:1990 -pedantic-errors -Wall
main.c -o foobar-cpp
main.c: In function ‘main’:
main.c:5: warning: type defaults to ‘int’ in declaration of ‘x’
main.c:11: warning: format ‘%lu’ expects type ‘long unsigned int’, but
argument 2 has type ‘unsigned int’
main.c:11: warning: format ‘%lu’ expects type ‘long unsigned int’, but
argument 3 has type ‘unsigned int’
[john@localhost src]$
[john@localhost src]$ ./foobar-cpp
9
4 4
[john@localhost src]$
[john@localhost src]$ gcc -std=iso9899:1990 main.c -o foobar-cpp
[john@localhost src]$ ./foobar-cpp
9
4 4
[john@localhost src]$
C95 mode:
[john@localhost src]$ gcc -std=iso9899:199409 main.c -o foobar-cpp
[john@localhost src]$ ./foobar-cpp
9
4 4
[john@localhost src]$
[john@localhost src]$ gcc -std=iso9899:199409 -pedantic-errors -Wall
main.c -o foobar-cpp
main.c: In function ‘main’:
main.c:5: warning: type defaults to ‘int’ in declaration of ‘x’
main.c:11: warning: format ‘%lu’ expects type ‘long unsigned int’, but
argument 2 has type ‘unsigned int’
main.c:11: warning: format ‘%lu’ expects type ‘long unsigned int’, but
argument 3 has type ‘unsigned int’
[john@localhost src]$ ./foobar-cpp
9
4 4
[john@localhost src]$
Without the -Wall we get no warnings:
[john@localhost src]$ gcc -std=iso9899:199409 -pedantic-errors main.c -o
foobar-cpp
[john@localhost src]$ ./foobar-cpp
9
4 4
[john@localhost src]$
For the code:
#include <stdio.h>
int main()
{
x;
x= 9;
printf("%d\n", x);
printf("%lu %lu\n", sizeof(x), sizeof(int));
return 0;
}
under VC++2008 Express (that supports C95):
------ Rebuild All started: Project: demo, Configuration: Debug Win32
------
Deleting intermediate and output files for project 'demo', configuration
'Debug|Win32'
Compiling...
demo.c
c:\documents and settings\owner\my documents\visual studio
2008\projects\demo\demo\demo.c(5) : error C2065: 'x' : undeclared
identifier
c:\documents and settings\owner\my documents\visual studio
2008\projects\demo\demo\demo.c(7) : error C2065: 'x' : undeclared
identifier
c:\documents and settings\owner\my documents\visual studio
2008\projects\demo\demo\demo.c(9) : error C2065: 'x' : undeclared
identifier
c:\documents and settings\owner\my documents\visual studio
2008\projects\demo\demo\demo.c(11) : error C2065: 'x' : undeclared
identifier
Build log was saved at "file://c:\Documents and Settings\Owner\My
Documents\Visual Studio 2008\Projects\demo\demo\Debug\BuildLog.htm"
demo - 4 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Both compilers also compile the following code successfully:
#include <stdio.h>
test(void)
{ return 0;
}
int main()
{
auto x;
x= 9;
printf("%d\n", x);
printf("%lu %lu\n", sizeof(x), sizeof(int));
return 0;
}
So since the above codes work the same with both VC++ 2008 Express
Edition and gcc, while the statement does not work in a similar way with
both compilers, I wonder whether the C90/C95 standards, required at
least one keyword for the implicit int declaration, like auto.