Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Functions and Scope

main() function in C

Reference for the C main() function: valid signatures, argc and argv, the return value as exit status, EXIT_SUCCESS and EXIT_FAILURE, and what happens at exit.

Every hosted C program starts running in a function named main. The runtime calls it after setting up the standard streams and static data. When main returns, the program ends, and the returned value becomes the exit status that the shell or parent process can inspect.

There are two standard signatures: one with no parameters, and one that receives the command-line arguments. An implementation may accept others, but only these two are portable.

C
int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }

Try it

C Inspecting argc and argv

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Exit status and implicit return

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int main(void);
int main(int argc, char *argv[]);   /* or char **argv */
int main();                         /* C23: same as main(void) */
Name Description
argc Number of entries in argv, not counting the final null pointer. If it is greater than zero, argv[0] represents the program name (possibly empty).
argv Array of argc + 1 pointers to modifiable strings; argv[argc] is NULL. The strings and the array last for the whole program.
return value An int status. 0 and EXIT_SUCCESS report success; EXIT_FAILURE reports failure. Other values are implementation-defined (on POSIX, 0 to 255 are meaningful).

Behavior: returning from main is equivalent to calling exit with that value. Reaching the closing } of main returns 0 (this special rule exists only for main). The parameter names are free: ac and av work too.

Errors / undefined behavior: modifying argv strings is allowed, but writing beyond their length is not. Calling main recursively is legal in C but rarely wise. Declaring main with another return type, such as void main, is non-portable (GCC accepts it in freestanding mode and warns otherwise).

Usage notes

  • Parse arguments by hand, or with getopt on POSIX systems. Convert numbers with strtol or strtod, which report errors, rather than atoi.
  • Exit codes in scripts. Shells treat 0 as success. Return distinct non-zero codes for distinct failures, and print error messages to stderr, not stdout.
  • exit() vs return. exit can end the program from any function and runs atexit handlers and flushes streams. _Exit and abort skip some or all of that.
  • Stack variables of main are destroyed at return, but static ones stay until the handlers run. Output buffered in stdout is flushed during exit.
  • Environment. POSIX adds a non-standard third parameter char *envp[]; use getenv instead for portability.
  • Wide systems. Windows has wmain and wchar_t arguments; they are not standard C.
  • argv[0] is not a reliable path to the executable; do not use it for security decisions.
  • Freestanding programs (kernels, microcontrollers) may name the entry point differently; this page describes hosted environments.
  • C23: int main() with empty parentheses now means no parameters, like main(void).