Your First C++ Program
#include <iostream>
int main() {
std::cout << "Welcome to C++ Programming!\n";
return 0;
}
Welcome to C++ Programming!
Setting Up Your Development Environment
To start coding in C++, you'll need: - A modern code editor (e.g., Visual Studio Code, Sublime Text) or an IDE (e.g., Visual Studio, CLion) - A C++ compiler: - Windows: MinGW-w64 (GCC) or MSVC (via Visual Studio / Build Tools) - macOS: Apple Clang (via Xcode Command Line Tools) - Linux: GCC from your distro's packages For beginners, Visual Studio Code is a great choice since it's lightweight, free, and has strong extension support (install the C/C++ extension).
Installing Essential Tools
Windows Users: 1. Option A (recommended for GCC): Install MSYS2 and then `pacman -S mingw-w64-ucrt-x86_64-gcc`; add the corresponding `bin` folder to PATH 2. Option B (MSVC): Install Visual Studio (Desktop development with C++) or Microsoft C++ Build Tools 3. Install Visual Studio Code and the C/C++ extension macOS Users: 1. Install Xcode Command Line Tools: `xcode-select --install` 2. Confirm with `clang++ --version` (or `g++ --version`, which typically points to Apple Clang) Linux Users: 1. Use your package manager to install development tools 2. Example on Ubuntu/Debian: `sudo apt install build-essential`
Creating Your First Project
Steps to create a project: 1. Make a new folder for your project 2. Open that folder in Visual Studio Code 3. Create a file named `hello.cpp` 4. Add the sample C++ program code from above 5. Save the file (Ctrl+S on Windows/Linux, Cmd+S on macOS)
hello.cpp
#include <iostream>
int main() {
// Print a message
std::cout << "C++ is powerful!\n";
// Perform a simple calculation
int result = 5 + 3;
std::cout << "5 + 3 = " << result << "\n";
return 0;
}
Compiling and Running
To build and run your program: 1. Open the integrated terminal in Visual Studio Code (Ctrl+`) 2. Compile with GCC/Clang: `g++ -std=c++17 -Wall -Wextra -O2 hello.cpp -o hello` 3. Run the program: - Windows (PowerShell/CMD): `.\hello.exe` - macOS/Linux: `./hello` MSVC (cl.exe) alternative on Windows: - Compile: `cl /EHsc hello.cpp` - Run: `.\hello.exe` The `-o` option sets the output name. Without it, compilers typically produce `a.out` on Unix-like systems or `a.exe` on Windows.
Expected Output
C++ is powerful! 5 + 3 = 8