Main Function in C

What is the main() Function in C?

In C, the main function is the entry point of the program. This means that when you run a C program, execution always starts from the main function. Without it, your program simply won’t run.

Why is main So Important?

The C compiler uses main to begin execution. Even if you have other functions in your program, nothing happens unless they’re called inside main (or from a function that was called by main).

Syntax of the main Function

1. Without Parameters:

int main(void) {
    // statements
    return 0;
}

2. With Parameters:

int main(int argc, char *argv[]) {
    // statements
    return 0;
}
  • int argc: Number of command-line arguments.
  • char *argv[]: Array of argument strings (argument vector).

Examples

Example 1: Simple main()

advertisement
#include <stdio.h>
 
int main(void) {
    printf("Welcome to Sanfoundry C Quiz\n");
    return 0;
}

Output:

Welcome to Sanfoundry C Quiz

This simple C program prints “Welcome to Sanfoundry C Quiz”. It uses printf() from stdio.h and has a main() function that returns 0.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

Example 2: main() with Command-Line Arguments

#include <stdio.h>
 
int main(int argc, char *argv[]) {
    printf("Total arguments: %d\n", argc);
    for (int quiz = 0; quiz < argc; quiz++) {
        printf("Argument %d: %s\n", quiz, argv[quiz]);
    }
    return 0;
}

To Run:

./a.out Sanfoundry C Programming

Output:

Total arguments: 3
Argument 0: ./a.out
Argument 1: Sanfoundry
Argument 2: C
Argument 3: Programming

This C program demonstrates command-line arguments. It prints the total number of arguments (argc) and each argument (argv[]). When run with ./a.out Sanfoundry C Programming, it outputs all four arguments. The code helps understand how arguments are passed to main() in C.

Return Value of main()

The return type of main is usually int, which means it returns an integer. This return value tells the operating system whether the program ran successfully.

  • return 0; → Success
  • return 1; or any non-zero → Error

Some older C compilers may allow void main(), but it is not standard. Always prefer using int main() to follow the ANSI C standard.

advertisement

Variations and Best Practices

  • Avoid void main(): Some compilers may allow void main(), but it is not standard. Always use int main() to stay compliant with the C standard.
  • Parameter Naming: While argc and argv are standard and widely used names, you can choose other valid identifiers. However, following common conventions improves code clarity.
  • Implicit Return: In C99 and later standards, if main() reaches its end without a return statement, it automatically returns 0. Even so, it is still considered good practice to explicitly return a value.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.