Getting Started
Your First C++ Program
Definition: Getting started in C++ involves configuring a tool chain—consisting of a text editor, a compiler (like GCC or Clang), or an Integrated Development Environment (IDE) like VS Code or Code::Blocks—to translate your human-readable source code into computer-executable files.
Learning Outcome: Setting up your workspace, writing your first program, and executing it is the introductory milestone of any standard C++ curriculum. This module establishes a fundamental understanding of how source code is compiled, linked, and executed by the operating system.
Writing the "Hello, World!" Program
The traditional starting point for learning any language is writing a program that displays a friendly message on the screen. Below is the standard structure of a minimal C++ console application:
#include <iostream> using namespace std; int main() { cout << "Hello, World!"; return 0; }
Line-by-Line Explanation
#include <iostream>: This is a preprocessor directive that imports the Input/Output Stream library. It tells the compiler to include the built-in system tools necessary for performing basic input and output operations, such as displaying text to the screen.using namespace std;: C++ organizes its massive standard libraries inside a container called a "namespace" namedstd(Standard). This line gives us direct access to names within that namespace, allowing us to write commands likecoutandcindirectly instead of writing their explicit prefixes (std::coutandstd::cin) every single time.int main(): This defines the main function. Every executable C++ application must contain amainfunction. It serves as the official entry point where the operating system begins executing your program's instructions.cout << "Hello, World!";:cout(pronounced "see-out") stands for "Character Output." It is combined with the insertion operator (<<) to send the text inside the double quotes directly out to your console screen.return 0;: This statement exits themainfunction. Returning the integer value0to the operating system is a conventional signal confirming that the program ran successfully and terminated without encountering any runtime errors.
The Compilation Cycle
| Stage | What Happens |
|---|---|
| 1. Writing | You save your source code instructions into a plain text file with a .cpp extension (e.g., main.cpp). |
| 2. Compiling | The compiler checks your code syntax. If no syntax errors are found, it converts the text into a low-level machine-readable object file (e.g., .obj or .o). |
| 3. Linking | The linker combines your object file with standard system libraries (like the iostream code) to build a standalone executable file. |
| 4. Executing | The operating system loads the final executable (e.g., a.out or program.exe) into memory and runs it, outputting the results. |
Key Notes
- Case Sensitivity: C++ is strictly case-sensitive. Writing
Cout,Main, orINCLUDEwith a capital letter will confuse the compiler and cause immediate build errors. - Semicolon Termination: In C++, semi-colons (
;) act as sentence terminators for statements. Forgetting a semicolon at the end of an instruction is one of the most common syntax errors made by beginners. - Code Readability: White spaces, line breaks, and indentation are completely ignored by the compiler, but using them consistently is critical to keeping your code clean and readable for humans.
🏋️ Test Yourself With Exercises
Take our quiz on Getting Started to test your knowledge.
Browse Quizzes »