Write • Save • Compile • Run • Fix Errors • Understand Code
This chapter teaches you how to write, save, compile, and run your first C++ program.
We will explain everything step-by-step, using very simple language.
4.1 What You Will Learn in This Chapter
- How to write your first C++ program
- How to save the program
- How to compile it
- How to run it
- How to fix basic mistakes
- Understanding what each line of code means
4.2 Structure of a Simple C++ Program
Below is the simplest C++ program that prints a message on the screen:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!";
return 0;
}4.3 Explanation (Line by Line)
| Line | Meaning |
|---|---|
#include <iostream> | Gives access to input/output commands like cout. |
using namespace std; | Allows writing cout without using std::cout. |
int main() | Every C++ program starts from the main() function. |
{ | Marks the beginning of the main function. |
cout << "Hello, C++!"; | Prints a message on the screen. |
return 0; | Tells the computer the program ended successfully. |
} | End of the program. |
4.4 Steps to Write Your First Program
✔ Step 1 — Open your C++ editor
Examples: Dev-C++, CodeBlocks, VS Code, Turbo C++, etc.
✔ Step 2 — Create a new file
Click: File → New → Source File
✔ Step 3 — Type the program
Copy and type:
#include <iostream>
using namespace std;
int main() {
cout << "My first C++ program!";
return 0;
}✔ Step 4 — Save the file
Use name like:
first.cpp4.5 How to Compile the Program
Depending on the software:
✔ In Dev-C++
Go to: Execute → Compile
If successful, no errors appear.
✔ In CodeBlocks
Click: Build → Build
OR press Ctrl + F9
✔ Using command line
If using g++:
g++ first.cpp -o firstThis creates an executable file named first.
4.6 How to Run the Program
✔ Dev-C++
Click Execute → Run
✔ CodeBlocks
Press F9
✔ Command Line
Type:
./first4.7 Expected Output
My first C++ program!4.8 Common Mistakes (and Fixes)
❌ Missing semicolon
cout << "Hello"✔ Fix:
cout << "Hello";❌ Missing quotes
cout << Hello;✔ Fix:
cout << "Hello";❌ Wrong case
Cout << "Hi";✔ Fix:
cout << "Hi";Remember: C++ is case sensitive.
4.9 A Better First Program
This shows multiple messages:
#include <iostream>
using namespace std;
int main() {
cout << "Hello Students!\n";
cout << "Welcome to C++ programming.\n";
cout << "Let's start learning.\n";
return 0;
}4.10 Summary of Chapter 4
- Every C++ program starts with
main() - Use
coutto print messages - Save program with
.cppextension - Compile to check errors
- Run to see output
- Use semicolons and correct spelling
4.11 Exercises
Q1. Write the simplest C++ program that prints your name.
Q2. What does #include <iostream> do?
Q3. What is the purpose of return 0;?
Q4. Find the error:
cout << Welcome;Q5. Predict the output:
cout << "C++\n";
cout << "Programming";⭐ 4.12 Answer Key
A1.
cout << "My name is ______";A2.
It allows us to use cout and cin.
A3.
It ends the program successfully.
A4.
Missing quotation marks.
Correct:
cout << "Welcome";A5.
C++
Programming