The fastest way to get comfortable with C++ is to write a tiny program end-to-end: type it, compile it, run it, then break it on purpose to see how the compiler talks back. This lesson does that, using a small e-commerce greeting that grows into a real store over the next few sections.
Open a plain text editor or an IDE and create a file named welcome.cpp. Type the program below exactly as shown:
That's the whole program. Five lines, one message, one return value. This little storefront returns later, with added products, prices, and a shopping cart, but for now it just prints a welcome line.
A few things to note before the breakdown:
cout and Cout are not the same name.{ and } come in a pair and enclose the body of main.#, which marks it as a preprocessor directive rather than a normal statement.None of the pieces of this program are decorative. Each one serves a specific purpose. The anatomy at a glance:
Read top to bottom, the diagram lines up with the five lines of source. Each piece in turn:
#include <iostream>This is a preprocessor directive. The # at the start tells a tool called the preprocessor to act on this line before the compiler proper sees it. The directive #include <iostream> tells the preprocessor to find a standard header called iostream and paste its contents into the file at this exact spot.
iostream is the standard library header for input and output streams. It contains the declarations needed to use std::cout (for output) and std::cin (for input), among others. Without this #include, the compiler has no idea what std::cout means and refuses to compile.
This lesson glosses over how includes really work. The compilation pipeline lesson covers it in detail. For now, treat #include <iostream> as the line that "makes std::cout available."
int main()Every standalone C++ program needs exactly one function named main, and that function is where execution starts. When the compiled program runs, the operating system calls main, runs its body, and uses what main returns as the program's exit status.
The int in front of main is the return type. It means main returns an integer. The C++ standard requires main to return int, no other return type is allowed for the entry point. The parentheses () mean the function takes no parameters in this version. There's another form, int main(int argc, char* argv[]), that allows reading command-line arguments, but it isn't needed yet.
{ and }The pair of curly braces after main() encloses the function body. Everything between them is the code that runs when main is called. Braces also introduce a new scope, meaning variables declared inside them only exist there. Later lessons cover scope, but the rule for now is simple: open brace, write statements, close brace.
std::coutstd::cout writes to the terminal. Two pieces are at work here:
std is the standard namespace. The C++ standard library puts almost everything it defines inside a namespace called std so its names don't collide with names defined elsewhere. A namespace is a labeled container for names.cout is the name of the standard output stream object, defined inside std. The full name is std::cout, with :: as the scope resolution operator. Read it as "the cout that lives in std".So std::cout is one specific object: the program's standard output stream. By default, it writes to the terminal.
<<The << token between std::cout and the string is the stream insertion operator. Read it as an arrow that says "push this value into that stream." So std::cout << "Welcome" means "push the string Welcome into the standard output stream."
In other contexts, << is the bitwise left-shift operator. In this context it's overloaded for output streams, meaning the standard library has defined a different behavior for it when the left side is a stream. Read it as: << with a stream on the left means "send the right-hand value to the stream."
Chained << calls can appear in one statement, which is useful when printing a product name and a price together later.
"Welcome to the AlgoMaster Store!"The text in double quotes is a string literal. Double quotes mark a sequence of characters as a string value, in this case 32 characters. The literal is the text to be printed.
Single quotes mean something different in C++ (a single character, like 'A'), so don't use them around words. The data types lesson covers the difference between strings and characters.
std::endlstd::endl is also defined in <iostream>. It does two things: it inserts a newline (so the next output starts on a new line), and it flushes the stream's buffer (so any buffered output gets pushed out to the terminal immediately).
An alternative just prints a newline without flushing: the character '\n', written inside a string as "\n". The two statements below produce nearly the same visible output:
The difference is the flush. std::endl flushes the stream every time, while "\n" doesn't. Flushing has a small cost, so in tight loops printing thousands of lines, "\n" is faster. For one-line programs like this one, the difference is not visible. Pick whichever reads more clearly. This lesson sticks with std::endl and starts mixing in "\n" later when it matters.
std::endl flushes the output buffer on every call. In a loop that prints many lines, use "\n" and flush manually when needed.
;The semicolon at the end is the statement terminator. Almost every C++ statement ends with ;, and the compiler uses it to know where one statement stops and the next begins. Missing one produces a clear compiler error, shown shortly.
return 0;The last line returns the integer 0 from main. By convention, returning 0 from main means "the program finished successfully." Returning any non-zero value means "something went wrong," and shell scripts and other programs use that value to decide what to do next.
main is special: an omitted return statement implicitly returns 0. Leaving it off works, but writing it explicitly is a good habit, since no other function in C++ gets this free pass.
Source files aren't directly runnable. Compile welcome.cpp into an executable first, then run the executable. The most common compiler on Linux and macOS is g++, and Windows users get the same tool through MinGW or WSL.
Open a terminal, change into the directory that contains welcome.cpp, and run:
Three pieces matter here:
g++ is the compiler.-std=c++17 tells g++ to use the C++17 standard. This course uses C++17 throughout because it's widely available and supports the features needed here.welcome.cpp is the source file.-o welcome names the output file welcome. Without -o, g++ would produce a file called a.out by default, which is rarely the desired name.If everything is correct, g++ produces no output. Silence is success. Now run the program:
On Windows, the executable will be welcome.exe, and you run it as welcome.exe or just welcome from the same directory.
The flow from source to terminal looks like this:
A lot happens inside that g++ box (preprocessing, compiling, assembling, linking). For now, treat g++ as a black box that turns .cpp into an executable.
The one-line greeting is fine, but a store says more than "welcome." Small variations build on the same skeleton.
Two std::cout statements, two lines of output. Each statement is its own self-contained instruction, terminated by a semicolon.
std::cout vs Two Separate OnesOne std::cout per piece isn't required. The << operator is chainable, so several pieces can join into a single statement:
Two details matter here. First, the line wraps in the source code but it's still one statement, ending with the single semicolon at the end. Second, a number (24.99) is mixed into the output without quoting. The stream knows how to format a double as text, so it does. The input and output lesson covers this in more depth.
The chained version is the more idiomatic style. The compiler treats both versions the same way at runtime, so pick whichever reads better. For a stream of related values that belong together (product name plus price), chaining is cleaner. For unrelated lines, separate statements are fine.
Comments are notes for humans. The compiler ignores them. C++ has two styles:
The Comments lesson covers conventions and when to use each style. For now, the rule is: // for short notes on a single line, /* ... */ for anything longer or for temporarily disabling a block of code.
using namespace std; and Why People Argue About ItTyping std:: in front of cout and endl every time gets old fast. C++ allows opting out with using namespace std;, which brings every name from std into the current scope. After that line runs, cout and endl work without the prefix:
Same program, less typing. So why doesn't everyone do this?
The short answer is that std is huge. It defines hundreds of names: vector, string, map, count, find, sort, swap, min, max, and many more. Dumping all of them into the global namespace risks a name collision with a variable or function defined elsewhere in the code. In a 20-line program for a lesson, this is fine. In a 200,000-line codebase with multiple libraries, it's a real problem.
The compromise most teams settle on:
| Where | Use using namespace std;? |
|---|---|
| Tiny tutorial programs and competitive programming | Fine |
.cpp source files in small projects | Often acceptable, by team convention |
Headers (.h / .hpp files) | Almost always avoided. A header gets included everywhere, and forcing using namespace std; on every file that includes it leaks the choice. |
| Large codebases with multiple libraries | Avoided. Spell out std:: for clarity and safety. |
This course keeps writing std:: explicitly. It's a few extra characters, but it makes every example unambiguous about where a name comes from, and it builds a habit that scales. The using namespace std; pattern in other people's tutorials or in competitive code is the same construct, used in contexts where the trade-off is different.
Most early bugs come from typos and layout slips, not from misunderstanding C++. The ones below show up first and the compiler's responses match each one. Every error message below is the real text g++ produces, not a paraphrase.
What's wrong with this code?
The std::cout line is missing its semicolon. g++ reports:
g++ points at return because that's the first token it sees that doesn't belong. The actual problem is the missing ; on the line before. Read the message as "I was expecting a semicolon before return."
Fix: Add the semicolon:
#include <iostream>What's wrong with this code?
No #include <iostream>, so the compiler doesn't know what std::cout or std::endl are. g++ reports:
The phrase "is not a member of std" means: as far as the compiler can tell, std doesn't have a cout in it. That's because the header that declares it was never loaded.
Fix: Add the include at the top:
Cout Instead of coutWhat's wrong with this code?
Cout with a capital C is not the same name as cout. g++ reports:
The "did you mean cout" hint is g++ being helpful. Recent versions of the compiler suggest the closest matching name when the typed one isn't found.
Fix: Lowercase the c:
C++ is case-sensitive everywhere. cout, Cout, and COUT are three different names, and only the first one is defined in the standard library.
std:: PrefixWhat's wrong with this code?
The include is there and the names are spelled correctly, but there's no std:: prefix and no using namespace std;. So cout and endl are bare names that the compiler can't resolve. g++ reports:
Fix: Add the std:: prefix:
Or add using namespace std; once, after the include, to avoid typing the prefix. For tutorials and small files, either is fine.
What's wrong with this code?
The closing } for main is missing. g++ reports:
The compiler reads to the end of the file looking for the brace that closes the body of main, doesn't find one, and reports "expected } at end of input."
Fix: Add the closing brace:
Braces always come in pairs. A good editor highlights the matching brace when the cursor is on one. If the pairing gets confusing, indent the code properly and the pairs become obvious.
This part is a teaser. When g++ -std=c++17 welcome.cpp -o welcome runs, the compiler does four things in sequence: it runs the preprocessor (which expands #include <iostream> into hundreds of lines of declarations), it compiles the result into assembly, it assembles that into machine code in an object file, and it links the object file with the standard library to produce a finished executable.
This lesson skipped past all of that. The next lesson opens up the pipeline step by step and shows what each stage produces. After it, the explanation goes beyond "the program prints Welcome to the AlgoMaster Store!" to "exactly how the text in the .cpp file becomes a running process on the machine."
10 quizzes