C Basics · beginner · ~5 min

The main function

Know the entry point and what the return value of main means.

Lesson

Every C program starts at a single function named main. The runtime calls it for you. The return value of main becomes the process exit status — 0 means success, anything else signals a failure to the parent process (shell, init, your CI).

The two standard signatures are int main(void) (no command-line arguments) and int main(int argc, char **argv) (where argc is the count and argv is the array, with argv[0] being the program name).

Code examples

#include <stdio.h>
int main(int argc, char **argv) {
    printf("got %d args; first one is %s\n", argc, argv[0]);
    return 0;
}

Common mistakes

  • Writing void main(void) — this is non-standard. Always return int.
  • Forgetting return 0; at the end (modern C lets you, but be explicit).