I am a beginner in the 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 happening when we use void main() and what is happening when I use int main() in simple language and so on?
I know, but I can’t understand what is it doing:
When I write a simple Hello, World! program 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?
The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().
Actually, both seem to be the same but, int main(void) is technically better as it clearly mentions that main can only be called without any parameter.
int main() and int main(void) are same ,i.e they can only be called without any arguments. int main() can be called with number of arguments but int main(void) can only be called without any argument and this applies for any function.
No. It's non-standard. The standard prototype of main is int main() with the optional command line arguments argc and argv . The int returned by main() is a way for a program to return a value to the system that invokes it.
Neither main()
or void main()
are standard C. The former is allowed as it has an implicit int
return value, making it the same as int main()
. The purpose of main
's return value is to return an exit status to the operating system.
In standard C, the only valid signatures for main
are:
int main(void)
and
int main(int argc, char **argv)
The form you're using: int main()
is an old style declaration that indicates main
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:
int main()
int main(void)
int main(int argc, char* argv[])
is:
int main()
int main(void)
int main(int argc, char * argv[])
About when using each of the functions
int main(void)
int main(int argc, char * argv[])
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 use int main()
on all of its variants, though void main()
will also compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With