A variable gives a typed object a name and lifetime. Initialize it where it is declared so every later read has a known value. Use const when code must not modify an object through that name, and constexpr when a value or function must be usable during constant evaluation.
auto asks the compiler to deduce a type from the initializer; it does not make C++ dynamically typed. Use it when the type is obvious or cumbersome, but spell out a domain type when deduction would hide units, signedness, ownership, or an important conversion.
Brace initialization rejects many narrowing conversions. const protects an initialized runtime value from later assignment, while constexpr requires a constant-expression initializer and implies const for an object.
#include <iostream>
constexpr int minutes_to_seconds(int minutes) {
return minutes * 60;
}
int main() {
constexpr int timeout = minutes_to_seconds(3);
const auto retries = 2;
std::cout << timeout << " seconds, " << retries << " retries\n";
}
180 seconds, 2 retries
timeout is available during constant evaluation; retries is deduced as int and cannot be reassigned.
A useful comment records why a rule exists, the unit of a value, an external protocol constraint, or a non-obvious tradeoff. Comments that merely translate syntax become stale when code changes. Prefer names such as timeout_seconds over comments that repeatedly explain units.
Use const for values that must not change and keep the changing state narrowly scoped.
#include <iostream>
int main() {
const int retry_limit{3};
int attempts{0};
while (attempts < retry_limit) {
++attempts;
}
std::cout << attempts << '/' << retry_limit << '\n';
}
3/3
<code>auto value = 1;</code> infers <code>int</code>, while some braced forms can infer <code>std::initializer_list<int></code>. That changes overload resolution and assignment behavior.
<code>const</code> prevents modification after initialization, but its value may still come from runtime input. <code>constexpr</code> requires an initializer that can be evaluated at compile time and is appropriate for template arguments and other constant-expression contexts.
A useful comment records an invariant, a non-obvious trade-off, a protocol requirement, or why a tempting alternative is unsafe. A comment such as “increment i” beside <code>++i</code> adds noise and can become stale.
Practice, interview questions, and compiler links for C++ Variables.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.