What a Variable Is
A variable is a named piece of memory that holds a value. In C++ every variable has a fixed type - chosen when you declare it - and that type never changes. This is what makes C++ statically typed: the compiler knows the type of every variable before the program runs and refuses to compile code that stores the wrong kind of value.
A declaration has three parts: the type, the name, and (almost always) an initial value.
Read int age = 30; as "make an int called age and put 30 in it". The semicolon ends the statement, just like the comments page showed every statement does. Note isActive printed as 1 - bool shows as 1/0 by default, something the data types page builds on.
Initialization vs. Assignment
These two look similar but are different operations, and the difference is one of the most important ideas on this page.
Initialization gives a variable its first value as part of the declaration. Assignment changes the value of a variable that already exists.
You can declare without initializing, then assign later, but be careful:
That works because we assigned score before reading it. The dangerous version is reading it first - covered next.
The Uninitialized-Variable Gotcha
This is the classic C++ trap that does not exist in many other languages. Reading a local variable that was declared but never given a value is undefined behavior: the variable holds whatever bytes happened to be in that memory.
int score; // not initialized
std::cout << score; // undefined behavior - prints garbage, or "works", or crashes
The compiler will happily build this. It may print 0, it may print 32766, and it may behave differently every run or on every machine - which makes these bugs miserable to track down. Two defenses:
- Always initialize at declaration.
int score = 0;costs nothing and removes the entire problem. - Turn on warnings. Compiling with
-Wall -Wextramakes the compiler flag many uninitialized reads before they bite you.
Prefer the first option for every local: give it a sensible starting value the moment you create it.
Initialization Styles: =, (), and {}
C++ gives you a few ways to write an initial value. They mostly do the same thing, but brace initialization has one extra safety feature worth knowing.
The reason to reach for {} is that it rejects narrowing conversions - assignments that would silently lose data. With =, a fractional value quietly truncates; with {}, the compiler stops you:
int x = 3.9; // compiles - x silently becomes 3 (the .9 is thrown away)
int y{3.9}; // compile error - narrowing from double to int is not allowed
Catching that at compile time is exactly what you want. A common modern habit is to default to {} for that extra check, falling back to = only when copy initialization reads more naturally.
Naming Rules and Conventions
C++ enforces a few hard rules, then everyone layers conventions on top. The rules: a name may contain letters, digits, and _; it cannot start with a digit; it cannot be a reserved keyword (like int or return); and it is case-sensitive (age and Age are two different variables). Avoid names that start with an underscore followed by a capital, or that contain two underscores in a row - those are reserved for the implementation.
The conventions most C++ code follows:
- Variables use
snake_caseorcamelCase- pick one and stay consistent:item_countoritemCount. - Names should describe the value:
count, notc;user_email, notx.
Clear names are not decoration - they are how your future self reads the code. total_price = item_count * price_per_item is self-explanatory in a way t = c * p never will be.
Variable Scope
A variable exists only inside the block - the { ... } - where it is declared, and it is destroyed at the closing brace. This is its scope. A variable declared inside a loop or if block is invisible outside it:
Both i and square belong to the loop and vanish when it ends; total is declared in the outer block, so it survives. An inner block can also shadow a name from an outer block - a new variable with the same name temporarily hides the outer one until the inner block closes, which is a frequent source of confusion, so avoid reusing names across nested scopes.
The practical takeaway: declare each variable in the smallest block that needs it, and initialize it there. Narrow scope means fewer names competing for your attention and fewer chances to read a value far from where it was set.
Next: Data Types
Every variable on this page started with a type - int, double, string, bool. The next page breaks down C++'s data types in detail: the fundamental types and their sizes, signed vs. unsigned integers, float vs. double, char, and how to pick the right type for the job.
Frequently Asked Questions
How do you declare a variable in C++?
Write the type, then a name, then optionally give it a value: int age = 30;. The type (int) is fixed for the life of the variable; the name (age) is how you refer to it. You can declare without a value - int age; - but for a local variable that leaves it holding garbage until you assign one, so always initialize at the point of declaration.
What is the difference between initialization and assignment in C++?
Initialization gives a variable its first value as part of its declaration: int x = 5; or int x{5};. Assignment changes the value of an already-existing variable: x = 7;. The distinction matters because reading a local variable that was declared but never initialized is undefined behavior.
What happens if you use an uninitialized variable in C++?
Reading the value of an uninitialized local variable is undefined behavior - the variable holds whatever bytes were already in that memory, so your program might print a random number, work by luck, or crash. The compiler will not stop you (though many warn with -Wall), so the fix is to always give locals a value when you declare them.