Variables and Data Types
Fundamental data types and variable declarations
Interview Relevant: Core concept for all interviews
Variables and Data Types
C++ is statically typed - every variable has a fixed type at compile time.
Fundamental Types
- int: Integer (typically 4 bytes)
- double: Double-precision floating-point (8 bytes)
- float: Single-precision floating-point (4 bytes)
- char: Single character (1 byte)
- bool: Boolean true/false
- void: No type (used for functions)
Type Modifiers
- short, long, long long: Size modifiers
- signed, unsigned: Sign modifiers
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*