Getting Started with C++: Writing Your First Program
C++ is one of the most powerful and widely used programming languages in the world. It's used in game engines, operating systems, embedded systems, and high-performance applications. The good news? Getting started is easier than you might think.
Step 1: Install a Compiler
Before you write a single line of C++, you need a compiler — the tool that transforms your human-readable code into machine instructions.
- Windows: Install MinGW-w64 (GCC for Windows) or download Visual Studio Community for MSVC.
- macOS: Run
xcode-select --installin Terminal to get Clang. - Linux: Run
sudo apt install g++(Debian/Ubuntu) or your distro's equivalent.
Step 2: Choose an Editor or IDE
You can write C++ in any text editor, but a good IDE makes life much easier. Popular choices include:
- VS Code — lightweight, free, excellent C++ extension available.
- CLion — full-featured JetBrains IDE, great for larger projects.
- Visual Studio — ideal on Windows, best-in-class debugger.
- Code::Blocks — simple, lightweight, and beginner-friendly.
Step 3: Write Your First Program
Create a new file called hello.cpp and type the following:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Let's break this down line by line:
#include <iostream>— This includes the input/output stream library, which gives you access tostd::coutfor printing to the console.int main()— Every C++ program must have amainfunction. This is where execution begins.std::cout << "Hello, World!"— This prints text to the standard output (your terminal).return 0;— Returns 0 to the operating system, signaling the program exited successfully.
Step 4: Compile and Run
Open your terminal in the same directory as hello.cpp and run:
g++ hello.cpp -o hello
./hello
You should see Hello, World! printed in your terminal. Congratulations — you've just compiled and run your first C++ program!
Understanding the Compilation Process
Unlike interpreted languages (Python, JavaScript), C++ is compiled. The compilation pipeline goes through these stages:
- Preprocessing — Handles
#includeand#definedirectives. - Compilation — Translates your code to assembly.
- Assembly — Converts assembly to machine code (object files).
- Linking — Combines object files and libraries into an executable.
Next Steps
Now that you have a working program, the next topics to explore are:
- Variables and data types (
int,float,std::string) - Control flow:
if,else,for,while - Functions — how to organize your code into reusable blocks
Learning C++ is a journey, but each small step builds a strong foundation. Keep experimenting, breaking things, and reading compiler errors — they're your best teacher!