Site

Hello — Your First C++ Program

Tutorial 1.0  •  C++ / Learn

1.0 What This Teaches

Every language starts with a working program you can read, compile, and run. This tutorial covers:

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.
HeaderProvides
<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
FlagMeaning
-std=c++17Use the C++17 standard
-WallEnable common warnings
-o helloName the output executable
-O2Optimize 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

TermMeaning
#includePreprocessor directive: pastes a header's contents before compilation
<iostream>Header providing std::cout, std::cin, std::cerr
std::coutStandard output stream; use << to send values
std::cinStandard input stream; use >> or getline to read
std::getlineReads a full line including spaces into a std::string
mainRequired entry point; returns int (0 = success)
std::endlNewline + buffer flush; slower than "\n" in loops
-std=c++17Compiler flag enabling C++17 language features