Nearby lessons

13 of 124

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!

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.

Trainer's Note: If your textbook or your college notes use 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.
Example03
CCode Cell
1#include <stdio.h>
2 
3int main() // standard form - use this one
4{
5 printf("Standard C\n");
6 return 0; // 0 = success
7}
8 
9/* The older form you may see in textbooks:
10 
11 void main()
12 {
13 printf("Older style\n");
14 } // no return value
15*/
Output
Standard C

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.

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.
Example04
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