Every C program code run in a hosted execution environment contains the definition (not the prototype) of a function called
main, which is the designated start of the program.
main() function is the first function in our program which will execute while compiling but it is not the very first function which is to be executed.The first function is _start(), which is typically provided by the C runtime library, linked in automatically when your program is compiled. When a program executes,thecontrol passes to main main() function.It usually controls the program execution by directing the calls to other function in main program.If in case,we need to make any function then we have to call that function in the main() function.Main function has return statement which will ensure that program has been executed or terminated.Main is the name of function which is predefined in C/C++ library.
Types of main function
- int main() → This will return integer value like:return 0;
- void main() → This will return nothing
- void main(void) → Return nothing and no arguments to pass(Technically better way to write)
- int main(void) → Return integer value and no arguments to pass
- main(void) →Return nothing and no arguments to pass
printf("This is main function which returns nothing/void");
This is main function which returns nothing/void
When you want to pass parameters to the main function, the parameters are traditionally named
argv, although the C compiler does not require these names. The types for
argv are defined by the C language. Traditionally, if a third parameter is passed to main, that parameter is named
envp.All these parameters are known as command line arguments.
main( int argc, char *argv[ ], char *envp[ ] )