wiki:c-main-return
Table of Contents
C main() return
main() return value is the program's C Exit status, communicated to the shell via the $? variable. Returning 0 indicates success (EXIT_SUCCESS); non-zero indicates failure (EXIT_FAILURE, typically 1). The shell can check this status with echo $? or use it in conditionals and pipelines.
Always explicitly return EXIT_SUCCESS or EXIT_FAILURE from main to make intent clear.
Example
This example demonstrates returning different exit statuses based on program logic.
// compile: gcc -o mainreturn mainreturn.c // run: ./mainreturn 5; echo $? // description: return exit status from main to signal success or failure #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <number>\n", argv[0]); return EXIT_FAILURE; } int num = atoi(argv[1]); if (num > 0) { printf("Positive number: %d\n", num); return EXIT_SUCCESS; } else { fprintf(stderr, "Error: number must be positive\n"); return EXIT_FAILURE; } }
wiki/c-main-return.md · Last modified: by 127.0.0.1
