Skip to content

Hello, C

The Course
•
30 min study

A computer only follows instructions, and it follows them exactly. C is a language for writing those instructions down. It is small, fast and close to how the machine really works, which is why operating systems, databases and the interpreters of many other languages are written in it. That closeness is also why C asks more of you: it rarely guesses what you meant. This course teaches you to write C whose behavior you can predict, starting with no previous programming.

Every example on this site runs in your browser. Press Run to compile and execute it. The first run downloads a compiler (about 30 MB); later runs reuse the cache. Reset restores the example and Stop ends a run.

Predict and run

Read the program before pressing Run. Write down the exact output, line by line, and say which statement produces each line.

Predict before running C
#include <stdio.h>

int main(void) {
    printf("Hello, C!\n");
    printf("Day 1\n");
    return 0;
}
Check your prediction
Text
Hello, C!
Day 1

Each printf call printed one line, in the order the statements appear. Swap the two calls and run again: the lines swap too. A program runs from top to bottom unless you tell it otherwise.

Source, compilation and execution

Three different things happened when you pressed Run, and keeping them apart will save you hours of confusion.

  1. Source is the text you wrote. The computer cannot run it directly.
  2. Compilation checks the source against the rules of C and translates it into machine instructions. If the source breaks a rule, the compiler stops and reports a diagnostic. No program is produced, so nothing runs and nothing new is printed.
  3. Execution runs the translated program. Only now does printf print anything.

So a compile error and a wrong answer are different problems. A compile error means the compiler refused your text. A wrong answer means the compiler accepted it and the program did exactly what you wrote, which was not what you meant.

The frame every program needs

#include <stdio.h> makes the standard input and output declarations, including printf, available to your file. int main(void) is the function where every C program starts: void says it takes no arguments, and int says it reports a whole number back when it finishes. The braces hold its body. return 0; reports success to whatever started the program. For now, keep this frame and change only the statements inside it.

A semicolon ends each statement. Indentation helps people read the code, but the compiler ignores it: braces, not indentation, decide where a block begins and ends. A // comment is a note for readers and never executes.

Read a diagnostic

Here is the first program with one semicolon missing:

C
#include <stdio.h>

int main(void) {
    printf("Hello, C!\n")
    printf("Day 1\n");
    return 0;
}

The compiler reports something like this:

Shell
main.c:4:26: error: expected ';' after expression
    printf("Hello, C!\n")
                         ^
                         ;

The numbers are the file, line and column. Read the first diagnostic first, because later ones are often side effects of it. Notice that the program printed nothing at all, not even the first line, because it was never built. Paste this version into the playground above, read the message, then restore the semicolon.

A spelling mistake inside the quotes is different. printf("Helo\n"); compiles happily and prints Helo. The compiler checks the rules of the language, not the meaning of your messages.

Newlines are yours to print

printf prints exactly the characters you give it and nothing more. It does not start a new line on its own. The two characters \n inside the quotes form one escape sequence, which stands for a single newline character.

Two calls, one line C
#include <stdio.h>

int main(void) {
    printf("Hello, ");
    printf("C");
    printf("\n");
    printf("Line two\n");
    return 0;
}
Text
Hello, C
Line two

Three calls built the first line because only the third one printed a newline. This is useful when you build a line from pieces, and it is a common surprise when you forget the \n at the end of a message: the next output runs straight on from the last.

A few other characters also need an escape because they would otherwise end or confuse the text. \" prints a double quote, and \\ prints one backslash.

Escape sequences C
#include <stdio.h>

int main(void) {
    printf("She said \"run it\"\n");
    printf("Folder: notes\\c\n");
    return 0;
}
Text
She said "run it"
Folder: notes\c

Without the backslash, the quote before run would end the text early and the compiler would report an error on the rest of the line.

Practice

Exercise

Your turn

Print exactly these three lines. The second line contains double quotes and the third contains one backslash.

Text
Study plan
"Start small"
Path: c\day-1
Your workspace C
#include <stdio.h>

int main(void) {
    // Write your solution here.
    return 0;
}
Hint 1: the idea

Every line needs its own \n. Characters that would end or confuse the text need an escape: \" for a quote and \\ for a backslash.

Hint 2: a first step

Get printf("Study plan\n"); working first. Then add the second line and run again before you attempt the backslash.

Show one solution
C
#include <stdio.h>

int main(void) {
    printf("Study plan\n");
    printf("\"Start small\"\n");
    printf("Path: c\\day-1\n");
    return 0;
}
Text
Study plan
"Start small"
Path: c\day-1

The quotes on the second line are escaped, so they become part of the text instead of ending it. On the third line, \\ prints one backslash; writing a single \d would be an unknown escape sequence, which the compiler warns about. In the next lesson you will store values in named variables and print them inside your messages.

Was this article helpful?

Your response is saved on this device.