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.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
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.
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.
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:
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.
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.
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'.