When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it’s the main() function that the operating system is looking for. The set of parentheses after a C language function name is used to contain any arguments for the function — stuff for the function to digest.
Why do we use return 0;?
In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the “Exit Status” of the application. When we return 0 at the end ( while using int as the return type) it tells the OS that the program is functional and is without any error(except logical error), as main function is called by OS and it returns 0 to the OS. On most operating systems returning 0 is a success status like saying “The program worked fine”.
Main returns are useful for debugging, program analysis and lots of stuff when you get serious with your programming.
On C++ compilers that use C99 and above, if you skip return statement in your main function, compiler would add a return 0 or return EXIT_SUCCESS for you. It varies from compiler to compiler but it’s important to keep in mind that main is the only function where omitting return is allowed unless that functions return type is void.
In summary, it’s good practice to use it since it reports any errors back to the environment in which it was run. Wherever the return statement is encountered, it marks that the function stops there.
What is the difference between int main() and int main(void)?
In C++, there is no difference, both are same.
Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter.
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters.
Why do we always have to use int with our main() function?
The return value of main is to be passed to the operating system (any operating system) in a single, consistent way. The information that the operating system needs to know is “did the program terminate successfully, or was there an error?” Returning an integer is the easiest way to do it on the OS level.
The key point to understand is that main – unlike other functions in any program – is not part of a protocol defined by the programmer, but the protocol used to interface with the host (OS).
What about void main()?
The keyword just before the function name is the return type of the function. If a function has void as its return type then you can’t return a value, so you can’t have return 0; inside that function.
void main() should not be used as it is not a good practice. C void main is permitted only if the compiler specifically supports it. Many compilers don’t complain very loudly if you write void main.
And what about argc & argv?
The main() function uses its parentheses to contain any information typed after the program name at the command prompt. You might think that the command line is a relic of the past but even today when the world runs on graphical operating systems internally all graphical operating systems still reference the command line and a c program can read these command line options and arguments.
Command line arguments are arguments that you can give to certain programs before they run. They are placed after the command to run an executable.
So how do we tell our program that it should be expecting command line arguments? We just have to include a few arguments when main() is declared.
When you don’t plan on your program accepting any command line arguments, you can leave the main() function’s parentheses empty.
Using them looks like this: int main()
But when arguments are used in your code, they must be declared.
Using them looks like this: int main(int argc, char *argv)
argc is the argument count value. It’s an integer that ranges from 1 through however many items were typed after the program name at the command prompt.
*argv is an array of char pointers. You can think of it instead as an array of strings, which is how it can be used in your code.
The main() function receives information about the command-line argument directly from the operating system. The command line is evaluated, and arguments are tallied and referenced. The tally appears as argc, and the references are stored in the argv array.