Skip to content
VLSI Mentor

C++ Part I · Module 2 · Building and Running C++ Programs

Your First C++ Program, Line by Line

Chapter 2.1's program was complete and silent. This one talks. Printing a value is the single most useful thing a beginner can do, because it turns every later lesson from something you read into something you can check — and checking is how you find out that your mental model is wrong before it costs you a day. The program here is twelve lines, and we will account for every one of them. That matters more than it sounds: most people meet this program as a template to copy, carry the habit of not knowing what three of its lines mean, and are still unsure two years later. We will also be honest about the parts that cannot be fully explained yet, say how long each stays provisional, and end by breaking the program on purpose three times, because seeing what a mistake looks like is the fastest way to recognise it again.

Foundation14 min readC++Part IFundamentalsOutput

Part I · Module 2 · Chapter 2.2 · Your First C++ Program, Line by Line

1. The Program

Put this in a file called hello.cpp:

Azvya Education Pvt. Ltd.VLSI Mentor
hello.cpp — a complete program
#include <iostream>

int main()
{
    int expected_value = 42;
    int actual_value   = 40;

    std::cout << "expected = " << expected_value << '\n';
    std::cout << "actual   = " << actual_value << '\n';
    std::cout << "difference = " << expected_value - actual_value << '\n';

    return 0;
}

Build and run it. Chapter 2.3 explains the command properly; for now use it as given:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
$ c++ -std=c++17 -Wall -Wextra -Wpedantic hello.cpp -o hello
$ ./hello

Expected output:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
expected = 42
actual   = 40
difference = 2

Two numbers and the gap between them. That shape — an expected value, an actual value, and the difference — is the smallest possible version of what a checker does, and it will come back throughout this curriculum.

Now the accounting. You already know int main(), the braces, the statements, the semicolons and return 0; from Chapter 2.1. Four things are new.

2. #include <iostream>

C++ does not know how to print. Nothing in the language itself has anything to say about a console.

What it has instead is a standard library: a large body of code and names that ships with every C++ implementation. Printing lives there. But the compiler does not go looking for it on its own — you have to ask for the part you need, and #include is how you ask.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
#include <iostream>

Read it as: bring in the declarations needed for input and output streams. iostream is the name of the piece you want — short for input/output stream.

Three things to notice now, each with an owner later:

  • It has no semicolon. This line is not a C++ statement. It is a directive for the preprocessor, a separate step that runs over your file before the compiler looks at it. Different language, different rules. Chapter 2.5 is entirely about what it does.
  • The angle brackets mean "a standard or system header". There is a second form using quotes, for headers you wrote yourself. Chapter 2.4 introduces it.
  • It goes at the top. Not because of a style rule, but because the names it brings in must exist before the lines that use them. A #include <iostream> placed after main would not help the std::cout inside it.

3. std::cout

std::cout is the name of the standard output stream — the console, by default.

It is two parts joined by ::.

std is a namespace: a named group that the whole standard library lives inside. Its purpose is to stop names colliding. If the library owned the plain name cout, then no library and no program anywhere could ever use that name for anything else. Putting it in std means the full name is std::cout, and your own cout — if you ever wanted one — would be a different name.

:: is the scope resolution operator. Read std::cout as "the cout that belongs to std".

That is as far as this chapter goes. Namespaces have real rules — nesting, qualification, what happens when two are in play — and Chapter 7.6 teaches them. For now, std:: is a prefix that says "this name comes from the standard library", and you will type it on every standard name you use.

4. <<

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
    std::cout << "expected = " << expected_value << '\n';

The << symbols send a value to a stream. Read the line left to right: send the text expected = to the console, then send the value of expected_value, then send a newline.

Two useful observations.

It chains. Each << hands the stream back, so the next << can use it again. That is why one statement can send four different things and why the order on the page is the order on the screen.

It adapts to the type. "expected = " is text and expected_value is a number, and you did not have to say which was which. In C the equivalent requires you to name the type in a format string, and getting that wrong is a classic bug — Chapter 1.4 was about exactly this kind of difference. Here the compiler picks the right behaviour from the type of each value.

How it does that is a real mechanism — << is an operator with several definitions, chosen by what sits on each side of it. Module 15 teaches operator overloading and explains it fully. Until then: << sends the thing on its right to the stream on its left, and handles common types correctly.

5. '\n'

'\n' is a single character: a newline. Without it, everything would print on one line.

Note the single quotes. In C++, single quotes mean one character and double quotes mean text. '\n' is one character; "\n" is text that happens to contain one character. Both work here. The distinction becomes important in Module 9, and using single quotes for a single character is the habit worth starting with.

The backslash is an escape: \n is two characters in your source that mean one character in the output. A few others you will meet are \t for a tab, \\ for a literal backslash and \" for a quotation mark inside text.

You may also see std::endl used to end lines:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
    std::cout << "expected = " << expected_value << std::endl;   // not what we use

std::endl writes a newline and does something else as well. That something else matters in exactly one situation, it is worth knowing about, and Chapter 2.8 measures it and tells you when to reach for it. Until then, use '\n'.

6. The Loop You Will Spend Years In

Reading about a program teaches less than changing one. The cycle is:

  1. Edit the source.
  2. Build it.
  3. Run it.
  4. Compare what happened with what you predicted.

Step 4 is the one people skip, and it is the one that does the teaching. Predicting first means a surprise is informative; running first and rationalising afterwards means you learn nothing, because anything the program did will seem reasonable once you have seen it.

Try one change now. Replace the third output line with this:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
    std::cout << "difference = " << actual_value - expected_value << '\n';

Predict the output before you build. Then build and check:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
expected = 42
actual   = 40
difference = -2

Subtraction is not symmetric, so the sign flips. Trivial — and the habit is not. In a checker, expected - actual and actual - expected are both defensible, and a team that has not agreed which it means will eventually read a report backwards.

7. Break It On Purpose

Three mistakes, each one a thing you will do for real. All three diagnostics below are from the machine used to write this chapter; the wording varies between compilers and versions, but the shape does not.

Mistake A — delete #include <iostream>

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
hello.cpp:7:5: error: use of undeclared identifier 'std'
hello.cpp:8:5: error: use of undeclared identifier 'std'
hello.cpp:9:5: error: use of undeclared identifier 'std'

What you intended: to print. What the code says: use a name called std, which nothing has introduced. Why: std::cout is not built into the language. Without the #include, the compiler reaches line 7 having never heard of std, and says so.

Notice there are three errors from one deleted line — one per use. The deleted line is not mentioned in any of them. This is worth registering now: the number of messages is not the number of mistakes, and the first message is the one to work on. Chapter 2.7 turns that into a method.

Mistake B — drop a semicolon

Remove the ; from the end of the first output line:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
hello.cpp:8:57: error: expected ';' after expression

What you intended: one output statement, then another. What the code says: one statement that keeps going onto the next line. Why: the compiler does not stop at the end of a line. It kept reading, joined line 7 and line 8 into one expression, and only complained when the result stopped making sense.

Look at where it complained: line 8, one past the line you actually broke. That is normal for a missing semicolon and it is the single most common reason a beginner stares at a line that is perfectly fine. When a message points at a line you believe is correct, check the line above it.

Mistake C — misspell a name

Change one std::cout to std::cuot:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
hello.cpp:9:10: error: no member named 'cuot' in namespace 'std'

What you intended: to print. What the code says: use a name in std that does not exist. Why: std was found — the include is present — but it has nothing called cuot.

Compare this with Mistake A. Both are "a name is not available", and the messages are different in a way that tells you where to look: "use of undeclared identifier std" means the whole namespace is missing, so suspect the include. "no member named cuot in namespace std" means the namespace is there and the specific name is wrong, so suspect your spelling.

8. What You Should Be Able to Say Now

  1. #include <iostream> brings in the declarations needed for console output. It has no semicolon because it is a preprocessor directive, not a C++ statement.
  2. It goes at the top because the names must exist before the lines that use them.
  3. std is a namespace — a named group holding the standard library — and :: means "the name that belongs to it". std::cout is the standard output stream.
  4. This curriculum writes std:: explicitly and avoids using namespace std;, because that line pulls in many ordinary words and the collisions it causes are hard to read.
  5. << sends the value on its right to the stream on its left, chains left to right, and adapts to the type of each value.
  6. '\n' is one newline character; single quotes mean one character and double quotes mean text.
  7. std::endl also ends a line and does one more thing, measured in Chapter 2.8. Use '\n' until then.
  8. The working loop is edit, build, run, compare against a prediction — and the prediction is what makes it teach.
  9. One mistake can produce several messages. The first one is the one to fix.
  10. A missing semicolon is usually reported on the line after the real mistake.
  11. "Undeclared identifier std" points at a missing include; "no member named X in namespace std" points at a misspelling.

9. Exercises

Exercise 1 — predict, then run. Without building, write down what this prints. Then check.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
    std::cout << "a" << 1 + 2 << "b" << '\n';

Exercise 2 — classify two failures. Given only these two messages, say which line of the program you would look at first and why: (a) error: use of undeclared identifier 'std' (b) error: no member named 'cot' in namespace 'std'

Exercise 3 — the off-by-one line number. Explain, in your own words, why a missing semicolon on line 7 is reported on line 8. Your answer should mention what the compiler does at the end of a line.

Exercise 4 — count the mistakes. A build produces nine errors. A colleague says "nine things are wrong". Give the better interpretation and the action it implies.

Exercise 5 — make the output honest. The program prints difference = 2 for an expected 42 and an actual 40. Change it so the label states the direction, and say why that matters to someone reading a regression log at 2 a.m.

Exercise 6 — justify the prefix. A colleague adds using namespace std; and says it makes the code cleaner. Give the specific technical objection from §3, and name one thing they are right about.

Exercise 7 — what is provisional? List the parts of hello.cpp you can now fully explain, and the parts you are still taking partly on trust. For each item in the second list, name the chapter that resolves it.

10. Summary

  • #include <iostream> is a preprocessor directive, not a statement — no semicolon — and it must come before the lines that need it.
  • std::cout is the standard output stream. std is the namespace the library lives in; :: reaches into it.
  • Write std:: rather than using namespace std;. It is explicit and it never collides.
  • << sends a value to a stream, chains left to right, and picks the right behaviour from the value's type.
  • '\n' is one character. Single quotes for a character, double quotes for text.
  • Edit, build, run, compare with a prediction. The prediction is the part that teaches.
  • One mistake can produce several messages, and a missing semicolon is usually reported one line late.
  • Read the message before the code. The wording distinguishes a missing include from a misspelling.

You now have a program you understand and a loop for changing it. The command used to build it, though, was handed to you without explanation — four options whose purpose was never stated, one of which quietly decides which version of C++ you are writing. Chapter 2.3 takes that command apart, and shows a program that compiles silently without those options and reports a real bug with them.

Standards & specifications

Governing standard
ISO/IEC 14882 (the C++ language standard)(opens ISO/IEC (JTC 1/SC 22/WG 21) in a new tab)

Defines the C++ language and its standard library — the type system and templates, the object model and object lifetime, initialisation and overload resolution, the memory model and the containers, algorithms and utilities the library provides. Like C's standard it is a FAMILY of editions (C++98/03, C++11, C++14, C++17, C++20, C++23) and the editions are NOT interchangeable: a feature this curriculum relies on may be unavailable, differently specified or merely proposed in another edition, so each lesson names the edition its claim depends on. It does not define any foreign-function interface to a hardware description language, nor what a compiler actually emits, nor platform behaviour on real silicon — those are the implementation's and the device's contracts. Working drafts published by WG21 are freely available and are what most engineers read day to day; a draft is not the published standard.

This page also covers RTL structure, verification approach and debugging technique. Those are engineering practice built on the standard, not requirements the standard itself imposes.

Where this fits

Part of the C++ Programming curriculum.