C++ Variables and Fundamental Types
Store meaningful values with deliberate types, initialization, const, and type inference while avoiding uninitialized data and misleading conversions.
Before this lesson
Choose common types for numbers characters and Boolean values
Distinguish initialization from later assignment
Use const and auto without losing intent
The short answer
A variable gives a typed value a meaningful name. Initialize variables when they are created, use const when a value must not change, and choose types from the values and operations the program actually requires.
Types define valid values and operations
A type tells the compiler how a value is represented and which operations make sense. int is a common signed integer type, double represents floating-point values, bool represents true or false, and char stores a character-sized code unit. std::string is a standard-library type for owned text.
Choose a type from domain meaning. A count should not quietly accept fractional values. Currency calculations should not assume binary floating point is exact. The type is part of the program's contract, not only a storage-size decision.
| Need | Typical type | Important question |
|---|---|---|
| Count or index | int or a suitable unsigned/index type | Can it be negative or very large? |
| Measurement | double | Is rounding acceptable? |
| True/false state | bool | Is this really only two states? |
| Owned text | std::string | Who owns and changes the text? |
Initialize before use
Initialization gives an object its first value; assignment replaces a value later. Prefer declaring a variable where its first meaningful value is known. int attempts{0}; is safer and clearer than declaring attempts early and hoping every control-flow path assigns it.
Brace initialization also catches narrowing. int whole{3.8}; is rejected instead of silently discarding the fraction. This does not prevent every conversion problem, but it makes object creation more deliberate.
#include <iostream>
int main()
{
int completed{4};
double target_hours{7.5};
bool on_schedule{completed >= 4};
std::cout << completed << " tasks, "
<< target_hours << " hours, on schedule: "
<< std::boolalpha << on_schedule << '\n';
}Const expresses a promise
Mark a variable const when it should not change after initialization. The compiler will reject later assignment, preventing accidental edits and helping readers distinguish fixed inputs from evolving state. Configuration loaded at runtime can still be const after loading; const does not mean compile-time-only.
Use meaningful names rather than unexplained literals. const int minutes_per_hour{60}; communicates the rule once. Do not turn every temporary into a global constant; keep names in the narrowest useful scope.
Auto infers a static type
auto total = quantity * unit_price_cents; asks the compiler to infer the type from the initializer. It is valuable when the type is obvious or verbose, especially with iterators. It is harmful when the inferred type hides an important unit, signedness, reference, or conversion.
Inference does not remove types. auto value = 3; is an int, so assigning 3.5 later converts to int rather than changing the variable's type. Inspect the initializer and use an explicit type when it better states the contract.
Quick knowledge check
Answer before you reveal.
01Why is int count; risky inside a function?
A local fundamental variable declared that way has an indeterminate value. Reading it before assignment produces undefined behavior.
02Does auto make C++ dynamically typed?
No. The compiler infers one static type from the initializer, and the variable keeps that type.
Exercise
Practice challenge
Model a product with a name, quantity, unit price in cents, and availability flag. Print a readable summary and update only the quantity.
Requirements
- Every variable is initialized at its declaration
- The product name and unit price cannot be reassigned
- The output labels every value clearly
Optional extension: Use auto for one variable whose inferred type remains obvious.
Open in C++ compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Should every number use double?
No. Counts are commonly integral, money often needs an exact scaled representation, and measurements may need floating point.
What is brace initialization for?
Braces initialize an object and reject several narrowing conversions that older assignment-style syntax accepts silently.
Is const only an optimization?
No. Its primary value is a checked design promise that the named object will not be modified through that declaration.