
Let’s think it through · Start with a story
The girl wants the screen to show “Hello.” For a first program, let’s start with a result we can see.
Break down the process
- PrepareInclude iostream
- Entry pointEnter main
- OutputRun std::cout
- Finishreturn 0
Think of a program as a task list with a starting point. A typical C++ console program starts in main, and std::cout sends text to standard output.
Hello inside quotation marks is the text to display, not a command name. A semicolon marks the end of a statement.
01Your first C++ code
Here is one of the most classic C++ programs:
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
return 0;
}Its job is to output one line of text.
02What is the first-line #include?
#include <iostream> is a preprocessor directive. During preprocessing, it makes declarations related to standard input and output available.
03Why is main special?
In an ordinary hosted environment, main is the entry-point function of a C++ program. int means it returns an integer status value.
04What does cout do?
std::cout is the standard output stream object. Here, << writes the data on its right into the output stream.
05What are semicolons and braces for?
Semicolon ;
Many C++ statements need to end with a semicolon.
Braces { }
They enclose code regions such as function bodies, condition blocks, and loop bodies.
06How does this code run?
07Some common mistakes
Missing semicolon
Can cause a compile error.
Misspelled name
Identifiers must be exact.
Unmatched quotation marks
The string cannot end correctly.
Unmatched braces
The code block structure is broken.
What you have learned
- C++ source code is usually saved in a .cpp file.
- #include <iostream> provides declarations related to standard input and output.
- main is the program’s entry-point function.
- std::cout is used for standard output.
- Source code must be built before it can run.
Your turn
Change Hello inside the quotation marks to Robot. What will the screen show? Will the semicolon appear too?
Have an answer? Click to see the explanation
It shows Robot. The quotation marks and the semicolon at the end of the statement are part of the code syntax, not part of the output text.