Introduction
C++ is a high-performance, compiled programming language that extends C with object-oriented features, giving programmers low-level control over memory and hardware for systems software, games, and performance-critical applications.
Part of the free C++ course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll be able to write, compile, and run a complete C++ program that prints text to the screen and reads input from the keyboard — the foundation of every C++ program you'll ever build.
What You'll Learn
💡 Real-World Analogy
C++ is like a manual race car . Languages such as Python are automatics — easy to drive, but the engine makes decisions for you. C++ hands you the gearstick: you control memory and squeeze out maximum speed, which is exactly why it powers games, operating systems, and anything where every millisecond counts. The trade-off is that you learn how the engine works — and this lesson is your first lap around the track.
1. What Is C++ (and Why Learn It)?
C++ is a compiled, high-performance language created by Bjarne Stroustrup in 1979. "Compiled" means your code is translated into raw machine instructions before it runs, so it executes extremely fast — far faster than languages that interpret code line by line. That speed and low-level control are why C++ sits underneath a huge amount of the software you use every day.
Where C++ runs the show
- Games & engines: Unreal Engine, most AAA titles, console software
- Operating systems: parts of Windows, macOS, and the Linux kernel
- Browsers: the core of Chrome, Firefox, and Edge
- Performance-critical systems: databases, trading platforms, robotics, embedded devices
2. How Compiling Works
A C++ file (ending in .cpp ) is just text — your CPU can't run it directly. A compiler translates that text into a machine-code executable you can run. The most common free compiler is g++ . On your own machine you'd save your code as main.cpp and run two commands in a terminal:
Compile, then run
The -o app part names the output file. If you leave it off, g++ creates a default file called a.out . The good news: the editor below compiles and runs for you, so you can focus on the code itself.
3. Your First Program, Line by Line
Every C++ program has the same skeleton. #include < iostream > pulls in the input/output library so you can print and read text. int main() is the entry point — the place execution begins. Inside it, std::cout < < sends text to the screen and std::endl ends the line. Read every comment in the worked example below, then run it.
4. Comments, Namespaces & Semicolons
Three small things carry a lot of weight. A comment ( // for one line, /* ... */ for several) is a note the compiler ignores — use it to explain why . The std:: prefix is a namespace : it says "find cout inside the standard library", which keeps names from clashing in big programs. And the semicolon ends every statement, because C++ ignores line breaks and needs a marker for where one instruction stops.
Your turn. The program below is almost complete — fill in the three blanks marked ___ using the hints in the comments, then run it.
5. Reading Input with std::cin
Output sends data out ; input brings data in . std::cin reads what the user types, using the > > operator. Notice the arrows point opposite ways: std::cout < < pushes text out to the screen, while std::cin > > pulls a typed value into a variable. Run the worked example, then complete the guided one.
Now you try. Fill in the two blanks so the program reads a city and greets it.
Common Errors (and the fix)
- "expected ';' before ..." — you forgot a semicolon at the end of a statement. Every line of code ends with ; . Add the missing one on the line the error points to.
- "'cout' was not declared in this scope" — you used cout without #include < iostream > at the top. Add that include line so the compiler knows what cout is.
- "'cout' was not declared" even with the include — you wrote cout instead of std::cout . Without using namespace std; you must prefix it with std:: .
- Program builds but the OS reports a non-zero exit — you forgot return 0; at the end of main() . Add it so you explicitly report success.
- Wrong operator: using = instead of < < . Output uses the insertion operator: std::cout < < "text" , not std::cout = "text" .
Pro Tips
- 💡 Compile early and often. Write a few lines, run them, repeat. Don't write 100 lines before your first compile.
- 💡 Fix the first error first. Compiler errors list a line number; later errors are often caused by the earliest one.
- 💡 Prefer '\n' over std::endl when you don't need to flush the buffer — it's slightly faster in tight loops.
📋 Quick Reference
Task
Code
Include I/O library
#include < iostream >
Entry point
int main() { ... return 0; }
Print a line
std::cout < < "text" < < std::endl;
Read input
std::cin > > variable;
Single-line comment
// note for humans
Multi-line comment
/* ... */
Compile & run
g++ main.cpp -o app && ./app
Frequently Asked Questions
Mini-Challenge: Greeting Card
No blanks this time — just a brief and a starter outline. Build it, run it, and check your output against the example in the comments. This is exactly the kind of small program everything bigger is made of.
🎉 Lesson Complete
- ✅ C++ is a fast, compiled language behind games, operating systems, and browsers
- ✅ g++ main.cpp -o app compiles your code; ./app runs it
- ✅ Every program needs #include < iostream > and an int main() entry point
- ✅ std::cout < < prints output; std::endl ends the line
- ✅ std::cin > > reads keyboard input into a variable
- ✅ std:: is the standard-library namespace; every statement ends with ;
- ✅ Next lesson: Variables & Data Types — store and manipulate numbers, text, and more
Practice quiz
Which header must you #include to use std::cout and std::cin?
- <string>
- <iostream>
- <stdio>
- <output>
Answer: <iostream>. <iostream> provides the input/output streams std::cout and std::cin.
Where does execution of a C++ program begin?
- The first #include
- The first function alphabetically
- int main()
- The last line of the file
Answer: int main(). int main() is the entry point — execution always starts there.
Which operator sends text to the screen with std::cout?
- << (insertion)
- >> (extraction)
- = (assignment)
- -> (arrow)
Answer: << (insertion). std::cout uses the << insertion operator to push text out to the screen.
Which operator reads keyboard input into a variable with std::cin?
- <<
- =
- >>
- &&
Answer: >>. std::cin uses the >> extraction operator to pull typed input into a variable.
What does 'return 0;' at the end of main() conventionally mean?
- The program failed
- The program finished successfully
- Print zero to screen
- Loop back to the start
Answer: The program finished successfully. By convention 0 means the program finished successfully; non-zero signals an error.
What does std::endl do?
- Ends the program
- Deletes the last character
- Ends the current line (like pressing Enter)
- Empties the variable
Answer: Ends the current line (like pressing Enter). std::endl ends the current line, like pressing Enter.
Why must you write std:: before cout (without 'using namespace std;')?
- std:: makes it run faster
- cout lives in the std namespace
- It is required punctuation for every word
- It changes cout to uppercase
Answer: cout lives in the std namespace. cout lives inside the standard library's std namespace, so its full name is std::cout.
What marks the end of a statement in C++?
- A new line
- A semicolon ;
- A period .
- A closing brace }
Answer: A semicolon ;. C++ ignores line breaks; the semicolon marks where one statement ends.
Which is a correct single-line comment in C++?
- # a note
- // a note
- <!-- a note -->
- ** a note
Answer: // a note. // begins a single-line comment; everything after it on the line is ignored.
What command compiles main.cpp into a program called app with g++?
- g++ main.cpp -o app
- run main.cpp app
- g++ app -o main.cpp
- compile main.cpp app
Answer: g++ main.cpp -o app. g++ main.cpp -o app compiles main.cpp; the -o flag names the output 'app'.
Continue this course
- Next: Variables & Data Types