This question already has an answer here:
- What should main() return in C and C++? 18 answers
I am a beginner in C language. Can anyone explain in detail using example how main(),int main(), void main(), main(void), void main(void), int main(void) work in C language? As in what is happeneing when we use void main() and what is happening when i use int main() in simple language and so on.
I know but cant understand what is it doing:
- main() - function has no arguments
- int main() - function returns int value
- void main() - function returns nothing etc.
when i write simple hello world using the int main() return 0 it still gives me the same output as when using void main()) so how does it work? What is its application?
Neither
main()
orvoid main()
are standard C. The former is allowed as it has an implicitint
return value, making it the same asint main()
. The purpose ofmain
's return value is to return an exit status to the operating system.In standard C, the only valid signatures for
main
are:and
The form you're using:
int main()
is an old style declaration that indicatesmain
takes an unspecified number of arguments. Don't use it - choose one of those above.If you really want to understand ANSI C 89, I need to correct you in one thing; In ANSI C 89 the difference between the following functions:
is:
int main()
int main(void)
int main(int argc, char * argv[])
About when using each of the functions
About
void main()
In ANSI C 89, when using
void main
and compiling the project AS-ansi -pedantic
(in Ubuntu, e.g) you will receive a warning indicating that your main function is of type void and not of type int, but you will be able to run the project. Most C developers tend to useint main()
on all of its variants, thoughvoid main()
will also compile.Hope that helps!
Good luck and happy coding