Learn/C++/1. Hello, output
Lesson 1 of 15
Sign in

C++, lesson 1

Hello, output

C++ is a fast, compiled language used for games, browsers, trading systems and anything else where speed matters. You write plain text, the compiler (here G++ 15) turns it into machine code, and then that machine code runs. If the compiler finds a mistake, nothing runs: you get an error message instead.

What each part does:

  • #include <iostream> brings in the standard input/output library, which provides std::cout.
  • int main() is where every C++ program starts. It returns an int to the operating system: 0 means success. In main (and only there) you may leave out return 0; and the compiler adds it for you.
  • Curly braces hold the body of the function, and every statement ends with .

2 exercises to try what you just read.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

Receipt

Press Ctrl Enter or Check to test your code.

{ }
;

Printing with std::cout

std::cout is the standard output stream (say "see-out"). The << operator pushes values into it, and you can chain as many as you like. Text, whole numbers and decimal numbers all work:

std:: means the name lives in the standard library namespace. A namespace is a family name that keeps library names from colliding with yours. You will sometimes see using namespace std; at the top of examples to avoid typing std::; in real projects it is discouraged because it pulls hundreds of names into your program, so this course writes std:: everywhere.

Line breaks

std::cout does not add a line break by itself. Ask for one with '\n' (or "\n" inside a longer string). You will also see std::endl, which ends the line and flushes the output buffer. Flushing is slow and rarely needed, so prefer '\n'.

Other escape sequences you will need: \t (tab), \" (a double quote inside a string) and \\ (a backslash).

Comments

Comments are for people. The compiler ignores them.

How a program runs

When you press Run, the whole file is compiled first, then the program starts at the top of main and runs one statement at a time. A typo anywhere, even on the last line, stops the program from compiling at all. Compiler messages can be long; read the first error first, because later ones are often side effects of it.

This course uses C++17, the language version G++ 15 compiles by default here. You will meet its most useful modern features as you go.

Further reading

  • cppreference: std::cout
  • cppreference: Escape sequences
  • cppreference: Comments
  • cppreference: The main function

Write a program that prints exactly these two lines:

The program reads no input.

Print this receipt with a single statement that chains <<. The numbers 3 and 4.5 must be written as numbers, not inside strings. Between Item and "Tea" and between Qty and 3 there is one tab character.

The program reads no input.

#include <iostream>

int main() {
    std::cout << "Hello, World!\n";
    return 0;
}
std::cout << "Score: " << 42 << '\n';      // Score: 42
std::cout << "Pi is about " << 3.14 << '\n';
std::cout << "Name:\t\"Ada\"\n";   // Name:<tab>"Ada"
// A line comment runs to the end of the line.

/* A block comment
   can span several lines. */
Hello, World!
I am learning C++.
Item	"Tea"
Qty	3
Total: 4.5