No history yet

C++ Basics

The Building Blocks of C++

Every C++ program, no matter how complex, is built from a few fundamental pieces. Let's start with the classic first program everyone writes: one that simply displays "Hello, World!" on the screen. It's a small piece of code, but it shows us the basic structure we'll use again and again.

#include <iostream>

int main() {
    // This line prints "Hello, World!" to the console.
    std::cout << "Hello, World!" << std::endl;
    
    // This tells the operating system the program ran successfully.
    return 0;
}

Let's break this down:

  • #include <iostream>: This is a preprocessor directive. It tells the compiler to include the iostream file, which contains the code that allows us to get input from and send output to the screen.
  • int main(): This is the main function. Every C++ program must have one. When you run your program, the code inside the curly braces {} of main is what gets executed first.
  • `std::cout <<