Get to know the skeleton of a C++ program line by line; print text to the screen, add comments and read compiler errors.
The small program you ran in the previous lesson is the skeleton of every C++ program. Now let's take it apart piece by piece and print something a little more interesting.
Anatomy of a program
#include <iostream>
int main() {
std::cout << "Byte Akademi\n";
std::cout << "V = 12 V, R = 4 ohm" << '\n';
return 0;
}
Byte Akademi
V = 12 V, R = 4 ohm
Let's go through the lines in order:
#include <iostream>: Tells the preprocessor "paste the header file of the input-output library here".std::coutis defined in this header. Lines starting with#are commands for the preprocessor, not C++ code, so they don't end with a semicolon.int main(): The program's entry point. When the operating system runs the program, this is the first function it calls. Theintin front says that the function returns an integer.- The part between
{and}is the body of the function. std::cout: The standard output stream, i.e. the screen. Thestd::prefix shows that this name comes from the "standard library" namespace.<<: The stream operator. It sends the value on its right into the stream. It can be chained.return 0;: Tells the operating system "everything went fine". If you leave this line out ofmain, the compiler adds it for you; still, writing it out of habit makes the code easier to read.
Line endings: \n or std::endl?
There are two ways to move to a new line. '\n' sends only the newline character. std::endl sends a newline and also empties (flushes) the buffer. Flushing the buffer every time is slow; if you don't need it, use '\n'. Single quotes mark a single character and double quotes mark a piece of text; both work here.
using namespace std
Writing std:: everywhere may look tiring, and many beginner books recommend the line using namespace std;. It is harmless in small experiments, but in large projects the same names from different libraries can clash. In this course we will prefer to write std::; that way you always see when you are using something from the standard library.
Comments
// Single-line comment: the compiler ignores this line.
/* A comment that spans
several lines */
int main() {
// Ohm's law: V = I * R
return 0;
}
A good comment explains why the code does something, not "what" it does. Lines like i++; // increment i are noise; // divide by 1023 because the ADC is 10-bit is valuable.
Common mistakes
| Mistake | What does the compiler say? |
|---|---|
Forgetting the ; at the end of a line | expected ';' before ... |
Not writing std:: | 'cout' was not declared in this scope |
Forgetting the #include | 'cout' is not a member of 'std' |
Writing >> instead of << | no match for 'operator>>' |
Start reading error messages from the very first one. The first error is usually the real problem; the dozens of lines below it are side effects. Fix one, then compile again.
Try it yourself
- Change the program to print your name and your department on two separate lines.
- Print an engineering formula, for example
P = V * I. - Use
std::endlinstead of'\n'and see that the output does not change. - Add a comment line and confirm that the compiler ignores it.
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.