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

  • int Integer (usually 4 bytes)
  • char Single character (1 byte)
  • bool Boolean (true/false)

Floating-Point Types

  • float Single-precision (4 bytes)
  • double Double-precision (8 bytes)
  • long double Extended-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*

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In