Lesson 4 of 45
C++Lesson 4: Variables and Data Types
9 min
Course progress9%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 4 of 45
C++9 min
Keep these ideas in mind as you work through the lesson.
Variables are named storage locations whose type determines the kind of data they can hold. C++ provides a rich set of built‑in types:
bool, char, signed/unsigned int, short, long, long long.float, double, long double.char (signed or unsigned depending on implementation) and char16_t, char32_t, wchar_t.bool with literals true and false.C++ also offers type inference via auto and type traits through <type_traits>.
int i = 42; // explicit type
auto f = 3.14; // deduced as double
unsigned long long big = 1ULL; // suffix U for unsigned, LL for long long
bool flag = true; // boolean literal
Use sizeof to query the byte size of a type:
std::cout << \"Size of int: \" << sizeof(int) << \" bytes\
\";
Tip: Prefer uniform initialisation (
int x{5};) to avoid narrowing conversions.
<Alert type="info">auto does not mean dynamic typing; the type is still fixed at compile time.