• Register
100 points
5 1

What is main() in c?

posted May 14 2 min read

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

  1. int main() → This will return integer value like:return 0;
  2. void main() → This will return nothing
  3. void main(void) → Return nothing and no arguments to pass(Technically better way to write)
  4. int main(void) → Return integer value and no arguments to pass
  5. main(void) →Return nothing and no arguments to pass

Example

#include<stdio.h>

void main()
{
printf("This is main function which returns nothing/void");
}

Output

This is main function which returns nothing/void

When you want to pass parameters to the main function, the parameters are traditionally named argc and argv, although the C compiler does not require these names. The types for argc and 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.

Syntax

main( int argc, char *argv[ ], char *envp[ ] )

 

1 Comment

This is a helpful post. Thanks for posting