Skip to content
  • Program Structure & main · C

Program Structure & main

LearnViaNotes @learnvianotes· 2 min read

A C program is a set of declarations and function definitions; execution starts at main, and its return value becomes the process exit status.

Anatomy of a program

C
/* 1. Preprocessor directives */
#include <stdio.h>
#define GREETING "Hi"

/* 2. Global declarations */
static int counter = 0;

/* 3. Function prototype (declaration) */
int square(int x);

/* 4. main: the entry point */
int main(void) {
    counter++;
    printf("%s! square(7) = %d, counter = %d\n", GREETING, square(7), counter);
    return 0;                /* 0 = success */
}

/* 5. Function definitions */
int square(int x) {
    return x * x;
}
Output
Hi! square(7) = 49, counter = 1

Valid signatures of main

SignatureUse
int main(void)No command-line arguments.
int main(int argc, char *argv[])With arguments (char **argv is identical).
void main()Not standard C. Some compilers accept it; never write it.
  • return 0; or return EXIT_SUCCESS; means success; EXIT_FAILURE (from <stdlib.h>) means failure.

  • Reaching the closing } of main without return is treated as return 0; (C99+) — but only for main.

  • int main() with empty parentheses means unspecified arguments in C17; prefer (void).

Comments

C
#include <stdio.h>

int main(void) {
    /* block comment — can span
       several lines, but cannot be nested */
    int x = 5;   // line comment (C99+)
    printf("%d\n", x);
    return 0;
}
Output
5

Declarations vs definitions

What happens before main runs?

What does the return value of main mean?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report