Skip to content
KALANBLON
Show contents

Chapter 2 of 4 · 15 min

Anatomy of a C program

By the end of this chapter, you'll be able to

  • Explain the role of #include, main and return 0
  • Display text over several lines with printf and \n
  • Comment your code
  • Spot a missing semicolon or curly brace

The skeleton, line by line

#include <stdio.h>    // 1. I use standard input/output

int main(void) {      // 2. the program starts here
    printf("Hi!\n");  // 3. an instruction, ending with ;
    return 0;         // 4. everything went well
}                     // 5. end of main
Hi!
  • #include <stdio.h> includes the header file of the standard input/output library. A header file introduces a library's functions to the compiler: this one declares printf. Without this line, the compiler doesn't know printf.
  • int main(void) declares the main function. Every C program starts by running main. void means it receives nothing, and int that it returns a whole number.
  • The curly braces { and } mark out the function's block: everything between them belongs to main.
  • Each instruction ends with a semicolon ;. It's C's punctuation.
  • return 0; ends main and returns 0 to the operating system, which means "everything went well". Any other value signals an error.
  • Everything after // on a line is a comment: the compiler ignores it. For a comment over several lines, you write between /* and */.

Indentation (the spaces at the start of a line) isn't mandatory in C, unlike in Python. But everyone uses it, because it makes the structure visible at a glance.

printf and special characters

printf displays the text between quotes exactly as it is, without adding a line break. To go to a new line, you write \n ("new line"). It's an escape sequence: a backslash followed by a character, to represent a character you can't type directly.

#include <stdio.h>

int main(void) {
    printf("Line 1\nLine 2\n");
    printf("A tab:\there\n");
    printf("Quotes: \"C\" and a backslash: \\\n");
    return 0;
}
Line 1
Line 2
A tab:	here
Quotes: "C" and a backslash: \

return 0 ends main and tells the system that everything went well.

Step 6 of 6
Text description of the animation
  1. The program has been compiled. When it's launched, execution starts in main.
  2. Line 3: we enter main. The lines will run in order.
  3. printf writes "Hello" followed by a space. No \n: the cursor stays on the same line.
  4. The next text sticks right behind it, then \n moves to a new line.
  5. "The end" is written on the new line.
  6. return 0 ends main and tells the system that everything went well.

Your turn

Worked exercise

Worked exercise: a receipt

Write a program that displays exactly this receipt, with a tab (\t) between the item and the price:

--- Central market ---
Mangoes	1500 FCFA
Rice	2500 FCFA
Thank you!
Hint 1
One printf per line is enough. Each text ends with \n.
Hint 2
The tab is written \t inside the quotes: "Mangoes\t1500 FCFA\n".
Show the solution and common mistakes
#include <stdio.h>

int main(void) {
    printf("--- Central market ---\n");
    printf("Mangoes\t1500 FCFA\n");
    printf("Rice\t2500 FCFA\n");
    printf("Thank you!\n");
    return 0;
}
--- Central market ---
Mangoes	1500 FCFA
Rice	2500 FCFA
Thank you!

Common mistakes:

  • Writing /n instead of \n: it has to be a backslash.
  • Forgetting #include <stdio.h>: GCC reports an implicit declaration of printf.
  • Typing real tabs in the text: it works, but \t is more readable and doesn't disappear when your editor converts tabs into spaces.

Summary

  1. 1#include <stdio.h> gives access to printf.
  2. 2The program starts in main; return 0 signals that everything went well.
  3. 3Each instruction ends with ; and curly braces mark out blocks.
  4. 4printf doesn't add a line break: write \n.
  5. 5Comments (// or /* */) are ignored by the compiler.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.