Lesson 1 of 13

Hello, World!

Write and run your first C++ program, and see how printing with cout differs from C's printf.

C++
#include <iostream>
#include <string>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

cout is the standard output stream. << is the stream insertion operator — it "inserts" data into the stream. endl flushes the buffer and adds a newline (use "\n" instead for performance in tight ). std:: is the .

C++
#include <iostream>
#include <string>
using namespace std;  // now you can write cout instead of std::cout

int main() {
    cout << "Hello, World!" << "\n";

    string name = "Alice";
    int age = 30;

    // Chain multiple insertions
    cout << "Name: " << name << ", Age: " << age << "\n";

    // endl vs "\n"
    // endl: flushes buffer (slower, use when you need immediate output)
    // "\n": just a newline (faster)
    cout << "Fast newline\n";
    cout << "Flushed output" << endl;

    // Input with cin
    // cout << "Enter your name: ";
    // string input;
    // cin >> input;   // reads one word
    // getline(cin, input);  // reads full line

    return 0;
}
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.