blog.dopana

Back

This is the first post in a series about basic C++. Every serious C++ journey starts with one small program that prints a line of text.

Why C++?#

C++ is a compiled, statically typed, general-purpose language that gives you direct control over memory while staying expressive. It powers operating systems, game engines, browsers, databases, and high-performance applications. Learning it teaches you how computers actually work.

Install a compiler#

To run C++ you need a compiler. On Linux or Windows with WSL, install GCC:

sudo apt install g++ # Debian / Ubuntu
bash

On macOS, install Xcode Command Line Tools:

xcode-select --install
bash

Verify it works:

g++ --version
bash

Your first program#

Create a file named hello.cpp:

hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    return 0;
}
cpp

Compile and run#

g++ -std=c++17 -Wall -o hello hello.cpp
./hello
bash

Output:

Hello, C++!
text

What each part does#

  • #include <iostream> — brings in the input/output stream library so std::cout works.
  • int main() — every C++ program starts executing from main. It must exist exactly once.
  • std::cout << — prints text to the console. \n is a newline.
  • return 0; — signals that the program finished successfully.
  • -std=c++17 — uses the C++17 standard; -Wall enables useful warnings.

Try changing the message, adding a second std::cout line, or using std::endl. Then compile again. The cycle of edit, compile, run, and observe is how you learn C++.

Conclusion#

You have compiled and run your first C++ program. Next in this series: variables and data types.

References#