Every C program starts running at main(). Its return value tells the operating system whether the program succeeded, and its parameters can receive command-line arguments.
Where Every C Program Begins
What is the main Function?
The main function is a user-defined function that serves as the starting point of program execution. When you run a C program, the operating system calls main, and execution begins from the first statement inside it. When main returns, the program terminates.
Example:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Expected output:
Hello, World!
Why is main Special?
- Entry point: The C runtime (startup code) calls
mainafter initializing the environment. - Required: Every C program must have exactly one
mainfunction (for hosted environments). - Return value: Indicates success (0) or failure (non-zero) to the operating system.
- Command-line arguments: Can accept arguments from the shell.
What Happens Without main?
If a program has no main, the linker fails with an error:
#include <stdio.h>
int func(void) {
printf("Hello, World!\n");
return 0;
}
Linker error:
/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status
Standard Forms of main
The C standard (C99/C11) specifies two standard forms:
int main(void)— No argumentsint main(int argc, char *argv[])— With command-line arguments
Both return int. Other forms (e.g., void main()) are non-standard and should be avoided.
Valid Forms of main()
Standard Syntax
Without Command-Line Arguments
int main(void) {
// Code
return 0;
}
With Command-Line Arguments
int main(int argc, char *argv[]) {
// Code
return 0;
}
Parameters:
argc(ARGument Count): Number of command-line arguments (including program name).argv(ARGument Vector): Array of strings containing the arguments.
Common Variations
| Form | Standard? | Description |
|---|---|---|
int main(void) | Yes | No arguments, returns int |
int main(int argc, char *argv[]) | Yes | With command-line arguments |
int main() | Yes (but less clear) | No arguments specified |
void main() | No | Non-standard, avoid |
main() | Yes (implicit int, C89) | Old style, avoid in C99+ |
Return Values
return 0;— Indicates successful execution.return EXIT_SUCCESS;— Macro for success (usually 0).return EXIT_FAILURE;— Macro for failure (usually non-zero).- Non-zero values — Indicate errors; can be used to communicate specific error codes.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// Successful execution
return EXIT_SUCCESS; // Usually 0
}
Command-Line Arguments
Command-line arguments allow users to pass data to the program at startup.
Example:
#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;
}
Compilation and execution:
gcc -std=c11 program.c -o program ./program hello world 42
Expected output:
Number of arguments: 4 argv[0] = ./program argv[1] = hello argv[2] = world argv[3] = 42
Important:
argv[0]is always the program name.- Arguments are always strings (even numbers).
- Convert strings to numbers using
atoi,strtol,strtod, etc.
Environment Variables (Extension)
Some compilers support a third parameter for environment variables:
int main(int argc, char *argv[], char *envp[]);
This is non-standard and should be used with caution. Use getenv() from <stdlib.h> instead.
Reading Command-Line Arguments
This program demonstrates the main function with command-line arguments in a realistic scenario: a simple calculator that accepts operation and operands from the command line.
Program: Command-Line Calculator
/**
* cli_calculator.c
* Demonstrates main function with command-line arguments.
* Usage: ./calculator <number1> <operator> <number2>
* Example: ./calculator 10 + 5
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* Function prototypes */
int parseDouble(const char *str, double *outValue);
int performOperation(double a, char op, double b, double *result);
int main(int argc, char *argv[]) {
/* Check command-line arguments */
if (argc != 4) {
fprintf(stderr, "Usage: %s <number1> <operator> <number2>\n", argv[0]);
fprintf(stderr, "Example: %s 10 + 5\n", argv[0]);
fprintf(stderr, "Operators: +, -, *, /\n");
return EXIT_FAILURE;
}
/* Parse numbers from command-line arguments */
double num1, num2;
if (parseDouble(argv[1], &num1) != 0) {
fprintf(stderr, "Error: '%s' is not a valid number.\n", argv[1]);
return EXIT_FAILURE;
}
if (parseDouble(argv[3], &num2) != 0) {
fprintf(stderr, "Error: '%s' is not a valid number.\n", argv[3]);
return EXIT_FAILURE;
}
/* Get operator */
char operator = argv[2][0];
/* Validate operator */
if (strlen(argv[2]) != 1 ||
(operator != '+' && operator != '-' &&
operator != '*' && operator != '/')) {
fprintf(stderr, "Error: Invalid operator '%s'.\n", argv[2]);
fprintf(stderr, "Valid operators: +, -, *, /\n");
return EXIT_FAILURE;
}
/* Perform calculation */
double result;
if (performOperation(num1, operator, num2, &result) != 0) {
return EXIT_FAILURE;
}
/* Display result */
printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
return EXIT_SUCCESS;
}
/* Parses a string to double with error checking */
int parseDouble(const char *str, double *outValue) {
char *endPtr;
errno = 0;
double val = strtod(str, &endPtr);
/* Check for conversion errors */
if (endPtr == str) {
return -1; /* No digits found */
}
if (*endPtr != '\0') {
return -1; /* Extra characters after number */
}
if (errno == ERANGE) {
return -1; /* Out of range */
}
*outValue = val;
return 0;
}
/* Performs arithmetic operation */
int performOperation(double a, char op, double b, double *result) {
switch (op) {
case '+':
*result = a + b;
return 0;
case '-':
*result = a - b;
return 0;
case '*':
*result = a * b;
return 0;
case '/':
if (b == 0.0) {
fprintf(stderr, "Error: Division by zero.\n");
return -1;
}
*result = a / b;
return 0;
default:
fprintf(stderr, "Error: Unknown operator '%c'.\n", op);
return -1;
}
}
Explanation
argcvalidation: Checks that exactly 4 arguments are provided (program name + 2 numbers + operator).argv[0]: Used to display the program name in usage messages.parseDouble: Safely converts string arguments todoubleusingstrtodwith full error checking.- Operator validation: Ensures the operator is a single valid character.
performOperation: Performs the arithmetic with division-by-zero check.- Return values: Returns
EXIT_SUCCESSon success,EXIT_FAILUREon errors.
Compilation and Execution
gcc -Wall -Wextra -std=c11 cli_calculator.c -o calculator
Successful execution:
./calculator 10 + 5
Output:
10.00 + 5.00 = 15.00
Invalid usage:
./calculator 10
Output:
Usage: ./calculator <number1> <operator> <number2> Example: ./calculator 10 + 5 Operators: +, -, *, /
Division by zero:
./calculator 10 / 0
Output:
Error: Division by zero.
Invalid number:
./calculator abc + 5
Output:
Error: 'abc' is not a valid number.
void main and Other Mistakes
1. Using void main() Instead of int main()
void main() is non-standard and may cause undefined behavior. The C standard requires main to return int.
void main() { // Non-standard
printf("Hello\n");
}
Solution: Always use int main(void) or int main(int argc, char *argv[]).
2. Forgetting to Return a Value from main
In C99 and later, if main reaches the end without a return statement, it implicitly returns 0. However, this is not the case for other functions.
int main(void) {
printf("Hello\n");
// Implicit return 0 in C99, but not in C89
}
Solution: Always explicitly return 0; or return EXIT_SUCCESS;.
3. Incorrect argc Handling
argc includes the program name. A program with no additional arguments has argc == 1.
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE;
}
// ...
}
Solution: Always check argc before accessing argv elements.
4. Accessing argv Out of Bounds
Accessing argv[i] where i >= argc is undefined behavior.
int main(int argc, char *argv[]) {
printf("%s\n", argv[1]); // BUG if argc < 2
return 0;
}
Solution: Always verify argc before accessing argv.
5. Command-Line Arguments are Strings
All command-line arguments are strings. Numeric arguments must be converted.
int main(int argc, char *argv[]) {
if (argc < 2) return EXIT_FAILURE;
int num = atoi(argv[1]); // Convert string to int
printf("%d\n", num);
return 0;
}
Solution: Use atoi, strtol, strtod for conversion, with error checking for robustness.
6. Not Handling Conversion Errors
atoi returns 0 on error, making it impossible to distinguish between “0” and invalid input.
int num = atoi("abc"); // Returns 0, but "abc" is invalid
Solution: Use strtol with error checking:
char *endPtr;
long val = strtol(argv[1], &endPtr, 10);
if (endPtr == argv[1] || *endPtr != '\0') {
fprintf(stderr, "Invalid number\n");
}
7. Using main() with Implicit Int (C89)
In C89, main() with no return type implicitly returns int. This was removed in C99.
main() { // Implicit int (C89 only)
return 0;
}
Solution: Always specify int main(void) explicitly.
8. Multiple main Functions
Having more than one main function in a program causes a link error.
/* file1.c */
int main(void) { return 0; }
/* file2.c */
int main(void) { return 0; } /* Link error: multiple definition of 'main' */
Solution: Ensure only one main exists across all source files.
9. main in a Library
Libraries should not contain a main function; otherwise, linking the library with a program that also has main causes conflicts.
Solution: Keep main only in the executable’s source file.
10. Environment Variables (Non-Standard)
Some compilers support int main(int argc, char *argv[], char *envp[]), but this is non-standard.
Solution: Use getenv() from <stdlib.h> for portability.
Memory Considerations
- The
mainfunction’s stack frame is the first (or one of the first) on the call stack. - Command-line arguments are stored in memory allocated by the operating system.
argvis an array of pointers to strings; it occupies memory, but the strings themselves are in the program’s environment.- Large local variables in
maincan consume significant stack space; consider usingstaticor heap allocation for large buffers.
main() Summary
The main function is the entry point of every C program. It must return int and can optionally accept command-line arguments via argc and argv.
Key Takeaways
mainis where program execution begins.- Standard forms:
int main(void)andint main(int argc, char *argv[]). - Return
0orEXIT_SUCCESSfor success; non-zero for failure. argcincludes the program name;argv[0]is the program name.- Command-line arguments are strings; convert to numbers using
strtol,strtod. - Avoid
void main(); it’s non-standard. - Always check
argcbefore accessingargv.
Best Practices
- Use
int main(void)orint main(int argc, char *argv[]). - Always return a value from
main(0orEXIT_SUCCESSfor success). - Check
argcbefore accessing command-line arguments. - Use
strtol/strtodfor converting command-line arguments with error checking. - Display usage messages when arguments are missing or invalid.
- Use
EXIT_SUCCESSandEXIT_FAILUREfrom<stdlib.h>for portability. - Keep
mainshort; delegate logic to other functions. - Avoid large local variables in
main; usestaticor heap. - Use
argv[0]in usage messages for clarity. - Compile with
-Wall -Wextrato catch return type and argument mismatches.
By mastering the main function, you’ll write C programs that integrate correctly with the operating system and provide robust command-line interfaces.
