Variables, Auto Type Inference and constexpr
Declare variables, use auto for type deduction, and learn when to mark values constexpr.
Variables, Auto Type Inference and constexpr is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Variable?
A variable is a named region of memory with a type. The type fixes how many bytes it occupies and how the bits are interpreted.
Declaring with Explicit Types
The classic form: type name = value;
int age = 30;
double pi = 3.14159;
char grade = 'A';
bool isReady = true;The auto Keyword
Since C++11, the compiler can deduce the type from the initializer. Use auto for cleaner code, especially with iterators or templates.
auto count = 42; // int
auto price = 9.99; // double
auto name = std::string("Ada"); // std::stringWhen auto Helps
Use auto when:
- The type is long (iterators, templates)
- You want to avoid implicit conversions
- The type follows directly from the initializer
When to Avoid auto
Avoid auto when the deduced type is not obvious to a reader. For numeric literals, an explicit type often documents intent better.
const: Read-Only Variables
Mark variables that should never change with const. The compiler enforces this.
const int MAX_USERS = 100;
const double TAX_RATE = 0.18;constexpr: Compile-Time Constants
constexpr goes further than const — the value must be known at compile time. The compiler can use it where compile-time constants are required.
constexpr int BUFFER_SIZE = 1024;
constexpr double PI = 3.14159265358979;constexpr Functions
Functions can also be marked constexpr if every step is computable at compile time. Calling them with constant arguments yields a compile-time result.
constexpr int square(int x) { return x * x; }
constexpr int area = square(5); // computed at compile timeInitialization Forms
C++ has several initialization syntaxes. Prefer brace initialization in modern code — it rejects narrowing conversions.
int a = 10; // copy init
int b(10); // direct init
int c{10}; // brace init (preferred)
int d = {10}; // copy-list initNarrowing and Brace Init
Brace initialization protects you from accidental truncation. The example below fails to compile.
double d = 3.7;
int a = d; // OK, silently truncates to 3
int b{d}; // ERROR: narrowing conversionVariable Scope
A variable exists from its declaration until the end of its enclosing block (a pair of { }). Declare variables in the narrowest scope you can — it limits their lifetime and reduces bugs.
Quick Check
Which declaration creates a value that the compiler can use as a compile-time constant?
Recap
Declare variables with an explicit type or with auto for deduction. Use const for read-only values and constexpr for compile-time constants. Prefer brace initialization to catch narrowing.
Frequently asked questions
Is the “Variables, Auto Type Inference and constexpr” lesson free?
Yes — the full text of “Variables, Auto Type Inference and constexpr” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Variables, Auto Type Inference and constexpr”?
Declare variables, use auto for type deduction, and learn when to mark values constexpr. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Variables, Auto Type Inference and constexpr” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up a C++ Compiler
- Hello World and Build Process
- Variables, Auto Type Inference and constexpr
- Basic Operators and Type Conversion