Introduction to C++ & Your First Program
What Is C++?
C++ is a compiled, statically-typed, general-purpose programming language. It was created by Bjarne Stroustrup starting in 1979 at Bell Labs, originally as an extension of the C language called "C with Classes." The name was changed to C++ in 1983 — the ++ is a nod to C's own increment operator, meaning "one step beyond C."
C++ gives you two things that many modern languages trade away for convenience: direct control over memory and hardware, and compiled performance close to the metal. That combination is exactly why it's still the language of choice whenever raw speed and predictable resource usage matter.
Where C++ Is Used
C++ shows up wherever a program needs to run fast, use memory efficiently, and talk closely to hardware or an operating system. That's a wide net — from the engine rendering a video game frame to the firmware inside a microcontroller.
Compiled vs. Interpreted — C++ vs. Python
If you've come from BitWithBite's Python course, this is the biggest mental shift you'll make. Python is interpreted — the Python interpreter reads your .py file and executes it line by line, on the spot. C++ is compiled — a separate program (the compiler) translates your entire .cpp file into a standalone machine-code executable before anything runs.
.cpp extension containing human-readable C++ code.g++ or clang++) checks your syntax and types, then translates the whole file into native machine code — an executable file.Installing a C++ Compiler
To turn C++ source code into a runnable program, you need a compiler. The two most common ones are g++ (part of the GNU Compiler Collection) and clang++ (the LLVM project's compiler). Either is a fine choice for this course.
🪟 Windows
g++ directly on Windows. Alternatively, install the Windows Subsystem for Linux (WSL) and use a Linux distribution's package manager.g++ --version. You should see version information printed back.🍎 macOS
xcode-select --install. This installs Apple's Clang compiler.clang++ --version in Terminal to confirm it's ready.🐧 Linux
# Update package list sudo apt update # Install the GNU C++ compiler sudo apt install g++ # Verify installation g++ --version
🌐 No installation? Use an online compiler
If you just want to try code snippets without installing anything yet, any browser-based C++ compiler works fine for following along with this course — you type code, click run, and see output immediately. You'll still want a real local compiler once you start building larger programs.
Your First C++ Program — Hello, World!
Every C++ program starts life as a .cpp text file. Let's write the classic first program and then compile and run it.
// hello.cpp // My first C++ program #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
Line by line
#include <iostream>std::cout (console output) lives. Without this line, the compiler has no idea what cout means.int main() { ... }main function — it's the entry point. Execution always starts at the first line inside main's curly braces. The int means main will hand back a whole number when it finishes.std::cout << "Hello, World!" << std::endl;std::cout is the standard output stream. The << operator "inserts" values into that stream — here, the text "Hello, World!", followed by std::endl, which prints a newline and flushes the output.return 0;main and hands the value 0 back to the operating system. By convention, 0 means "the program finished successfully"; any non-zero value signals an error occurred.std::?std is the standard namespace — the container that holds every piece of the C++ Standard Library, including cout, cin, string, and vector. Writing std::cout tells the compiler exactly which cout you mean, avoiding naming clashes with your own code.Compiling and running hello.cpp
Save the file, open a terminal in the same folder, and run these two commands:
# Step 1: compile hello.cpp into an executable named "hello" g++ hello.cpp -o hello # Step 2: run the executable ./hello # macOS / Linux hello.exe # Windows # Output: # Hello, World!
#include will stop compilation with an error message pointing at a line number. This is normal — reading compiler errors is a skill you'll build quickly. No executable is produced until the code compiles cleanly.A Second Program — Reinforcing Compile & Run
Let's extend the pattern: print a few lines, then do a tiny calculation and print its result too.
// welcome.cpp // Prints a short welcome, then adds two numbers #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; std::cout << "Welcome to C++." << std::endl; int a = 7; int b = 5; std::cout << "7 + 5 = " << (a + b) << std::endl; return 0; }
Compiling this works exactly the same way: g++ welcome.cpp -o welcome, then ./welcome. The output prints three lines — the two greetings, then 7 + 5 = 12. Notice that a and b are declared with a type (int) before they're used — you'll dig deeper into this in Lesson 2.
.cpp file does nothing to an executable you already built. Every time you change the source, you must re-run the compile step before your changes show up when you execute the program again.Lesson Summary
Let's recap everything you learned in this lesson:
g++ or clang++ — installed and on your PATH before you can build programs.#include <iostream>, int main(), std::cout <<, and return 0; are the four pillars of a minimal C++ program.g++ file.cpp -o program, then run the resulting executable directly.std::cout?return 0; at the end of main() signal?Now it's your turn to write real code. Compile and run it with g++ or an online compiler.
Write a program called
about_me.cpp that prints your name on the first line, and a short 3-line bio below it — for example, where you're from, what you're learning, and a goal. Something like:
I'm learning C++ on BitWithBite.
I'm from Pakistan.
My goal is to build fast, efficient software.
Rules: Use only
std::cout statements inside int main(). Include #include <iostream> at the top and end with return 0;. Add at least one comment explaining what the program does.
💡 Show hints if you're stuck
- Each line needs its own
std::cout << "..." << std::endl;statement - Don't forget the semicolon
;at the end of every statement - Compile with
g++ about_me.cpp -o about_me, then run./about_me - Start your file with a comment:
// About Me — my first C++ challenge