Watch your code run Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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

  1. #include <stdio.h> adds the Standard Input/Output library, which contains printf.
  2. int main(void) is the function where every C program begins.
  3. The { and } braces hold the statements of main.
  4. Each statement ends with a semicolon ;
  5. return 0; ends main and reports success to the operating system.

From code to program

  1. You write the source code in a file, e.g. hello.c.
  2. The preprocessor handles # lines: it copies in stdio.h and removes comments.
  3. The compiler checks the code and translates it into assembly. Mistakes stop it here.
  4. The assembler turns the assembly into machine code (an object file, hello.o).
  5. The linker adds library code such as printf and creates the program (hello.exe / a.out).

How the program runs

  1. The operating system loads the program into memory.
  2. Execution starts at the first statement inside main.
  3. Statements run one after another, top to bottom.
  4. printf sends text to the screen; \n moves to a new line.
  5. return 0 ends the program; 0 means everything went well.
Walkthrough

Read a C program line by line

Every line, explained.

1 /* My first program: greet the user */ A comment. Anything between /* and */ (or after //) is for people — the compiler ignores it.
2 #include <stdio.h> A preprocessor directive: copy in the declarations of the standard input/output functions, such as printf.
3
4 int main(void) The main function. int means it returns a whole number; void means it takes no input values.
5 { The body of main starts here.
6 printf("Welcome to C!\n"); Print the text between the quotes. \n is an escape sequence: it starts a new line.
7 printf("Year: %d\n", 1972); %d is a placeholder for a whole number; printf replaces it with 1972, the year C was created.
8 return 0; End main and report success (0) to the operating system.
9 } The body of main ends here.
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Forgetting the semicolon

✗ Wrong
printf("Hi")
✓ Right
printf("Hi");

Every statement must end with ; — without it the compiler stops with "expected ';'".

Missing #include <stdio.h>

✗ Wrong
int main(void) {
    printf("Hi\n");
}
✓ Right
#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

✗ Wrong
Printf("Hi\n");
✓ Right
printf("Hi\n");

C is case-sensitive: Printf, PRINTF and printf are three different names. Only printf exists.

Single quotes around text

✗ Wrong
printf('Hello');
✓ Right
printf("Hello");

Text (a string) uses double quotes. Single quotes are for ONE character, like 'A'.

Forgetting \n

✗ Wrong
printf("Hello");
printf("World");
✓ Right
printf("Hello\n");
printf("World\n");

printf does not add a new line by itself — the first version prints HelloWorld on one line.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

Where does every C program start running?

Question 2

What does #include <stdio.h> do?

Question 3

What does printf("A\nB"); print?

Question 4

Which program turns your C source code into machine code?

Question 5

What does return 0; at the end of main mean?

Assessment

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.