C++ Programming Tutorial
Learn modern C++ step-by-step — from basics to advanced features like templates, STL, and smart pointers. Clear explanations with practical, runnable examples.
Hello World in C++
The Hello World program is the simplest complete C++ program. It demonstrates the basic structure every C++ program follows: including headers, defining the main function, and using the standard output stream.
The Program
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Line-by-Line Explanation
| Line | Code | Explanation |
|---|---|---|
| 1 | #include <iostream> | Preprocessor directive that includes the Input/Output stream library. Required for std::cout and std::endl. |
| 3 | int main() { | Entry point of every C++ program. The OS calls this function when the program starts. Returns an int (0 = success). |
| 4 | std::cout << "Hello, World!" | The insertion operator << sends the string to the standard output stream (cout). |
| 4 | << std::endl; | Inserts a newline character and flushes the output buffer to ensure text appears immediately. |
| 5 | return 0; | Returns exit code 0 to the operating system, indicating successful execution. |
Variations and Alternatives
#include <iostream>
using namespace std; // allows using cout without std:: prefix
int main() {
// Using
instead of endl (faster — does not flush)
cout << "Hello, World!
";
// Multiple outputs chained together
cout << "Name: " << "C++" << ", Version: " << 17 << "
";
// C++23 std::print (modern alternative)
// std::print("Hello, {}!
", "World");
return 0;
}Understanding the Output Stream
std::cout (character output) is an object of type std::ostream. The << operator is overloaded to accept different types — strings, integers, floats, and more — making output type-safe without format specifiers.
#include <iostream>
int main() {
int age = 25;
double pi = 3.14159;
char grade = 'A';
bool passed = true;
std::cout << "Age: " << age << "
";
std::cout << "Pi: " << pi << "
";
std::cout << "Grade: " << grade << "
";
std::cout << "Passed: " << std::boolalpha << passed << "
";
// Output:
// Age: 25
// Pi: 3.14159
// Grade: A
// Passed: true
return 0;
}Reading Input with std::cin
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
std::cout << "Enter your name: ";
std::getline(std::cin, name); // reads entire line (with spaces)
std::cout << "Enter your age: ";
std::cin >> age; // reads a single value
std::cout << "Hello, " << name << "! You are " << age << " years old.
";
return 0;
}Common Mistakes
- Forgetting
#include <iostream>: Without this,std::coutis undefined. - Missing semicolons: Every statement in C++ must end with
;. - Using
coutwithoutstd::: Either addusing namespace std;or always writestd::cout. - Mixing
cin >>andgetline: Aftercin >>, a newline remains in the buffer. Usecin.ignore()beforegetline.
endl vs
std::endl outputs a newline AND flushes the buffer (forces output to display). "
" just outputs a newline without flushing. For performance-sensitive code (loops with many outputs), prefer "
". Use std::endl when you need guaranteed immediate output (debugging, interactive prompts).
Keep Practicing
Use the online compiler to run every example and experiment with modifications. The best way to learn C++ is by writing code — even small programs build strong foundations.