C++ · Module 2 · Lesson 12 min read

Fundamental Data Types

The built-in (fundamental) types

TypeTypical sizeHoldsExample
bool1 bytetrue / falsebool paid = true;
char1 byteone characterchar grade = 'A';
short2 bytessmall integershort year = 2026;
int4 bytesinteger (≈ ±2.1 billion)int count = 42;
long long8 bytesbig integer (≈ ±9.2 × 10¹⁸)long long views = 9000000000;
float4 bytesdecimal, ~7 digits precisionfloat temp = 36.6f;
double8 bytesdecimal, ~15–16 digits ✅ defaultdouble price = 99.99;
long double8–16 bytesextended precision

Sizes can vary between platforms; check with sizeof.

signed vs unsigned

  • int can be negative; unsigned int cannot but goes twice as high
  • Mixing signed and unsigned in comparisons causes surprising bugs ⚠️

Literals

LiteralType
42int
42LLlong long
42uunsigned
3.14double
3.14ffloat
'A'char
"text"string literal (array of const char)
truebool
0x1F, 0b1010, 017hex, binary (C++14), octal
1'000'000digit separators (C++14)

bool printing

std::cout << true prints 1. Use std::boolalpha to print true/false.

char is a small integer

'A' is stored as 65 (ASCII). char c = 'A' + 1; gives 'B'.

Which type should I use?

  • Whole numbers: int (or long long for big values)
  • Decimals: double
  • Yes/no: bool
  • Text: std::string (Unit 3)
  • Money: integers in the smallest unit (paisa/cents) to avoid rounding

Example

C++
#include <iostream>

int main() {
    bool is_paid = true;
    char grade = 'A';
    int lessons = 130;
    long long downloads = 9'000'000'000LL;
    double price = 499.99;
    float rating = 4.5f;

    std::cout << "sizeof(bool)      = " << sizeof(bool) << '\n';
    std::cout << "sizeof(char)      = " << sizeof(char) << '\n';
    std::cout << "sizeof(int)       = " << sizeof(int) << '\n';
    std::cout << "sizeof(long long) = " << sizeof(long long) << '\n';
    std::cout << "sizeof(double)    = " << sizeof(double) << '\n';

    std::cout << "is_paid: " << is_paid << " / " << std::boolalpha << is_paid << '\n';
    std::cout << "grade: " << grade << ", next grade code: " << grade + 1
              << ", as char: " << static_cast<char>(grade + 1) << '\n';
    std::cout << lessons << " lessons, " << downloads << " downloads\n";
    std::cout << "price " << price << ", rating " << rating << '\n';
    std::cout << "hex 0xFF = " << 0xFF << ", binary 0b1010 = " << 0b1010 << '\n';
    return 0;
}

/* Output:
sizeof(bool)      = 1
sizeof(char)      = 1
sizeof(int)       = 4
sizeof(long long) = 8
sizeof(double)    = 8
is_paid: 1 / true
grade: A, next grade code: 66, as char: B
130 lessons, 9000000000 downloads
price 499.99, rating 4.5
hex 0xFF = 255, binary 0b1010 = 10
*/