Nearby lessons
6 of 72C - 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.
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.