0
0
Cprogramming~10 mins

Structure of a C program - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Structure of a C program
Start
Include Headers
Define main() Function
Declare Variables
Write Statements
Return from main
End
This flow shows the basic parts of a C program from start to finish.
Execution Sample
C
#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}
This simple C program prints 'Hello, world!' and ends.
Execution Table
StepCode LineActionOutput/Result
1#include <stdio.h>Include standard input/output libraryNo output
2int main() {Start main functionNo output
3printf("Hello, world!\n");Print text to screenHello, world!
4return 0;Return 0 to OS (program success)No output
5}End main functionProgram ends
💡 Program ends after main function returns 0
Variable Tracker
VariableStartAfter Step 3Final
No variablesN/AN/AN/A
Key Moments - 3 Insights
Why do we need #include <stdio.h> at the top?
It tells the program to use the standard input/output library so we can use printf. See step 1 in the execution table.
What does return 0; mean in main()?
It tells the operating system the program finished successfully. See step 4 in the execution table.
Why is main() needed in every C program?
main() is the starting point where the program begins running. Without it, the program won't run. See step 2 in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does step 3 do?
AEnds the program
BPrints 'Hello, world!' to the screen
CIncludes the stdio library
DReturns 0 to the OS
💡 Hint
Check the 'Action' and 'Output/Result' columns at step 3 in the execution table.
At which step does the program tell the OS it finished successfully?
AStep 4
BStep 2
CStep 1
DStep 5
💡 Hint
Look for 'return 0;' in the code line column in the execution table.
If we remove #include <stdio.h>, what will happen at step 3?
AThe program will print 'Hello, world!' anyway
BThe program will return 0 immediately
CThe program will fail to compile because printf is undefined
DThe program will run but print nothing
💡 Hint
Recall why #include is needed from the key moments section.
Concept Snapshot
#include <stdio.h>  // Include library for input/output
int main() {          // Main function starts
    printf("Hello, world!\n");  // Print text
    return 0;         // End program successfully
}                     // End of main
Full Transcript
A C program starts by including libraries like stdio.h for input/output. The main function is where the program begins running. Inside main, we write statements like printf to show text on the screen. The program ends by returning 0 to tell the operating system it finished successfully.