-
main() question
Hi,
i know that main() returns an int,
but, check these out:
Code:
#include <stdio.h>
main()
{
printf("Hello, world.");
return 0;
}
Code:
#include <stdio.h>
int main()
{
printf("Hello, world.");
return 0;
}
These programs both work properly,
so isnt the 'int' keyword in front of main() useless?
Because why put an int in front of it if without an int works also fine...
thank you
encrypted
-
The latest standard states that you MUST declare the return type. If you don't then your code is not valid C code. It may work on one compiler, but it may not on another. Always explicitly specify "int" as the return type of main.
-
Polymorphic is right...
Maybe your code without 'int' works fine in your compile, and don't give you any warning or error.
Now, I tried to compile under unix (with gcc) a code that contains, void main(), and guess what?
yonis.cpp:300: `main' must return `int' :mad:
Now, if you want to see, a more specific example, try looking
here
Good luck.
-
Yeah, well i knew that void main() would give an error with gcc,
anyway 'int main()' is necessary because it becomes a standard to specify the return type right :)
thats what i wanted to know
thank you guys
encrypted
-
It used to be that if you didn't specify a return type it was implied you meant int. It's not like that anymore however.
-
My motto from now on :"If you are to lazy for three little letters then you are to lazy to program." Not the best in the world but it gets the point across with main().