Understanding Main() Function in C

Understanding Main() Function in C Language

In this article, I will discuss the Main Function in C Language. Please read our previous article discussing User-Defined Functions in C Language with Examples.

Main Function in C Language

The main() function in C language is the entry point for a C program. When a C program is executed, the execution starts from the main() function. This function is essential for every C program and has a specific structure. Here’s a breakdown of its key aspects:

Function Definition: The main() function is defined like any other function in C, but it’s special because it’s automatically called when the program starts.

Return Type: The return type of main() is int, indicating that it returns an integer to the operating system. The return value is usually used to indicate whether the program executed successfully or if there were errors.

Syntax: The basic syntax is int main(void) for a program that doesn’t take command-line arguments or int main(int argc, char *argv[]) for a program that does.


  • argc (Argument Count): An integer that represents the number of command-line arguments passed to the program.
  • argv (Argument Vector): An array of character pointers listing all the arguments. argv[0] is the name of the program itself, and argv[1] is the first command-line argument, and so on.

Return Values:

  • Returning 0 typically means that the program executed successfully.
  • Non-zero values are used to indicate different types of errors. The specific meaning can vary by convention and operating system.

Execution Flow: When you run a C program, the system calls your main() function first. Once the code in main() finishes, the program ends. If main() calls other functions, those functions return to main() when they are complete.

No Arguments Version: If your program doesn’t need command-line arguments, you can use int main(void), which explicitly states that main() takes no parameters.

#include <stdio.h>

int main() {
    //Your Code Goes Here
    return 0;


  • #include <stdio.h>: This line includes the Standard Input Output header file (stdio.h), which is necessary for using input/output functions like printf() and scanf().
  • int main(): This is the declaration of the main() function. The int before main() indicates that this function returns an integer. In the context of the main() function, returning 0 typically indicates that the program executed successfully.
  • {}: The curly braces define the start and end of the function’s body.
  • // Your code goes here: This is a comment indicating where you can put the executable code of your program.
  • return 0;: This is the return statement of the main() function. It returns an integer value to the operating system. A return value of 0 typically signifies that the program has been executed successfully without errors.

Here’s a simple example of a C program using the main() function:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;

When this program runs, it will print “Hello, World!” to the console and exit with a return status of 0, indicating successful execution.

Main Function Key Points in C.
  1. The main() function is an identifier in the program, which indicates the starting point of program execution. The main() function is a user-defined function with the pre-implemented signature of the linker. The linker will always search to start the program from main() only. That’s why the main() function should be required.
  2. We can design the program without using the main() function, but we can’t execute it because compilation can be a success. But, the execution is a failure (linker error will occur).
  3. In any application, only one main() function should be required. If we are placing more than one main() function, then the compiler will give an error, i.e., multiple declarations for main().
  4. Generally, the main function does not return any value, so the return type of the main function is void. When we need to return the exit status of an application, then the main function return type should be an int.
  5. The void main() function does not provide any existing status back to the Operating System. int main function will provide existing status back to the operating system i.e. success or failure.
  6. When the main function return type is mentioned as an integer type, then it is possible to return the values from -32768 to 32767, but those are all meaningless except 1 and 0. When the user explicitly terminates the program, the exit code is -1.

In the next article, I will discuss Types of User-Defined Functions in C Language with Examples. In this article, I explain the Main Function in C Language. I hope you enjoy this Main Function in C Language article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *