Your first C++ program, explained line by line
A first program is small enough to trace completely: it includes one standard header, defines main, writes a message, and ends successfully.
Learning objectives
- Create the smallest conventional hosted C++ program that writes text.
- Explain every symbol that matters in the example.
- Compile, run, and compare actual output with predicted output.
- Use compiler diagnostics as information rather than as a reason to guess.
Prerequisites: none. This is the starting lesson; you only need to be able to open a terminal or an IDE.
Before you type
Create a plain-text file named first_program.cpp. A source file is not a document with formatting, rich text, or hidden style information. It is text that your C++ toolchain reads. Save it in a folder where you can find it again.
The complete program
#include <iostream>
int main() {
std::cout << "Hello, learner!" << '\n';
return 0;
}Line-by-line explanation
| Code | Meaning |
|---|---|
#include <iostream> | This preprocessing directive makes declarations for standard stream input/output available. The angle brackets name a standard-library header. |
int main() | Defines the special entry function of a normal hosted C++ program. int is its integer return type; parentheses show that this form takes no parameters. |
{ | Begins the block—the group of statements that forms the function body. |
std::cout | Names the standard character output stream. std:: says the name belongs to the standard namespace. |
<< | In this context, inserts the value on its right into the output stream on its left. |
"Hello, learner!" | A string literal: characters written directly in the source. Double quotation marks surround text strings. |
'\n' | A character literal representing a newline. Single quotation marks surround one character; the backslash begins an escape sequence. |
; | Ends this C++ statement. |
return 0; | Ends main with status 0. In this particular function, reaching the closing brace would also have the effect of returning 0, but keeping it visible helps show the function’s end. |
} | Ends the function body. |
Compile and run
Use the instructions supplied by your chosen compiler or IDE. One GCC/MinGW command-line example is:
g++ -std=c++17 -Wall -Wextra -pedantic first_program.cpp -o first_programThen run the output program using the convention of your environment. On many terminals, that might be ./first_program or first_program.exe. The exact command is not portable across all systems; the source code is the lesson, not a promise that one shell command works everywhere.
Execution trace
- The toolchain has already translated and linked the source into a runnable program.
- When launched, the hosted program starts and reaches
main. std::coutreceives the string literal, then the newline character.- The standard output destination shows the message on one line.
return 0;finishesmainwith status 0.
main starts ↓ write H e l l o , ... ! ↓ write newline ↓ return status 0
Why the punctuation exists
Programming languages need clear structure. The braces show which statements belong to the function. Parentheses distinguish a function declaration/definition form. Semicolons mark the end of ordinary statements. Quotes distinguish literal text from a name such as cout. These small marks let a compiler parse the source consistently rather than relying on a reader’s guess.
Common beginner mistakes
- Writing
coutwithoutstd::: this example has not introduced a using declaration, so the full name is required. - Using smart quotes: word processors may replace
"with typographic quotation marks that C++ does not treat as string delimiters. - Forgetting the semicolon: the output statement needs one.
- Mixing quote types:
"A"is a one-character string;'A'is a character literal. They are not the same type. - Typing output into the source: expected output belongs in the terminal after running, not inside the C++ file.
- Changing several lines after an error: read the first diagnostic, make one reasoned correction, and rebuild.
Practical exercises
- Change the message to your name and compile the program. Predict the exact output before running it.
- Add a second output statement that writes your study goal on a new line. Keep the semicolon and newline correct.
- Deliberately remove the semicolon after the first output statement. Build it, read the first diagnostic, restore the semicolon, and build again.
- Replace the double quotation marks around the message with single quotation marks. Read the diagnostic and explain why a full sentence cannot be one character literal.
- Save a copy with a different file name and change only the message. This is your first controlled experiment with source code.
Show sample exercise solutions
- Change the string literal, for example to
"Hello, learner!". The output is exactly that text followed by a newline. - Add
std::cout << "My study goal is to practise C++ daily." << '\n';on the next line insidemain. The second message appears on a separate output line. - The compiler reports a syntax error near the following statement or closing brace; wording and location vary. Restore the semicolon after the output statement and compile again.
- Single quotes are for character literals, not ordinary strings. A multi-character literal such as
'Hello'is not a string and has an implementation-defined value. Use double quotes:"Hello, learner!". - Compile the renamed file by passing its new name to the compiler, for example
g++ -std=c++17 renamed.cpp -o renamed. If the program text is unchanged except for the message, the only output difference is that message.
Assessment: check your understanding
1. What does main represent in this program?
Answer: The designated entry function for this hosted C++ program. Why: Program startup reaches main before it evaluates the body’s statements.
2. Why is <iostream> included?
Answer: It provides declarations used for standard stream input/output. Why: The example uses std::cout, whose declaration is made available through that header.
3. What is the difference between "A" and 'A'?
Answer: The first is a string literal and the second is a character literal. Why: C++ uses double quotes for a sequence of characters and single quotes for one character.
4. Does return 0; print the number zero?
Answer: No. Why: It supplies a completion status to the environment; std::cout performs visible output.
5. What should you do first after a compiler reports an error?
Answer: Read the first relevant diagnostic and compare it with the source near the reported location. Why: One earlier syntax mistake can cause later messages, so random edits usually make diagnosis harder.
Revision summary
A minimal C++ program has a source file, an included header when it needs library declarations, a hosted entry function named main, statements inside braces, and clear punctuation. You can now write, build, run, and trace a program that produces one visible result. The next module gives programs values they can store and use.