Your First C++ Program
Walk through Hello World line by line: #include, main(), cout, and semicolons.
Time to understand every line of the program you saw:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}#include <iostream>
C++ ships with pre-written code organized into files called header files. iostream (short for input/output stream) is the header that contains the printing and keyboard-reading tools. #include copies those tools into your program.
Without this line, the compiler has never heard of cout and refuses to compile.
using namespace std;
All the standard C++ tools live in a named group (a namespace) called std, short for standard. This line says: "when I write cout, I mean the one from std." Without it, you'd have to write std::cout every time. Both styles are valid — this course uses the short one for readability.
int main() { ... }
main is where every C++ program starts running — the entry point. When you run your program, the computer finds main and executes the instructions between its braces { }, top to bottom.
Every program must have exactly one main. (You're looking at your first function — a named block of code. Module 5 is entirely about writing your own.)
cout << "Hello, World!" << endl;
cout (console output, pronounced "see-out") is how C++ prints to the screen. The << arrows push whatever is on their right into cout:
cout << "Hello"; // prints text
cout << 42; // prints a number
cout << "Age: " << 30; // chain several things: Age: 30endl (end line) moves to the next line, like pressing Enter.
Line 6: return 0;
When main finishes, it reports a number to the operating system. By universal convention, 0 means "everything went fine" and any other number means "something went wrong".
What does this print?
#include <iostream>
using namespace std;
int main() {
cout << "Year: " << 2024 << endl;
return 0;
}- AYear: 2024
- BYear:2024
- C"Year: " 2024
- DError
Two rules of C++ grammar you must know from day one.
Rule 1: statements end with a semicolon.A statement is one complete instruction — like one sentence. In C++, the 'period' at the end of the sentence is the semicolon ;:
cout << "Hi"; // one statement
return 0; // another statementForget a semicolon and the compiler sees two sentences running together — it stops with an error.
Rule 2: braces group code, indentation is decoration.The braces { } after main() mark where the program's instructions begin and end. The spaces at the start of each line (indentation) are purely for human eyes — the compiler ignores them completely. We indent anyway, always, because readable code matters as much as working code.
A programmer forgot a semicolon. What happens?
#include <iostream>
using namespace std;
int main() {
cout << "Hello" // missing semicolon
return 0;
}- APrints Hello
- BPrints nothing
- CCompile error — the program won't run
- DThe program crashes while running
What does using namespace std; do?
- ALets you write cout instead of std::cout
- BLoads the input/output tools into your program
- CMarks where the program starts
- DEnds the program successfully
One of these lines has quotes, the other doesn't. What is the output?
#include <iostream>
using namespace std;
int main() {
cout << 3 + 4 << endl;
cout << "3 + 4" << endl;
return 0;
}- A7 3 + 4
- B3 + 4 3 + 4
- C7 7
- DError
Complete the line so it prints the message and then moves to a new line:
cout << "Done" << ___;