main
main is the function a hosted C program starts in. The runtime calls it after static initialization, and the integer it returns becomes the process exit status.
Detailed Explanation
A hosted implementation — a desktop OS with a C library — promises that your program begins at main. The compiler and linker arrange a small piece of startup code, often named _start, that sets up the C runtime and then calls main. You write main. You do not write _start.
The standard allows two portable signatures. int main(void) takes no arguments. int main(int argc, char *argv[]) receives the argument count and a NULL-terminated array of argument strings. argv[0] is the program name as the environment supplied it. argv[1] through argv[argc - 1] are the arguments. argv[argc] is always a null pointer, so a loop can walk the array without looking at argc.
Some systems also accept int main(int argc, char *argv[], char *envp[]), where envp is a NULL-terminated list of KEY=value strings. That form is common and useful, and it is not required by ISO C. Portable code reads the environment with getenv instead.
main must return int. Returning 0 or the macro EXIT_SUCCESS from <stdlib.h> means success. Returning EXIT_FAILURE or any non-zero value means failure. Falling off the closing brace of main is a special case: the compiler inserts return 0 for you. No other function gets that gift. If you write void main(void), you have left the standard. Some compilers accept it as an extension; others warn. Do not.
A return from main is not the only way to leave. exit runs atexit handlers and flushes stdio buffers, then terminates. _Exit and abort skip some or all of that cleanup. Recursing into main is allowed by the standard and almost never a good idea, because readers assume it is the unique entry point.
Freestanding implementations — kernels, bootloaders, microcontrollers — do not have to provide main at all. The entry symbol is whatever the linker script says. The hosted rules above apply only when you are writing a program that runs under an operating system.
Code Examples
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "usage: %s <name>\n", argv[0]);
return EXIT_FAILURE;
}
printf("hello, %s\n", argv[1]);
return EXIT_SUCCESS;
}#include <stdio.h>
/* Walking argv without using argc. argv[argc] is defined to be NULL. */
int main(int argc, char *argv[])
{
(void)argc;
for (char **p = argv; *p != NULL; p++) {
printf("%s\n", *p);
}
return 0; /* falling off main would also return 0 */
}Common Gotchas
`argv[0]` can be empty or unhelpful
the caller may pass an empty string, or a path you did not expect. Use it for usage text, not as a security boundary.
`void main` is not standard C
hosted programs return `int`. Compilers that accept `void main` are granting an extension, and the exit status the OS sees is then unspecified.
Falling off `main` returns 0; falling off any other non-void function is undefined behavior
the special case is only for `main`. Everywhere else, return a value.
Recursing into `main` confuses every reader and many tools
the standard permits it. Static analyzers, coverage tools, and humans all assume `main` runs once.