1.0 What This Teaches
Every language starts with a working program you can read, compile, and run.
This tutorial covers:
- The structure of a minimal C++ source file
- The
#include directive and standard headers
- The
main function as the program entry point
std::cout for output and std::cin for input
- Compiling with g++ or clang++
1.1 Your First Program
// hello.cpp - first C++ program: print a greeting and read a name.
#include <iostream>
#include <string>
int main() {
std::cout << "Hello, World!\n";
std::cout << "Enter your name: ";
std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
return 0;
}
Save this as hello.cpp. Every statement ends with a semicolon.
The file compiles to a native executable with no runtime required.
1.2 The #include Directive
#include <header> pastes the contents of a standard library
header into your file before compilation.
| Header | Provides |
| <iostream> | std::cout, std::cin, std::cerr |
| <string> | std::string, std::getline |
Angle brackets mean a standard library header. Double quotes mean a local header
in your project: #include "myheader.h".
1.3 The main Function
main is the entry point. The OS calls it when the program starts.
It returns an int: 0 means success, any other value signals an error.
int main() {
// program body
return 0; // 0 = success
}
The return 0; is implicit at the end of main if omitted,
but writing it is good practice.
1.4 std::cout and the << Operator
std::cout is the standard output stream. The <<
operator sends values to it. Calls chain left to right:
std::cout << "name: " << name << ", age: " << 30 << "\n";
"\n" is a newline character. std::endl also writes a
newline but additionally flushes the buffer - use "\n" for
performance unless an immediate flush is required.
1.5 Compiling and Running
g++ -std=c++17 -Wall -o hello hello.cpp
./hello
| Flag | Meaning |
| -std=c++17 | Use the C++17 standard |
| -Wall | Enable common warnings |
| -o hello | Name the output executable |
| -O2 | Optimize for speed (release builds) |
On Windows the executable is hello.exe; run it with
.\hello in PowerShell.
1.6 Example - All Together
// hello.cpp - first C++ program: print a greeting and read a name.
#include <iostream>
#include <string>
int main() {
std::cout << "Hello, World!\n";
std::cout << "Enter your name: ";
std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
return 0;
}
Expected output (with input "Alice"):
Hello, World!
Enter your name: Alice
Hello, Alice!
1.7 Exercise
Exercise
- Modify the program to ask for first and last name separately, then print
a greeting using both.
- Print the total character count of the full name using
name.length().
- Use
std::cerr << "error\n"; to write to stderr and
observe that it appears separately from stdout when redirected.
1.8 Common Mistakes
Missing semicolon
std::cout << "Hello" // error: expected ';'
std::cout << "World";
Every statement ends with ;. The compiler error often points
to the line after the missing one.
Forgetting #include
int main() {
std::cout << "Hello\n"; // error: 'cout' not a member of 'std'
}
Add #include <iostream> at the top.
Using std::endl in a tight loop
std::endl flushes the buffer on every call. In a loop this is
much slower than "\n". Use "\n" unless you explicitly
need a flush.
1.9 Key Terms
| Term | Meaning |
| #include | Preprocessor directive: pastes a header's contents before compilation |
| <iostream> | Header providing std::cout, std::cin, std::cerr |
| std::cout | Standard output stream; use << to send values |
| std::cin | Standard input stream; use >> or getline to read |
| std::getline | Reads a full line including spaces into a std::string |
| main | Required entry point; returns int (0 = success) |
| std::endl | Newline + buffer flush; slower than "\n" in loops |
| -std=c++17 | Compiler flag enabling C++17 language features |