C++ variables have an explicit type: int count = 0;. Unlike JavaScript, you cannot silently change a variable from int to std::string without a compile error.
Common scalar types
int,long— integersdouble,float— floating point (preferdouble)char— single characterbool—trueorfalse
auto and initialization
auto count = 10; // deduced as int
double ratio = 0.75;
bool ok = true;
Unlike C, C++ encourages initialization—value-initialize or assign before use.
Important interview questions and answers
- Q: Size of int?
A: Implementation-defined but at least 16 bits; use<cstdint>for fixed widths when needed. - Q: auto pitfalls?
A:auto x = ref;copies unless you writeauto&orconst auto&.
Self-check
- What type does auto deduce from an integer literal?
- How is bool different from C truthiness?
Pitfall: auto without reference captures copies—use const auto& in range-for when iterating large containers.
Interview prep
- Why initialize variables?
Although C++ improves on C, uninitialized built-ins have indeterminate values until assigned—always initialize.
- bool vs C truthiness?
C++ has a distinct
booltype withtrueandfalsekeywords.