Chapter 3: Your First C++ Program
Now that you have installed a C++ development environment, it's time to write your first C++ program.
Traditionally, every programmer begins with the Hello, World! program. Although it is simple, it introduces the basic structure of every C++ application.
Hello, World! Program
Create a new file named hello.cpp and write the following code:
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Hello, World!" << endl; 8 9 return 0; 10}
Output
1Hello, World!
This program displays the text Hello, World! on the screen.
Understanding the Program
Let's examine each line in detail.
1. Header Files
1#include <iostream>
The #include directive tells the compiler to include another file before compilation.
<iostream> is a header file that provides functionality for input and output operations.
Without including this header, objects such as cout, cin, and endl are unavailable.
Why is it needed?
1#include <iostream> 2 3int main() 4{ 5 std::cout << "Hello"; 6}
Without <iostream>, the compiler will report an error because it doesn't know what std::cout is.
Common C++ Header Files
| Header | Purpose |
|---|---|
<iostream> | Input and output |
<string> | String operations |
<vector> | Dynamic arrays |
<cmath> | Mathematical functions |
<fstream> | File handling |
<algorithm> | Standard algorithms |
<map> | Key-value containers |
<set> | Unique element containers |
2. using namespace std;
1using namespace std;
The C++ Standard Library places its features inside the std namespace.
Instead of writing:
1std::cout << "Hello";
you can write:
1cout << "Hello";
because of:
1using namespace std;
Without using namespace std
1#include <iostream> 2 3int main() 4{ 5 std::cout << "Hello, World!"; 6 return 0; 7}
With using namespace std
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Hello, World!"; 8 return 0; 9}
Best Practice: In large projects, prefer using
std::coutinstead ofusing namespace std;to avoid naming conflicts.
3. The main() Function
1int main() 2{ 3}
Every C++ program starts execution from the main() function.
It is the entry point of the application.
The operating system calls this function when the program begins.
Syntax
1int main() 2{ 3 // Program statements 4}
Program Flow
Program Starts
│
▼
main()
│
▼
Statements Execute
│
▼
Program Ends
Only one main() function is allowed in a C++ program.
4. Curly Braces { }
1int main() 2{ 3 // Statements 4}
Curly braces define a block of code.
Everything between { and } belongs to the main() function.
Example:
1int main() 2{ 3 int age = 20; 4 5 cout << age; 6 7 return 0; 8}
5. cout
1cout << "Hello, World!";
cout stands for Character Output.
It is used to display output on the console.
Syntax
1cout << value;
Example
1cout << "Welcome";
Output
1Welcome
Printing Numbers
1cout << 100;
Output
1100
Printing Variables
1int age = 22; 2 3cout << age;
Output
122
Printing Multiple Values
1string name = "Ankit"; 2int age = 23; 3 4cout << name << " " << age;
Output
1Ankit 23
The << operator is called the stream insertion operator.
It sends data to the output stream.
6. endl
1cout << "Hello" << endl;
endl inserts a new line and flushes the output buffer.
Example:
1cout << "One" << endl; 2cout << "Two";
Output
1One 2Two
You can also use the newline character:
1cout << "One\n"; 2cout << "Two";
Output
1One 2Two
endl vs \n
endl | \n |
|---|---|
| Adds a new line | Adds a new line |
| Flushes the output buffer | Does not flush the buffer |
| Slightly slower | Faster |
For most applications, \n is preferred for better performance unless you specifically need to flush the output.
7. return 0;
1return 0;
The return statement ends the main() function and returns a value to the operating system.
A return value of 0 indicates that the program executed successfully.
Example
1int main() 2{ 3 cout << "Done"; 4 5 return 0; 6}
Output
1Done
Return Codes
| Return Value | Meaning |
|---|---|
0 | Program completed successfully |
| Non-zero | Program ended with an error or specific status |
Complete Program Flow
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Hello, World!" << endl; 8 9 return 0; 10}
Execution order:
- The compiler includes the
iostreamheader. - The operating system calls
main(). coutprints Hello, World!.endlmoves the cursor to the next line.return 0ends the program successfully.
Common Beginner Mistakes
Missing Semicolon
Incorrect:
1cout << "Hello"
Correct:
1cout << "Hello";
Missing Header File
Incorrect:
1int main() 2{ 3 cout << "Hello"; 4}
Correct:
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Hello"; 8}
Misspelling main
Incorrect:
1int Main() 2{ 3}
Correct:
1int main() 2{ 3}
Missing return
Although modern C++ allows omitting return 0; in main(), explicitly including it improves readability.
1int main() 2{ 3 cout << "Hello"; 4 5 return 0; 6}
Practice Exercises
Exercise 1
Print your name.
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Ankit"; 8 9 return 0; 10}
Exercise 2
Print two lines.
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Welcome to C++" << endl; 8 cout << "Happy Coding!"; 9 10 return 0; 11}
Exercise 3
Print your age.
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Age: 23"; 8 9 return 0; 10}
Summary
In this chapter, you learned:
- How to write your first C++ program.
- The purpose of the
#includedirective and header files. - Why
using namespace std;is used. - The role of the
main()function as the program's entry point. - How
coutdisplays output to the console. - The difference between
endland\n. - Why
return 0;signals successful program execution. - Common beginner mistakes and how to avoid them.
With this foundation, you're ready to explore variables, data types, input/output, and other core C++ concepts in the next chapter.