The main Function Since: C89(1989)
Every C program begins execution from the main function. The main function is the entry point of a program, and every C program must have exactly one.
Syntax
// When no arguments are needed.
int main(void) {
// Program logic
return 0;
}
// When receiving command-line arguments.
int main(int argc, char *argv[]) {
// argc: number of arguments (including the program name)
// argv: array of argument strings
return 0;
}
Parameters
| Parameter | Type | Description |
|---|---|---|
| argc | int | The number of command-line arguments. Because the program name itself is included, this value is at least 1 even when no arguments are passed. |
| argv | char *[] | An array of command-line argument strings. argv[0] is the program name, and argv[1] onward are the arguments provided by the user. |
Return Values
| Value | Description |
|---|---|
| 0 | Indicates successful termination. By convention, 0 means success. |
| Non-zero | Indicates abnormal termination. The specific value can be freely defined by the program. |
| EXIT_SUCCESS | A macro defined in stdlib.h for successful termination (typically 0). |
| EXIT_FAILURE | A macro defined in stdlib.h for failure termination (typically 1). |
The return value of the main function is passed to the OS as the program's exit status. You can check it with echo $? (Unix-like systems) or echo %ERRORLEVEL% (Windows).
Sample Code
Basic main Function
hello.c
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
gcc hello.c -o hello ./hello Hello, World!
Receiving Command-Line Arguments
greet.c
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s name\n", argv[0]);
return 1; // Exit with error due to missing arguments.
}
printf("Hello, %s!\n", argv[1]);
return 0;
}
gcc greet.c -o greet ./greet Usage: ./greet name ./greet Alice Hello, Alice!
Processing Multiple Arguments
args.c
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
gcc args.c -o args ./args foo bar baz Number of arguments: 4 argv[0] = ./args argv[1] = foo argv[2] = bar argv[3] = baz
Using Exit Codes
exitcode.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s num1 num2\n", argv[0]);
return EXIT_FAILURE; // Abnormal exit.
}
int a = atoi(argv[1]);
int b = atoi(argv[2]);
printf("%d + %d = %d\n", a, b, a + b);
return EXIT_SUCCESS; // Successful exit.
}
gcc exitcode.c -o exitcode ./exitcode 10 20 10 + 20 = 30 echo $? 0
Overview
The main function is the entry point of a C program. No matter how many functions you define, execution always starts from the main function. When the main function terminates (reaches a return statement), the entire program ends.
Use int main(void) when you do not need arguments, and int main(int argc, char *argv[]) when you need to process command-line arguments. While void main() is sometimes seen, the C standard (C99 and later) specifies that main must return int, so always use int main.
The return value of main is communicated to the OS as the program's exit code. It is used by shell scripts and other programs to determine success or failure, so return 0 (or EXIT_SUCCESS) on success and a non-zero value (or EXIT_FAILURE) on failure.
If you find any errors or copyright issues, please contact us.