SyntaxPilot
Courses/C Programming/Lesson 1

C Programming · Lesson 1 of 30 · 11 min

Compiler, printf & first program

published

These are the notes for C episode 1. You will learn what C is and where it runs (operating systems, washing machines, cars), what a compiler does (gcc, main.c to a.out), your first C program line by line (#include, main, printf, return 0), printf with \n, escape sequences and %d, how to read gcc errors, and comments. Every example below runs as-is in the SyntaxPilot compiler (choose C as the language).

The problem: the computer doesn't speak our language

A computer only understands machine code: zeros and ones. We write programs in a human-readable language. So how does our code run?

Think of a book written in English for a reader who only knows Japanese. A translator translates the whole book in advance, and the reader reads the translated book directly.

Book C
You write the book in your language You write main.c
The translator translates the whole book gcc, the compiler, translates the whole program
A translated book a file of machine code (a.out by default on Linux)
The reader reads it directly the CPU runs it directly, with no translator in between: very fast

What is C?

  • Created by Dennis Ritchie at Bell Labs in 1972. More than fifty years later it is still everywhere.
  • Fast, because it works close to the hardware.
  • Operating systems: Linux and many parts of Windows (and macOS) are written in C.
  • Embedded programming: the small chips inside washing machines (wash, rinse, spin), microwaves and cars (fuel, brakes, airbags) often run C, because speed and reliability matter most there.
  • C++, Java and Python were all influenced by C, which is why C is called the mother of programming languages. Once you learn C, other languages feel easier, and you understand how a computer really works inside.

What a compiler does: from main.c to output

main.c  --gcc-->  a.out (machine code)  --CPU-->  output on screen

Three points for your notebook:

  1. gcc checks all the code first. A mistake means a compile error, and the program is not built at all.
  2. The machine code is made for one specific operating system and CPU.
  3. So to move from Windows to Linux, you compile again. (Java solves this with the JVM; C does not have one.)

In a terminal these are two commands:

gcc main.c
./a.out

On SyntaxPilot, one Run button (or Ctrl + Enter) does both. gcc main.c -o hello names the output hello instead of a.out.

The SyntaxPilot compiler

  • Select C in the language dropdown. Nothing to install.
  • Left: the editor; the file is main.c.
  • Right: the terminal, where output appears; the command at the top shows gcc main.c && ./a.out.
  • Press Run or Ctrl + Enter.

Your first C program

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Output:

Hello, World!

What each line means

Line Meaning Real life
#include <stdio.h> Brings in the standard input output library, where printf comes from borrowing a ready-made book from the library
int main() { The program's entry point: every C program starts here the main door
printf("Hello, World!\n"); Prints text on the screen. The f stands for formatted making an announcement
return 0; Tells the operating system the program finished fine. 0 means success a delivery person saying "Delivered!"
} Closes main the walls of the room

Rules to remember

  • Every statement ends with a semicolon ;.
  • { } mark where main starts and ends.
  • C is case sensitive: printf and Printf are different things.
  • printf does not add a new line by itself: use \n.

printf and \n

Two printf calls without \n:

#include <stdio.h>

int main() {
    printf("Hello ");
    printf("C");
    return 0;
}

Output:

Hello C

Both ended up on one line. Now with \n:

#include <stdio.h>

int main() {
    printf("Hello\n");
    printf("C\n");
    printf("Line three\n");
    return 0;
}

Output:

Hello
C
Line three

\n is like pressing Enter on the keyboard. Get into the habit of ending the last line of output with \n too.

Escape sequences

Special characters inside a string start with a backslash:

Escape Meaning
\n new line
\t tab: adds space to line up columns
\" a double quote inside a string
\\ a backslash itself
%% a percent sign inside printf (because % starts a format code)
#include <stdio.h>

int main() {
    printf("Line 1\nLine 2\n");
    printf("Name:\tAarav\n");
    printf("He said \"Hi\"\n");
    printf("C:\\Users\\C\n");
    return 0;
}

Output:

Line 1
Line 2
Name:   Aarav
He said "Hi"
C:\Users\C

printf with numbers: %d

printf works like filling in a form. The form says Age: ____; in printf, that blank is %d. After a comma you give the value, and it fills the blank.

#include <stdio.h>

int main() {
    printf("Age: %d\n", 21);
    printf("5 + 3 = %d\n", 5 + 3);
    printf("Marks: %d out of %d\n", 87, 100);
    return 0;
}

Output:

Age: 21
5 + 3 = 8
Marks: 87 out of 100
  • Each %d is replaced by its value.
  • 5 + 3 is calculated first, then 8 fills the blank. (In C there is no Java-style + trap, because text and numbers are never glued with +.)
  • Two blanks, two values, filled in order.
  • %d means decimal integer (a whole number). Other format codes like %f and %c come with data types in the next episode.

Mismatches are dangerous, and gcc only warns:

printf("%d %d\n", 5);        // one value missing
printf("Price: %d\n", 9.5);  // a decimal given to %d
main.c:4:17: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
main.c:5:21: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

The program still runs, but prints garbage values that change from run to run. Always match each %d with exactly one whole-number value.

Reading compiler errors

A compile error is like a teacher checking homework: gcc checks every line, marks the mistake with the line number, and the program is built only after you fix it.

Missing semicolon

#include <stdio.h>

int main() {
    printf("Hello, World!\n")
    return 0;
}
main.c:4:30: error: expected ‘;’ before ‘return’
    4 |     printf("Hello, World!\n")
      |                              ^
      |                              ;

Read it as file : line : column : problem. The semicolon belonged at the end of line 4, and gcc noticed when it reached return on the next line, so it even shows where the ; should go.

Forgetting #include

int main() {
    printf("Hi\n");
    return 0;
}
main.c:2:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
main.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’

gcc does not know where printf comes from, and even suggests the fix. With gcc 13 this is a warning (the program still runs); newer compilers (gcc 14 and later) treat it as an error, so always include the header.

Printf with a capital P

Printf("Hi\n");
main.c:4:5: warning: implicit declaration of function ‘Printf’; did you mean ‘printf’? [-Wimplicit-function-declaration]
main.c:(.text+0x18): undefined reference to `Printf'
collect2: error: ld returned 1 exit status

C is case sensitive. The final error comes from the linker (ld), the step after compiling that connects your code to library functions: there is no function called Printf.

Common mistakes (and the real error messages)

Mistake Code Error
Missing semicolon printf("Hi\n") error: expected ‘;’ before ‘return’
No #include <stdio.h> printf("Hi\n"); alone warning: implicit declaration of function ‘printf’
Capital P Printf("Hi\n"); undefined reference to `Printf'
Main instead of main int Main() { undefined reference to `main'
Missing closing quote printf("Hi\n); error: missing terminating " character
Missing closing } main never closed error: expected declaration or statement at end of input
No \n printf("Hello"); printf("World"); no error, prints HelloWorld
%d without a value printf("%d %d\n", 5); warning: format ‘%d’ expects a matching ‘int’ argument, then a garbage value

Comments: sticky notes in your code

Comments are written for humans; the compiler ignores them.

#include <stdio.h>

/* This program prints
   a welcome message */
int main() {
    printf("Hi\n");   // prints Hi
    // printf("Not printed\n");
    return 0;
}

Output:

Hi
  • /* ... */ is a multi-line comment.
  • // starts a single-line comment. Putting // in front of a line stops it running, which is very handy for testing.
  • Good comments explain why.

Mini project: a profile card

#include <stdio.h>

int main() {
    // Mini project: profile card
    printf("==============================\n");
    printf("Name:\tAarav\n");
    printf("City:\tLucknow\n");
    printf("Age:\t%d\n", 21);
    printf("Goal:\tC Developer\n");
    printf("==============================\n");
    return 0;
}

Output:

==============================
Name:   Aarav
City:   Lucknow
Age:    21
Goal:   C Developer
==============================

Tabs line up the values, and %d fills in the age. Put in your own details and build your own card.

Practice

  1. Build your own profile card with at least one %d.
  2. Print a 3-row marks table using \t.
  3. Make each error from this lesson on purpose (missing ;, missing #include, Printf) and read the messages.
  4. Print 7 x 8 = 56 using printf and %d, letting C do the multiplication.
  5. Print this line exactly: Discount: 50% on "C Basics".
Solutions and hints
  1. Copy the mini project and change the values; for example printf("Age:\t%d\n", 19);.
  2. One printf per row with \t between the columns: printf("Subject\tMarks\n");, then printf("Maths\t90\n"); and so on.
  3. Expect expected ‘;’ before ‘return’, implicit declaration of function ‘printf’ and undefined reference to `Printf'.
  4. printf("7 x 8 = %d\n", 7 * 8); prints 7 x 8 = 56.
  5. printf("Discount: 50%% on \"C Basics\"\n");. Use %% for the percent sign and \" for the quotes.

Quick quiz

What will this code print?

printf("Hello");
printf("World");
  • A. Hello World
  • B. Hello and World on two lines
  • C. HelloWorld
  • D. Error
Answer

C. HelloWorld. printf never adds a space or a new line by itself, so the two words join together. For two lines, write printf("Hello\n");.

Interview questions

  • Who created C, and why is it still used today?
  • What is a compiler? What does gcc main.c produce?
  • Why does a C program have to be recompiled for a different operating system?
  • What does #include <stdio.h> do? What is a header file?
  • What does return 0; in main mean? (The program's exit status: 0 means success; the shell can read it.)
  • What is the difference between a compile error, a warning and a linker error?
  • What does %d mean in printf, and what happens if the arguments don't match?

Summary

  • C: created by Dennis Ritchie in 1972; fast, close to the hardware, used in operating systems and embedded devices.
  • gcc compiles main.c into machine code (a.out) that the CPU runs directly; a new OS means compiling again.
  • #include <stdio.h> brings in printf; int main() is where the program starts; return 0 means success.
  • printf needs \n for a new line; escape sequences \n, \t, \", \\.
  • %d is a blank for a whole number, filled in order.
  • Read errors as file : line : column : problem. Comments: // and /* */.

Next episode: variables and C's data types (int, float, double and char) and how much memory each one uses.