Chapter 1

Hello, World — and What Actually Happened

Taking four lines apart, one character at a time.

Here’s what you typed in chapter 0.

#include <iostream>

int main() {
    std::cout << "Hello, world\n";
}

One of those lines you can probably guess. The rest look like someone sat on a keyboard. By the end of this chapter you’ll know what every character is doing — not roughly, every character.

This is the only program in the book we’ll take apart this carefully. It’s worth it, because some version of these four lines sits at the top of nearly everything you’ll write from here on.

The line that did something

Three of those lines are scaffolding. This is the one that put words on your screen:

std::cout << "Hello, world\n";

std::cout is where your text goes when you want it seen — your terminal window. The name is short for “character output”, which nobody says out loud.

The << is the direction. Text flows the way the arrows point: out of your program and into cout. That’s the right instinct to keep, and you’ll be pointing things at cout for the rest of this book.

The quotes, and the \n that isn’t a slash

The quotation marks are not part of your text. They’re how you show the compiler where your text begins and ends. Everything between them gets sent exactly as you wrote it — spaces, capitals, and all.

Which leaves the strange bit at the end: \n.

That isn’t a backslash followed by an n. It’s one character — a newline, the invisible one that ends a line of text. You can’t type it between the quotes, because pressing return would end your line of code instead. So C++ lets you spell it with two visible characters, and the compiler understands.

Exercise 1 · Break the newline

Delete the \n, leaving std::cout << "Hello, world";. Compile it, run it, and look at where your prompt ends up.

Now try "Hello, world\n\n". Two newlines, two line endings, one blank line between you and your prompt. Put it back to one when you’re done.

int main(), or where a program starts

int main() {

When you run a program, the computer needs somewhere to begin. In C++ that place is always called main. Not by convention — by rule. A program without a main doesn’t fail at some point during the run; it never gets built in the first place.

The braces, { and }, are the boundaries. Everything between them is what main does, in order, top to bottom. Yours contains exactly one instruction.

The empty parentheses are where you’d hand main some information, if you had any to hand it. You don’t. Chapter 9 is where parentheses start earning their keep.

And int says that main reports a number back when it’s finished. Zero means it went fine; anything else means it didn’t. Whoever started your program — your terminal, or another program — can read that number and decide what to do about it.

The line at the top

#include <iostream>

Here’s the thing that surprises people: std::cout is not part of the C++ language. Printing isn’t built in. It arrives from a large collection of ready-made tools called the standard library, and iostream is the piece of it that handles input and output.

That line is you asking for the piece you need. Without it, the compiler has never heard of std::cout, and it will tell you so at considerable length.

Notes to yourself

Anything after // on a line is ignored by the compiler completely. It’s there for humans.

#include <iostream>

int main() {
    // Say hello. Chapter 1 makes a whole meal of this line.
    std::cout << "Hello, world\n";
}

There’s a second form, /* like this */, which can run across several lines. You’ll mostly want //.

Write comments about why, not what. A comment saying “print hello” next to a line that prints hello is just noise you now have to keep in step with the code.

Sending more than one thing

You can point several things at cout in a single statement. Just keep adding arrows:

std::cout << "Hello, " << "world" << '\n';

Notice the last one uses single quotes. Double quotes mean a piece of text; single quotes mean exactly one character. '\n' is one character, so single quotes fit it perfectly. Both work here — the difference starts to matter in chapter 4.

Exercise 2 · Two lines, one statement

Print your name on one line and the year on the next, using a single std::cout statement and no more than one semicolon.

And that’s the whole program. Four lines, no mysteries left in them.

Check yourself

1. Which line actually puts text on the screen?

Not quite. That line fetches the printing tools from the standard library. It borrows the equipment; it does not use it.

Not quite. That is where the program starts, not where it prints. A program with only this line runs and does nothing.

Yes. The arrows send the text out to std::cout, which is your terminal.

2. How many characters is the \n at the end of the text?

Not quite. That is how you type it, but not what it is. The backslash tells the compiler the next character is not literal, and the pair becomes one character.

Yes. It is a single invisible character that ends a line. It takes two visible characters to write because you cannot press return inside quotes.

Not quite. It really is a character, and it really is sent to the terminal. It just happens to be one you cannot see.

3. Our main never says return 0, yet the program is correct. Why?

Not quite. Only main gets this exemption. An ordinary function that promises a number must hand one back — chapter 9 comes to that.

Yes. It is the one exception in the language. Rename main to something else and the compiler immediately complains that a value is missing — chapter 2 does exactly that.

Not quite. Program length has nothing to do with it, and the value is not ignored — whoever launched your program can read it.

From here on the loop is always the same: edit the file, run the compile command, run the program. Edit, compile, run. You’ll do it thousands of times, and within a week your fingers will do it while you think about something else.

Project

A fortune card

Roughly 20 minutes

Write a program that prints a fortune card — a bordered box with a fortune inside it. Something like:

+----------------------------+
|                            |
|  Your fortune:             |
|  You will write a program   |
|  that works. Eventually.   |
|                            |
+----------------------------+

Rules of the game:

  • The fortune is yours, not mine. Write something you’d actually want to read.
  • Use comments to mark the top border, the message, and the bottom border.
  • The right-hand edge must line up. Every | in a column, dead straight.

That last rule is the whole exercise, and yes, it means counting characters by hand. Notice how irritating it is — I’ve left a wonky line in the example above for you to find. Chapter 4 gives you a way to ask how long a piece of text is, which is the beginning of getting the computer to do this counting for you.

Stretch: print the card twice, with two different fortunes, from one program. If you find yourself copying and pasting the border lines, you’ve just discovered why chapter 9 exists.