Variables and Data Types
Fundamental data types and variable declarations
Interview Relevant: Core concept for all interviews
Variables and Data Types
C++ employs strong, static typing. Every variable's data type must be definitively known at compile-time, which allows the compiler to allocate highly optimized contiguous blocks of memory predictably.
Integral Types
intInteger (usually 4 bytes)charSingle character (1 byte)boolBoolean (true/false)
Floating-Point Types
floatSingle-precision (4 bytes)doubleDouble-precision (8 bytes)long doubleExtended-precision
Size and Sign Modifiers
Integral types can be prefixed with modifiers to alter range and memory footprint:
short int
long int
long long int
unsigned int
signed char
Code Examples
Variable declarations with different data types.
cpp
1int age = 25;
2double price = 19.99;
3char grade = 'A';
4bool isActive = true;
5unsigned int count = 100;
6long long bigNum = 9223372036854775807LL;
7
8// C++11: auto type inference
9auto x = 42; // int
10auto y = 3.14; // double
11auto name = "John"; // const char*