skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
c-exit-status
This page is read only. You can view the source, but not change it.
# C exit status **[Exit status](https://en.wikipedia.org/wiki/Exit_status)** is an integer code (0-255) returned by a program to indicate success or failure. A return value of 0 means success (`EXIT_SUCCESS`); any non-zero value indicates failure. The shell stores the exit status in `$?` for inspection with `echo $?`, and it can be used in shell conditionals and pipelines. By convention, use 0 for success and 1 for generic failure; reserve other values for specific error conditions. ## Example This example demonstrates checking and using exit status in a C program and shell. ```c // compile: gcc -o exitstat exitstat.c // run: ./exitstat file.txt; echo $? // description: return and check exit status to signal program success/failure #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <filename>\n", argv[0]); return EXIT_FAILURE; } FILE *f = fopen(argv[1], "r"); if (!f) { perror("fopen"); return EXIT_FAILURE; } char buf[256]; while (fgets(buf, sizeof(buf), f)) { printf("%s", buf); } fclose(f); return EXIT_SUCCESS; } ```
Page Tools
Show pagesource
Backlinks
Back to top