By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,572 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

what's the difference between int main() and void main()?

P: 14
hlo, i've got a question , some people here gived me advice to use int main(),actually , i dont know what int main() is,but in my manual that i read ,the writer of the book says that i've got to use void main().
dont know what the problem is , help me!
Aug 14 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
hlo, i've got a question , some people here gived me advice to use int main(),actually , i dont know what int main() is,but in my manual that i read ,the writer of the book says that i've got to use void main().
dont know what the problem is , help me!
The difference is simple: 'void main()' is just plain wrong. Per the Standard the
return type of main should be int; the int value is used to supply the exit value
of the process that was started by invoking that same 'int main()' function.

It was Microsoft that started this 'void main()' nonsense because the return
values of their processes are very often ignored (for reasons that are beyond me),
but keep in mind: int main good, void main bad; and spread the word.

kind regards,

Jos
Aug 14 '07 #2

P: 26
if you assign void main then it means that main function will not return any thing and you did not have to specify of return in main
if you assign int main then it means that main function will return a int value and you should specify a return statement in the main function otherwise you will get a warning by the compiler

and other thing is that some 32 bit compiler doesn't support void main and you have to specify int main in them
Aug 14 '07 #3

Expert 10K+
P: 11,448
and other thing is that some 32 bit compiler doesn't support void main and you have to specify int main in them
Those are called 'compliant' compilers, i.e. they comply with the Standard. Never
trust a C compiler that accepts 'void main()' without even a warning diagnostic.

kind regards,

Jos
Aug 14 '07 #4

P: 14
AH, you mean that void dont exist , that i must use int ?? and that int main() the head function is?
Aug 14 '07 #5

Expert 10K+
P: 11,448
AH, you mean that void dont exist , that i must use int ?? and that int main() the head function is?
Yep, void main() is just wrong. Here is what the C99 Standard has to say about it:

5.1.2.2.1 Program startup

[#1] The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;8) or in some other implementation-defined
manner.

[#2] If they are declared, the parameters to the main
function shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.

-- If the value of argc is greater than zero, the array
members argv[0] through argv[argc-1] inclusive shall
contain pointers to strings, which are given
implementation-defined values by the host environment
prior to program startup. The intent is to supply to

____________________

8) Thus, int can be replaced by a typedef name defined as
int, or the type of argv can be written as char ** argv,
and so on.
All the 'shall clauses' are considered mandatory.

kind regards,

Jos
Aug 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.