Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > main() in C90/C95

Reply
Thread Tools

main() in C90/C95

 
 
Kenny McCormack
Guest
Posts: n/a
 
      03-01-2008
In article <9a631$47c9cedb$541dfcd3$(E-Mail Removed)1.nb.ho me.nl>,
Harald van Dijk <(E-Mail Removed)> wrote:
....
>Thank you. I now see it also allows 'x = 42;' (and of course, the original
>'x= 7;') at global scope, it's just at function scope that it rejects it.
>As you mentioned, a diagnostic is generated for the code, so the
>standard's requirements are met.


And thank GOD for that.

 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-02-2008
Keith Thompson wrote:
>
> 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.
>
>> If it can't work in C90/C95 mode, why do they have these command
>> options anyway?

>
> 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)
{
}


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.
 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-02-2008
A minor correction:


Ioannis Vranos wrote:
> Keith Thompson wrote:
>>
>> 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.
>>
>>> If it can't work in C90/C95 mode, why do they have these command
>>> options anyway?

>>
>> 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.

 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-02-2008
The text corrected:


Keith Thompson wrote:
>
> 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.
>
>> If it can't work in C90/C95 mode, why do they have these command
>> options anyway?

>
> 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 that get compiled successfully, work the same
with both VC++ 2008 Express Edition and gcc, while the x; statement does
not work, again in a similar way with both compilers, I wonder whether
the C90/C95 standards, required at least one keyword for the implicit
int declaration of a variable, like auto.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments