Show contents
Chapter 2 of 4 · 15 min
Anatomy of a C++ program
By the end of this chapter, you'll be able to
- Explain the role of #include, main and return 0
- Display text and numbers with std::cout and the << operator
- Go to a new line with \n
- Tell a text between double quotes from a character between single quotes
The skeleton, line by line
#include <iostream> // 1. I use input/output streams
int main() { // 2. the program starts here
std::cout << "Hi!\n"; // 3. an instruction, ending with ;
return 0; // 4. everything went well
} // 5. end of main
Hi!
#include <iostream>includes the part of the standard library that handles input and output streams. That's what providesstd::cout.int main()declares the main function: every C++ program starts by runningmain, which returns a whole number (int).- The curly braces
{ }mark out the block ofmain. Each instruction ends with;. return 0;tells the system that everything went well. (Inmain, and only inmain, this line is optional: C++ adds it for you. We still write it at first, to see clearly where the program ends.)//starts a comment until the end of the line, and/* … */a comment over several lines.
std::cout and the << operator
std::cout ("character output") represents standard output: the terminal. The << operator sends a value into that stream. You can chain it to send several values in a row, mixing text and numbers:
#include <iostream>
int main() {
std::cout << "6 x 7 = " << 6 * 7 << "\n";
std::cout << "Pi is about " << 3.14159 << '\n';
std::cout << "A tab:\there\n";
return 0;
}
6 x 7 = 42
Pi is about 3.14159
A tab: here
std::means thatcoutbelongs to the namespacestd, the standard library's. It's like a family name, which avoids confusion with acoutyou might have created yourself.\nmoves to a new line, and\tinserts a tab. These are escape sequences. You'll also come acrossstd::endl, which moves to a new line and forces an immediate display: it's slower, and\nis almost always enough.
End of main: the program finishes successfully, as with return 0.
Step 6 of 6
Text description of the animation
- When the program is launched, execution starts in main.
- The text is sent to the console as it is. No \n: we stay on the same line.
- The calculation is done first, then its result is displayed right after.
- \n ends the line.
- So the next text starts on a new line.
- End of main: the program finishes successfully, as with return 0.
Your turn
Worked exercise
Worked exercise: the receipt
Write a program that displays this receipt. The total must be computed by the program (3 × 1200), not written by hand:
Item: notebook
Quantity: 3
Total: 3600 KES
Hint 1
A single instruction can chain text and a calculation:
std::cout << "Total: " << 3 * 1200 << " KES\n";.Hint 2
Don't forget the space before
KES, inside the quotes.Show the solution and common mistakes
#include <iostream>
int main() {
std::cout << "Item: notebook\n";
std::cout << "Quantity: " << 3 << "\n";
std::cout << "Total: " << 3 * 1200 << " KES\n";
return 0;
}
Item: notebook
Quantity: 3
Total: 3600 KES
Common mistakes:
- Writing
"3 * 1200"between quotes: the text is displayed as it is, without any calculation. - Forgetting a
<<between two values: compilation error, because C++ doesn't know how to stick two values together without an operator.
Summary
- 1#include <iostream> gives access to std::cout; the program starts in main.
- 2std::cout << value displays a value; chain the << to display several.
- 3\n moves to a new line; std::endl does too, but more slowly.
- 4A text is written between double quotes, a single character between single quotes.
- 5std:: refers to the standard library.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.