Main Function in C

The main function in C is one of the most important concepts in the language because program execution starts from it. No matter how many functions a C program has, the system begins execution by entering the main() function. That is why every normal C program must have a properly defined main function.

Beginners often memorize int main() or int main(void) without understanding what it actually means. They may also get confused by return 0, command line arguments, or whether void main() is valid. In this article, we will understand the main function in C in a clear technical way, including syntax, purpose, return type, arguments, common forms, startup behavior, and common mistakes.

What is Main Function in C?

The main function in C is the entry point of a program. When a C program runs, execution begins from main(). This function controls the starting flow of the program and usually coordinates the rest of the logic by calling other functions.

In simple words, if a C program is a machine, then main() is the place where the machine is switched on.

Execution in a standard C program starts from main(), not from the first function written in the file.

Syntax of Main Function in C

The two most common standard forms are:

int main(void)
{
    return 0;
}
int main(int argc, char *argv[])
{
    return 0;
}
PartMeaning
intThe function returns an integer status code to the operating system
mainName of the program entry function
voidIndicates no arguments are accepted in that form
argcNumber of command line arguments
argvArray of argument strings

These forms are standard and portable. They should be preferred over non-standard variations.

Why Main Function is Important in C

  • it marks the starting point of program execution
  • it organizes the top-level flow of the program
  • it can return a status code to the operating system
  • it may receive command line arguments
  • it connects the runtime startup process to your program logic

Without a valid main() function, a normal hosted C program will not build or run correctly.

How Main Function Works in C

  1. The program is loaded into memory.
  2. Runtime startup code performs low-level initialization.
  3. Control is transferred to main().
  4. The statements inside main() execute.
  5. The function returns a status code when finished.

This is an important detail: the processor does not jump directly from nowhere into your source code. There is usually startup code before main() that prepares the runtime environment.

Simple Example of Main Function in C

Here is the most common beginner-level form:

#include <stdio.h>

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

This program prints a line and then returns 0. The return value tells the operating system that the program ended successfully.

What Does return 0 Mean in Main Function?

The statement return 0; indicates successful termination of the program. In hosted environments such as Windows, Linux, and macOS, the integer returned by main() is passed back to the operating system.

Return valueMeaning
0Program completed successfully
non-zero valueProgram ended with some error or special condition

In many small beginner programs, people always write return 0;. That is fine, but you should also understand why it exists.

Different Valid Forms of Main Function in C

The most commonly accepted standard forms are:

  • int main(void)
  • int main(int argc, char *argv[])
  • int main(int argc, char **argv)

The second and third forms are equivalent in meaning because char *argv[] and char **argv both represent an array of strings.

Main Function with Command Line Arguments in C

When a program is run from the command line, arguments can be passed to main().

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Number of arguments: %d\n", argc);
    printf("Program name: %s\n", argv[0]);
    return 0;
}

Here:

  • argc stores the number of command line arguments
  • argv stores the actual argument strings
  • argv[0] usually contains the program name or path

This form is useful when your program needs input from the command line without interactive typing.

Difference Between int main() and int main(void)

This topic confuses many beginners because both forms appear in tutorials.

FormMeaningPractical note
int main()Function with unspecified parameters in old-style C declaration contextCommonly seen, but less explicit
int main(void)Function that takes no parametersClearer and better style in standard C

For clear modern teaching, int main(void) is usually the better form when no command line arguments are needed.

Is void main() Valid in C?

void main() is a very common beginner mistake because some old compilers and tutorials allowed it. But in standard hosted C programming, the proper return type of main() is int.

That means this is not the recommended form:

void main()
{
    /* not recommended for standard hosted C */
}

The correct standard style is:

int main(void)
{
    return 0;
}

Some embedded environments may behave differently because of custom startup models, but for general C learning and standard hosted programs, int main is the right form.

Can We Call main() from Another Function in C?

Technically, some compilers may allow it in certain cases, but it is not good design and should be avoided. The main function is meant to be the entry point of the program, not a reusable normal function.

If you need logic to run multiple times, place that logic inside a separate helper function and call that helper function from main().

Main Function and Other Functions in C

In well-structured C programs, main() should coordinate program flow, not contain every line of logic itself.

#include <stdio.h>

void displayMessage(void)
{
    printf("Program started\n");
}

int main(void)
{
    displayMessage();
    return 0;
}

This is better design than placing all logic directly into main(). As programs grow, main() should stay readable and act as the high-level control point.

Main Function in Embedded C

In embedded systems, the role of main() is still very important, but the environment may differ from a desktop operating system. Startup code may initialize memory sections, configure clocks, and prepare hardware before control reaches main().

In many embedded programs, main() performs initialization first and then enters an infinite loop.

int main(void)
{
    initHardware();

    while (1)
    {
        runApplication();
    }
}

This pattern is very common in bare-metal embedded coding, where there may be no operating system returning control after the program starts.

Common Mistakes with Main Function in C

  • using void main() in standard hosted C programs
  • forgetting to return an integer status code
  • putting too much program logic directly inside main()
  • not understanding argc and argv
  • trying to use main() like a normal reusable function
MistakeProblemBetter approach
void main()Non-standard in hosted CUse int main(void) or argument form
No clear return statusLess proper program termination behaviorUse return 0; for success
Huge main functionPoor readability and maintenanceMove logic into helper functions

Best Practices for Main Function in C

  • Use int main(void) when no command line arguments are needed.
  • Use int main(int argc, char *argv[]) when argument handling is required.
  • Keep main() short and readable.
  • Move detailed logic into separate functions.
  • Return a meaningful status code when appropriate.
  • Do not use void main() for standard C teaching.

FAQs

What is main function in C?

The main function in C is the entry point of a program where execution begins.

Why does main function return int in C?

The main function returns an integer status code to the operating system to indicate successful or unsuccessful program termination.

What is the difference between int main() and int main(void)?

int main(void) clearly means the function takes no parameters, while int main() is less explicit in standard C declaration terms.

Is void main() valid in C?

For standard hosted C programs, void main() is not the recommended form. Use int main instead.

What are argc and argv in main function?

argc stores the number of command line arguments, and argv stores the argument strings.

Can a C program run without main()?

A normal hosted C program is expected to have a valid main() function. Without it, the program will generally not link or run properly.