Declaring Variables and Constants in C++
A declaration introduces a name and its type; a definition also creates the associated object.
Summary
A declaration introduces a name and its type; a definition also creates the associated object. For example, extern int count; can declare an object defined elsewhere, while int count{5}; defines and initializes an object. Initialization supplies an initial value; assignment, such as count = 8;, changes an existing object's value.
Choose an appropriate type and initialize each object before use. An uninitialized local built-in variable such as int count; has no usable initial value, while int count{}; initializes it to zero. Brace initialization rejects narrowing conversions: int count{3.5}; is ill-formed.
| Specifier | Main requirement | Key distinction |
|---|---|---|
| const | A simple scalar constant requires initialization | Prevents modification; initialization may occur at runtime |
| constexpr | Requires a constant-expression initializer | Declares a const object suitable for constant-expression contexts |
Compile-time constants support fixed array bounds, template arguments, and other contexts requiring constant expressions. The auto specifier deduces a variable's type from its initializer: auto count = 10; deduces int, while auto value = 3.5; deduces double.
Common Misconceptions: A declaration does not always create an object. A local built-in variable is not automatically initialized to zero. A const initializer need not be evaluated at compile time.
🧠 Key Concepts
- Variable declaration
- Object definition
- Initialization
- Assignment
- Brace initialization
- Narrowing conversions
- Const qualification
- Constant expressions
- Type deduction
🧠 Quick Check
See what you remember from the summary.
Which best describes extern int count; when count is defined elsewhere?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Declaring Variables and Constants in C++
📘 Overview A C++ variable declaration introduces a name and its type; a definition creates the associated object. Initialization gives that object its initial value, while const and constexpr express different restrictions on modification and compile-time evaluation.
🧠 Key Idea Declare each object with an appropriate type and initialize it before use. Use const for an object that must not be modified and constexpr for a constant that must be initialized by a constant expression.
⚔️ Core Details: - A typical variable definition specifies a type, an identifier, and an optional initializer: int count = 10;. - Brace initialization rejects narrowing conversions: int count{10}; is valid, but int count{3.5}; is ill-formed. - An uninitialized local built-in variable such as int count; has no usable initial value; int count{}; initializes it to zero. - For a simple scalar constant, const requires an initializer and prevents later modification: const int limit{100};. - A constexpr variable must have a constant-expression initializer: constexpr int capacity{64};. - The auto specifier deduces a variable's type from its initializer: auto count = 10; deduces int.
🎯 Why It Matters: - Explicit initialization prevents unpredictable behavior caused by reading local variables before they hold valid values. - Const-qualified objects make intended invariants visible and let the compiler reject accidental modification. - Compile-time constants support fixed array bounds, template arguments, and other contexts that require constant expressions. - Choosing suitable types and rejecting narrowing conversions helps prevent data loss and conversion errors.
🧠 Quick Recall: - Declaration versus definition - A declaration introduces an entity; a definition also creates the object. extern int count; can declare an object defined elsewhere. - Initialization versus assignment - int count{5}; creates and initializes an object; count = 8; changes an existing object's value. - const - Prevents modification through that const-qualified object; its initializer may be evaluated at runtime. - constexpr - Declares a const object whose initializer must be a constant expression. - auto - Deduces a variable's type from its initializer; auto value = 3.5; gives value the type double.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.