Contact
Evil Genius SocietyHomeLearnForumLeaderboardSocialsMessages
← Back to module

Lesson 3 of 6

You've run this program. Now let's account for every part of it.

#include <iostream>

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

#include <iostream>

A preprocessor directive (lesson 2.2) — it pastes in the contents of the iostream header, which declares the input/output tools, including std::cout.

Without it, the compiler has never heard of std::cout and refuses to compile.

The angle brackets < > mean "look in the standard system locations." Quotes — #include "my.h" — mean "look next to my source file first," which is what you'll use for your own headers (lesson 5.8).

Note there's no semicolon. Preprocessor directives aren't C++ statements.

int main()

Every C++ program starts at main. Not at the top of the file — at main. It's the entry point the operating system calls, and a program without exactly one of it will not link.

  • int is the return type: main hands an integer back to the OS when it finishes.
  • main is the name. It must be spelled exactly this way.
  • () is the (here empty) parameter list.

main is special in a few ways you'll meet later, but the important one now is that it's where execution begins.

The braces

{ and } delimit a block — a group of statements treated as a unit. Here they hold the body of main. Statements inside run top to bottom.

Braces also control lifetime: variables declared inside a block are destroyed when it ends. That turns out to be one of the most important ideas in the language (lesson 8.2), but for now just read them as "the function's body starts here and ends there."

std::cout << "Hello, World!" << std::endl;

The busiest line. Four things are happening.

std::cout is the standard output stream — the terminal. Read it as "console out."

std:: is a namespace prefix. The standard library puts all its names inside a namespace called std so they can't collide with yours. If you write your own cout, they coexist — yours is cout, theirs is std::cout. The :: is the scope resolution operator: "the cout that lives inside std."

You'll see tutorials write using namespace std; to skip the prefix. Don't. It drags every standard library name into your file and causes genuinely confusing collisions in real projects. Typing std:: is a small price for knowing exactly where a name came from.

<< is the stream insertion operator. Read it as an arrow pointing at the destination: values flow into std::cout. It chains left to right, so this line sends two things to the terminal in order — the text, then the newline.

std::endl ends the line. It does two jobs: writes a newline, and flushes the stream (forces anything buffered to be written out now).

Most of the time you want "\n" instead:

#include <iostream>

int main() {
    std::cout << "Fast\n";
    std::cout << "Also fine" << std::endl;
    return 0;
}
Fast
Also fine

"\n" just writes the newline and lets the system flush when convenient, which is faster in a loop. Use std::endl when you specifically need the output visible immediately — for example when debugging a program that might crash before the buffer is flushed.

The semicolon ends the statement. C++ ignores line breaks; the semicolon is what says "this instruction is complete." Forgetting one is the single most common beginner error, and lesson 2.5 is about reading the message you get when you do.

return 0;

main returns an integer to the operating system: the exit status. 0 means success; anything else means failure. Scripts and build tools rely on this to know whether your program worked.

You can see it in your shell:

./hello
echo $?

$? holds the exit status of the last command. After a successful run it prints 0.

main is unusual in that you may omit return 0; — the compiler inserts it for you. Write it anyway while you're learning; it makes the contract visible.

Try it

Write a program that prints your name and, on a second line, the city you live in. Use \n rather than std::endl. Compile and run it.

Checkpoint

  • #include is a preprocessor directive that pastes in a header; no semicolon.
  • Execution starts at main, which returns an int exit status to the OS.
  • std:: names come from the standard library's namespace — prefer it over using namespace std;.
  • << sends values to a stream and chains left to right.
  • \n writes a newline; std::endl also flushes, which is usually unnecessary.
  • Semicolons end statements — line breaks mean nothing to the compiler.

Questions about this lesson

Log in to ask a question.