Introduction to C
Your first program, step by stepC is a small, fast language that still runs operating systems, phones and microcontrollers. A C program is plain text that a compiler turns into a program the computer can run. In the lab below you run programs one line at a time and watch the output appear — then see how hello.c is built.
- Created1972, by Dennis Ritchie
- Compiledsource code → program
- Starts inthe function main()
\n and \t.
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The smallest complete C program
Every C program has the same frame: #include lines that bring in ready-made tools, a function called main where the program starts, statements that end with a semicolon, and return 0 to tell the operating system "finished, no errors". The lab above runs exactly this program.
#include <stdio.h> // tools for input / output
int main(void) // the program starts here
{
printf("Hello, World!\n");
return 0; // 0 = success
}
The parts of a C program
- #include <stdio.h> adds the Standard Input/Output library, which contains printf.
- int main(void) is the function where every C program begins.
- The { and } braces hold the statements of main.
- Each statement ends with a semicolon ;
- return 0; ends main and reports success to the operating system.
From code to program
- You write the source code in a file, e.g. hello.c.
- The preprocessor handles # lines: it copies in stdio.h and removes comments.
- The compiler checks the code and translates it into assembly. Mistakes stop it here.
- The assembler turns the assembly into machine code (an object file, hello.o).
- The linker adds library code such as printf and creates the program (hello.exe / a.out).
How the program runs
- The operating system loads the program into memory.
- Execution starts at the first statement inside main.
- Statements run one after another, top to bottom.
- printf sends text to the screen; \n moves to a new line.
- return 0 ends the program; 0 means everything went well.
Read a C program line by line
Every line, explained.
/* My first program: greet the user */
A comment. Anything between /* and */ (or after //) is for people — the compiler ignores it.
#include <stdio.h>
A preprocessor directive: copy in the declarations of the standard input/output functions, such as printf.
int main(void)
The main function. int means it returns a whole number; void means it takes no input values.
{
The body of main starts here.
printf("Welcome to C!\n");
Print the text between the quotes. \n is an escape sequence: it starts a new line.
printf("Year: %d\n", 1972);
%d is a placeholder for a whole number; printf replaces it with 1972, the year C was created.
return 0;
End main and report success (0) to the operating system.
}
The body of main ends here.
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Forgetting the semicolon
printf("Hi")
printf("Hi");
Every statement must end with ; — without it the compiler stops with "expected ';'".
Missing #include <stdio.h>
int main(void) {
printf("Hi\n");
}
#include <stdio.h>
int main(void) {
printf("Hi\n");
}
Without it the compiler does not know printf: you get an "implicit declaration" warning or error.
Capital letters in names
Printf("Hi\n");
printf("Hi\n");
C is case-sensitive: Printf, PRINTF and printf are three different names. Only printf exists.
Single quotes around text
printf('Hello');
printf("Hello");
Text (a string) uses double quotes. Single quotes are for ONE character, like 'A'.
Forgetting \n
printf("Hello");
printf("World");
printf("Hello\n");
printf("World\n");
printf does not add a new line by itself — the first version prints HelloWorld on one line.
Check your understanding
Answer each question — you will see the explanation straight away.
Where does every C program start running?
Execution always starts at the first statement inside main, no matter where main is in the file.
What does #include <stdio.h> do?
It is a preprocessor directive: before compiling, the contents of stdio.h are copied in so the compiler knows printf.
What does printf("A\nB"); print?
\n is the newline escape sequence, so B appears at the start of the next line.
Which program turns your C source code into machine code?
The compiler (with the preprocessor, assembler and linker) translates C into a program the computer can run.
What does return 0; at the end of main mean?
main returns 0 to the operating system to say "finished, no errors". Other values usually mean an error.
Introduction to C quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.