Module 2 · Variables, Operators and Control Flow — topic 1 of 4
Variables and Data Types
Fundamental types, bool, std::string, the auto keyword, const and constexpr, and the brace initialisation that prevents narrowing conversions.
Learning objectives
- Declare variables using several initialisation styles
- Use auto, const and constexpr appropriately
- Explain how brace initialisation prevents narrowing
Definition
A variable is a named object of a specific type. C++ provides fundamental types (bool, char, int, long long, float, double…), library types such as std::string, and lets the compiler deduce a type with auto.Introduction
C++ keeps C's numeric types and adds a real bool type with the literals true and false, plus library types like std::string that behave like built-in values.
C++11 introduced auto, which deduces a variable's type from its initialiser, and brace initialisation int x{5};, which works uniformly for every type and rejects accidental data loss.
Why it matters
Choosing the right type and initialising every variable removes many bugs at compile time.
const and constexpr let the compiler enforce that values do not change and even compute them at compile time.
Detailed explanation
Initialisation styles: int a = 5; (copy), int b(5); (direct), int c{5}; (brace / list). Brace initialisation is preferred because int d{3.7}; is a compile error — the double would be narrowed — whereas int d = 3.7; silently truncates.
auto x = 3.0; makes x a double; auto s = std::string("hi"); a std::string. auto still gives a static type — it is decided at compile time.
const int max = 100; cannot change after initialisation. constexpr int size = 10 * 4; must be computable at compile time and can be used where constants are required, such as array sizes.
Syntax
- Use
{}for most initialisation. autorequires an initialiser.constexprimpliesconst.
Code examples
Types and sizes
true A 92 8.75 1 4 8
std::boolalpha prints booleans as words instead of 1/0.
auto deduces the type
7 3.5 3
More examples
constexpr and narrowing
36 36 7
Step-by-step explanation
constexpr int square(int)can run at compile time when its argument is constant.areais 36 and is known before the program runs, so it can size an array.static_cast<int>(avg)makes the truncation explicit.- The commented line would not compile: braces forbid narrowing.
Expected output
Running “constexpr and narrowing” prints exactly:
Practical applications
- Embedded C++ uses constexpr tables computed at compile time to save run-time work.
- Game code uses
autofor long iterator and lambda types. - Libraries mark configuration values
constexprso they cost nothing at run time.
Best practices
- Initialise every variable, preferably with braces.
- Use
autowhen the type is obvious from the right-hand side or very long. - Use
constby default and drop it only when the value must change.
Common mistakes
Silent truncation with =
Avoid
Prefer
Copy-initialisation silently drops .6; braces would refuse, forcing you to make the conversion explicit.
auto with braces in C++11
Avoid
Prefer
auto x = {5}; deduces an initializer_list, not an int.
Important notes
- Signed integer overflow is undefined behaviour in C++ just as in C.
std::stringis not a fundamental type but behaves like a value: copying it copies the characters.
Key takeaways
- Brace initialisation prevents narrowing.
autodeduces a static type from the initialiser.constexprvalues are computed at compile time.
Try it yourself
- 1Declare variables of five different types using brace initialisation and print them.
- 2Write a
constexprfunctioncubeand use it to size an array.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
8 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is the result of compiling `int x{3.7};`?
- BeginnerWhat is printed?
- BeginnerWhat is printed?