C++ Academy · Lesson

Variables, Auto Type Inference and constexpr

Declare variables, use auto for type deduction, and learn when to mark values constexpr.

Lesson 3 of 413 steps

Variables, Auto Type Inference and constexpr is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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::string

When 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 time

Initialization 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 init

Narrowing 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 conversion

Variable 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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Variables, Auto Type Inference and constexpr” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Variables, Auto Type Inference and constexpr”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Setting Up a C++ Compiler
  2. Hello World and Build Process
  3. Variables, Auto Type Inference and constexpr
  4. Basic Operators and Type Conversion
← Back to C++ Academy