Nearby lessons
13 of 124C - Program Structure
Every C program — from a three-line greeting to an operating system kernel — follows the same skeleton: #include at the top, main() as the starting point, and the real work inside { }. This lesson builds that skeleton, runs your first program, explains why main() returns a value, and ends with a program that adds two numbers.
The Structure of a C Program
Every C program follows a simple structure. Learn this skeleton and every program fits into it:
#include <stdio.h> brings in ready-made tools, main() is where the program starts, and the real instructions live inside { }.
| Part | What it does |
|---|---|
#include <stdio.h> |
Brings in ready-made functions (printf, scanf) from the stdio header file. |
int main() |
The starting point of the program. Execution always begins here. |
{ } |
The body of main() — all the actual instructions live here. |
return 0; |
Tells the operating system the program finished successfully. |
; |
Every statement in C ends with a semicolon. |
The four parts inside main() are not compulsory, but they are the natural order of thinking.
Your First C Program
printf(...) is the function that prints text on the screen. The \n inside the quotes means move to a new line.
printf("Hello");. The compiler reports the error on the next line, which is confusing the first time you see it. If an error makes no sense, check the line above it.
int main() vs void main()
You will see two forms of main() in books and on the internet. Both usually compile, but only one is standard C:
int main() |
void main() |
|
|---|---|---|
| Standard C? | Yes — this is the correct form | No — not in the C standard |
| Returns a value | return 0; — success |
Returns nothing |
| Compilers | Accepted everywhere | Usually accepted, often with a warning |
| Where you see it | Modern code, real projects | Older textbooks, some college notes |
The returned number is an exit status: the operating system reads it to find out whether your program succeeded. 0 means success, and any non-zero value means something went wrong. Scripts and build tools rely on this.
void main(), your programs will still run — do not panic. But write int main() with return 0; yourself. It is the standard, it is what interviewers expect, and it is the only form guaranteed to compile everywhere.
A Slightly Bigger Example — Adding Two Numbers
Notice %d — it is a format specifier that tells printf where to print the integer sum. We study format specifiers properly in the printf lesson.
%d is a placeholder. printf sees %d, goes to the value sum, and prints it exactly in that place. Change the value, and the same printf line prints the new number.
- Program structure: #include <stdio.h> → main() → body in { }.
- main() is where execution always begins.
- Every statement ends with a semicolon.
- printf() prints output; %d is the format specifier for integers.
- int main() is standard C; return 0; reports success to the operating system.
- The four parts of main(): declaration, input, processing, output.