Skip to main content

Hello, C++ โ€” Anatomy of a Program

beginner10 min readLesson 2 of 204

Your first program, line by line: includes, main, statements, braces, semicolons, and the stream insertion operator.

Here is a complete C++ program:

#include <iostream>

int main() {
    std::cout << "Hello, C++!" << std::endl;
    return 0;
}

Seven lines. Let's read every one of them, because each is a habit for life.

#include <iostream>

#include asks the compiler to paste in the declarations for a part of the standard library โ€” here, iostream, which provides input and output. Angle brackets <...> mean "look in the standard library". Includes always appear at the top of the file.

int main()

Every C++ program starts at a function named main. The int before it means main returns a whole number to the operating system: 0 conventionally means "finished successfully". The () after the name means it takes no parameters. Exactly one main exists per program.

The body: braces and statements

The { } braces mark the function's body. Inside, each statement ends with a semicolon โ€” C++ uses ; the way humans use periods. Forget one and the compiler will complain, usually on the next line, which is why reading errors calmly matters (next lesson).

std::cout << "Hello, C++!" << std::endl;

This single line has three parts:

  • std::cout โ€” the console output stream ("character output"). The std:: prefix says this name lives in the standard namespace; for now, write it as-is every time.
  • << โ€” the stream insertion operator. You can chain it: std::cout << a << b; sends a then b.
  • std::endl โ€” a newline plus a flush. For a plain newline, '\n' inside the string is enough: "Hello!\n".

So the line means: send the text, then a newline, to the console.

Comments

// a one-line comment
/* a comment
   spanning lines */

Comments are for humans; the compiler deletes them. Write why, not what.

Coming up

Printing is half the conversation. Next: reading input โ€” and why graded challenges on this platform avoid it.

Now practice

Hello Practice: Output BasicsSix output challenges: exact lines, chained values, and prediction. Every solution is two or three lines of std::cout.1 challenge ยท ยท ~20 min