A program is a set of instructions written in a programming language. A processor cannot read C++ directly: it only runs machine instructions, which are far too detailed for people to write by hand. C++ lets you describe what should happen in readable text, and a tool called the compiler translates that text into a program the machine can run. You can start C++ here without learning C, Python or any other language first.
The compiler does more than translate. It checks your source before anything runs, so many mistakes are caught early with a message that points at a line. That is the habit you will build today: write a small program, predict what it prints, run it, and read the compiler's first message when something is wrong.
Press Run to compile and execute an example; Reset restores the supplied code. The first C/C++ run downloads about 30 MB of compiler files, then caches them. Stop ends a long run.
Predict and run
Read the program and write down exactly what you expect it to print, including line breaks. Then run it.
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
std::cout << "Day 1\n";
return 0;
}Check your prediction
Hello, C++!
Day 1The program printed two lines because each piece of text ends with \n, the newline character. Statements run from top to bottom, so the order of the two output lines matches the order of the two statements. Swap them and run again to confirm it.
The parts of every program
Every program in this course uses the same frame. Learn what each piece does, because you will keep it while you change what is inside.
#include <iostream>makes the standard input and output tools available. Without it, the compiler does not know whatstd::coutis.int main()names the function where your program starts. Every C++ program has exactly onemain.- The braces
{and}mark where the body ofmainbegins and ends. - Each statement ends with a semicolon. The semicolon, not the line break, tells the compiler where a statement stops.
return 0;reports to the operating system that the program finished successfully.
std::cout is the standard output stream: text sent to it appears in the output panel. The << operator sends the value on its right into the stream. The std:: prefix says the name comes from the standard library. Text in double quotes is a string literal. A // begins a comment, which the compiler ignores. Indentation helps people read blocks, but it does not change what the program does; only the braces do.
Send several values in one statement
You can chain << to send several values in a row. Numbers do not need quotes: the stream prints them in decimal. An expression such as 20 + 5 is calculated first, and its result is printed.
#include <iostream>
int main() {
// Text, then a number, then a newline.
std::cout << "Minutes: " << 25 << "\n";
std::cout << "Two sessions: " << 20 + 5 << " minutes\n";
std::cout << "No newline yet... ";
std::cout << "same line\n";
return 0;
}Minutes: 25
Two sessions: 25 minutes
No newline yet... same lineThe third statement did not end its text with \n, so the fourth statement continued on the same line. Output only moves to a new line when you send a newline. Notice also the difference between "20 + 5" in quotes, which would print those exact characters, and 20 + 5 without quotes, which prints 25.
Compile time and run time
Two kinds of mistake behave very differently. A compile error means the source is not valid C++, so no program is produced and nothing runs. A logic error means the source is valid but says something other than what you meant, so the program runs and prints the wrong thing.
Fix it
Press Run on the broken program below. No output appears: the compiler refuses to build it, and the panel shows a message instead. Read the first error line, find the line and column it names, and repair the program so it prints the two lines from the start of this lesson.
#include <iostream>
int main() {
std::cout << "Hello, C++!\n"
std::cout << "Day 1\n";
return 0;
}The compiler reports something like this:
main.cpp:4:33: error: expected ';' after expressionCheck your fix
Add the ; at the end of line 4. The repaired program:
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
std::cout << "Day 1\n";
return 0;
}Hello, C++!
Day 1The message names a file, a line and a column: line 4, column 33, which is just past the closing quote. The compiler only noticed the problem when it reached the next std::cout on line 5 and found two statements run together. Read the first error first: one missing character can cause several later messages, and they often disappear once the first is fixed. Fix one thing, then run again.
A misspelled word inside quotes is not a compile error. "Helo, C++!" compiles and prints Helo, C++! because the compiler does not check the spelling of your text. Only you can catch that, which is why you predict the output before you run: a prediction turns "it ran" into "it did what I meant."
Practice
Your turn
Print this three-line study card exactly:
Study planDay 1: Hello, C++Minutes: 25
Send the 25 to std::cout as a number, not as part of a quoted string, and use exactly one std::cout statement per line of output.
#include <iostream>
int main() {
// Write your solution here.
return 0;
}Hint 1: the idea
Each output line needs its own \n at the end. A number can follow text in the same statement with another <<.
Hint 2: a first step
Get the first line working with std::cout << "Study plan\n";, run it, then add the next statement.
Show one solution
#include <iostream>
int main() {
std::cout << "Study plan\n";
std::cout << "Day 1: Hello, C++\n";
std::cout << "Minutes: " << 25 << "\n";
return 0;
}Study plan
Day 1: Hello, C++
Minutes: 25Each statement sends its values in order and ends with a newline, so the output has exactly three lines. The last line mixes a string literal and a number in one chain, which is the pattern you will use constantly. In the next lesson you will give that 25 a name, so the program can store it, change it and reuse it.
Completion is saved in this browser only. It does not measure mastery.