Nearby lessons

6 of 72

C - 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:

In simple words: every C program has the same skeleton — #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.

Example01
CCode Cell
1#include <stdio.h> // header file - gives us printf, scanf
2 
3int main() // main() - every program starts here
4{
5 // 1. declaration part - tell C what variables you need
6 // 2. input part - take data from the user
7 // 3. processing part - do the calculations
8 // 4. output part - show the result
9 
10 return 0; // 0 means "finished successfully"
11}

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.

Common Mistake: forgetting the semicolon after 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.
Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 printf("Hello, welcome to C!\n");
6 return 0;
7}
Output
Hello, welcome to C!

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.

In simple words: %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.
Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a = 10; // declaration + initialization
6 int b = 20;
7 int sum;
8 
9 sum = a + b; // processing
10 
11 printf("Sum = %d\n", sum); // output
12 return 0;
13}
Output
Sum = 30
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5