Introduction to Programming and C++ Environment
From the c++ curriculum
TL;DR
Programming is giving a computer step-by-step instructions to solve problems, and C++ is a powerful language we'll use for that. You'll write your C++ code, then a special program called a compiler will translate it into something your computer can understand and run. Setting up your C++ environment means installing the necessary tools like a compiler and an editor so you can start coding.
1. The Mental Model
Think of programming as writing a recipe for a very literal chef (the computer). You have to be super specific about every single step. C++ is one of many languages you can use to write these recipes.
2. The Core Material
What is Programming?

Photo by Myburgh Roux on Pexels
At its heart, programming is about telling a computer what to do. Computers are incredibly fast at following instructions but incredibly bad at guessing what you mean. So, you write a sequence of precise instructions, called code, in a language a computer can eventually understand.
What is C++?

Photo by Ann H on Pexels
C++ is a powerful, high-performance programming language. It's used for everything from operating systems (like Windows or macOS) and game engines to web browsers and financial applications. It gives you a lot of control over your computer's resources, which is why it's so popular for performance-critical tasks.
The Programming Workflow

Photo by Godfrey Atima on Pexels
When you write C++ code, you don't write it in a language the computer can directly execute. Computers understand very low-level instructions (machine code). So, there's a process to turn your human-readable C++ code into machine code.
Here's the typical workflow:
graph TD
A["Write C++ Code (.cpp files)"] --> B["Compile (g++)"]
B --> C{Compilation Successful?}
C -- Yes --> D["Link (g++)"]
D --> E{Linking Successful?}
E -- Yes --> F["Executable Program (.exe, a.out)"]
E -- No --> G["Error Messages (Go back to A)"]
C -- No --> G
F --> H["Run Program"]
- Write Code: You'll use a text editor or an Integrated Development Environment (IDE) to write your C++ code. Your C++ source files typically end with a
.cppextension (e.g.,main.cpp). - Compile: A compiler is a program that translates your C++ code into an intermediate form called object code. It also checks for syntax errors (like typos or grammar mistakes in your C++ code). If there are errors, it'll tell you, and you'll have to fix them. A common C++ compiler is
g++. - Link: After successful compilation, a linker combines your object code with any necessary libraries (pre-written code that your program uses, like functions for displaying text on the screen) to produce a single, executable program.
- Run: Once linked, you have an executable file (e.g.,
myprogram.exeon Windows, ora.outon Linux/macOS) that your computer can finally run.
Setting Up Your C++ Environment

Photo by RealToughCandy.com on Pexels
To start coding in C++, you need two main things:
- A C++ Compiler: This is the tool that translates your code. We'll typically use
g++(part of GNU Compiler Collection, GCC). - A Text Editor or IDE: This is where you'll write your code.
- Text Editor: Simple programs like VS Code, Sublime Text, or Notepad++ are good for writing code. You'd then use the command line to run the compiler.
- IDE (Integrated Development Environment): These are all-in-one solutions that combine a text editor, compiler, debugger, and other tools into a single program. Popular choices include VS Code (with C++ extensions), Code::Blocks, or Visual Studio. For beginners, VS Code or Code::Blocks are good starting points.
How to get g++ (GCC)
- Windows: The easiest way is to install MinGW-w64. This provides a full GCC environment, including
g++, for Windows. You can often find installers online or it's included with some IDEs like Code::Blocks. - macOS: GCC is usually available by installing Xcode Command Line Tools. Open your Terminal and run:
xcode-select --install. - Linux (Ubuntu/Debian-based): Open your Terminal and run:
sudo apt update && sudo apt install build-essential.build-essentialincludesg++.
Once installed, you can open a terminal/command prompt and type g++ --version to check if it's working.
3. Worked Example
Let's write a super simple C++ program, compile it, and run it.
- Create a file: Open your text editor or IDE and create a new file named
hello.cpp. -
Write the code: Type the following code into
hello.cpp:```cpp
include
// This line includes a library for input/output operations int main() { // The 'main' function is where your program starts executing
std::cout << "Hello, C++ World!" << std::endl; // This prints text to the console
return 0; // Indicates that the program finished successfully
}
``` -
Save the file: Save
hello.cppin a convenient location (e.g., a folder calledcpp_projects). - Open a terminal/command prompt: Navigate to the folder where you saved
hello.cppusing commands likecd. For example, if you saved it inC:\Users\YourName\Documents\cpp_projects, you'd typecd C:\Users\YourName\Documents\cpp_projects. -
Compile: Type the following command and press Enter:
bash g++ hello.cpp -o hello
*g++: Invokes the C++ compiler.
*hello.cpp: Is the source file you want to compile.
*-o hello: Tells the compiler to name the output executable filehello(orhello.exeon Windows).If there are no errors, you won't see any output. A new file named
hello(orhello.exe) will appear in your folder. -
Run: Type the appropriate command and press Enter:
- Windows:
.\hello.exe - macOS/Linux:
./hello
You should see
Hello, C++ World!printed in your terminal. - Windows:
4. Key Takeaways
- Programming means writing precise, step-by-step instructions for a computer to follow.
- C++ is a powerful, high-performance language used in many different types of software.
- Your C++ code is human-readable, but a compiler (like
g++) translates it into machine code. - The typical workflow involves writing code, compiling it, linking it, and then running the executable.
- A C++ environment requires a compiler (e.g.,
g++) and a text editor/IDE (e.g., VS Code). - The
mainfunction is the starting point of every C++ program. std::coutis used to print output to the console.
Common Mistakes to Avoid:
- Forgetting to save your changes before compiling can lead to compiling an older version of your code.
- Syntax errors are common, especially early on; the compiler will tell you about them, so read the messages carefully.
- Incorrect compiler or executable names when trying to compile or run (g++ vs. gpp, ./a.out vs. hello).
- Not installing the compiler correctly or not having it in your system's PATH, which means your terminal won't find g++.
- Mixing up Windows and Linux/macOS commands for running executables (.\hello.exe vs ./hello).
5. Now Try It
Your task is to modify the hello.cpp program.
1. Change the message to something personal, like "My name is [Your Name], and I'm learning C++!".
2. Add another line of code using std::cout that prints "This is exciting!".
3. Compile and run your modified program.
Success looks like: You should see two lines of text printed in your terminal, with your custom messages, after successfully compiling and running your updated code.
Frequently asked about Introduction to Programming and C++ Environment
Study this next
Get the full c++ curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free