Sunday, 9 August 2026

Data Types in C

What are Data Types?

Data types tell the compiler what kind of data a variable can store and how much memory to allocate for it. Every variable in C++ must have a data type.

 

Common Data Types (again)

Let's review most commonly used data types in C++:

 

Data Type       Size (bytes)     Range/Values Example

int        4          -2,147,483,648 to 2,147,483,647       int salary = 75000;

short    2          -32,768 to 32,767       short year = 2024;

long     8          Very large whole numbers     long population = 8000000000;

float    4          ±3.4 × 10^38 (6-7 digits precision)   float temperature = 98.6f;

double 8          ±1.7 × 10^308 (15-16 digits precision)         double pi = 3.14159265359;

char     1          Single character (ASCII)        char grade = 'A';

bool     1          true or false    bool isValid = true;

Data Type Sizes

Different data types require different amounts of memory:

 

#include <iostream>

 

// We will run this program later to demonstrate

int main() {

    std::cout << "Size of int: " << sizeof(int) << " bytes" << '\n';

    std::cout << "Size of double: " << sizeof(double) << " bytes" << '\n';

    std::cout << "Size of char: " << sizeof(char) << " byte" << '\n';

    std::cout << "Size of bool: " << sizeof(bool) << " byte" << '\n';

    return 0;

}

Typical sizes:

 

char: 1 byte (can store values -128 to 127)

bool: 1 byte (stores true/false)

int: 4 bytes (can store roughly -2 billion to +2 billion)

double: 8 bytes (can store very large/small decimal numbers)

Why Size Matters

Choosing the right data type size affects both memory usage and program performance. Here's why it's important:

 

1. Memory Usage When you have thousands or millions of variables, size adds up quickly:

 

// Example: Storing 1 million user ages

int ages[1000000];        // Uses 4MB of memory (4 bytes × 1M)

short ages[1000000];      // Uses 2MB of memory (2 bytes × 1M) - 50% savings!

2. Performance Impact Smaller data fits better in computer cache, making programs faster:

 

char smallNumber = 100;     // 1 byte - fits more data in cache

int largeNumber = 1000000;  // 4 bytes - takes more space

double precision = 3.14159265359; // 8 bytes - needed for precision

3. Range Limitations Each type has limits - exceed them and you get overflow errors:

 

short maxShort = 32767;     // Maximum value for short

maxShort = maxShort + 1;    // Overflow! Wraps to -32768

 

int safeNumber = 50000;     // Well within int range

4. Real-World Impact

 

// Game with 10,000 players online

short playerLevel[10000];   // 20KB memory

int playerLevel[10000];     // 40KB memory - double the space!

 

// For a mobile app, this difference matters for battery and performance

Memory efficiency example:

 

// Storing ages in a program with thousands of users

short userAge = 25;              // 2 bytes - good range for ages (-32,768 to 32,767)

int userAge2 = 25;               // 4 bytes - wasteful (-2,147,483,648 to 2,147,483,647)

 

// Scientific calculations

float roomTemp = 23.5f;          // 4 bytes - adequate for temperature readings

double piValue = 3.14159265359;  // 8 bytes - needed for high precision math

 

// Different integer sizes for different ranges

short year = 2024;               // 2 bytes - perfect for years

int population = 50000;          // 4 bytes - good for city populations

long worldPopulation = 8000000000; // 8 bytes - needed for very large numbers

Integer Types

Basic Integer Types

int: Whole numbers (-2,147,483,648 to 2,147,483,647)

short: Smaller integers (-32,768 to 32,767)

long: Larger integers

int normalNumber = 42;

short smallNumber = 100;

long bigNumber = 1000000000;

When to Use Each

// Use int for most situations

int studentCount = 25;

int temperature = -5;

 

// Use short when memory is critical and values are small

short monthNumber = 12;

 

// Use long for very large numbers

long worldPopulation = 8000000000;

Floating-Point Types

Float vs Double

float: 6-7 decimal digits precision (4 bytes)

double: 15-16 decimal digits precision (8 bytes)

float basicPrice = 19.99f;       // Note the 'f' suffix

double precisePi = 3.14159265359;

Generally use double - it has better precision and is the default for decimal numbers in C++.

 

Character and String Types

Single Characters

char letter = 'A';

char digit = '9';

char symbol = '$';

Important: Use single quotes for characters!

 

Strings

#include <string>  // Required for string

 

string firstName = "John";

string lastName = "Doe";

string fullName = firstName + " " + lastName;  // String concatenation

Note

Strings are more complex than basic data types. We'll explore strings further in later lessons.

Boolean Type

The bool type stores true/false values:

 

bool isStudent = true;

bool isGraduated = false;

bool hasDriversLicense = true;

Boolean values:

 

true (equivalent to 1)

false (equivalent to 0)

Choosing the Right Data Type

Decision Guidelines

// For whole numbers:

int age = 25;           // Most common choice

short dayOfMonth = 15;  // When memory matters and values are small

long distance = 93000000; // For very large values

 

// For decimal numbers:

double price = 19.99;   // Preferred for most decimals

float coordinate = 10.5f; // Only when memory is very limited

 

// For text:

char grade = 'A';       // Single character

string name = "Alice";  // Text/words

 

// For yes/no values:

bool isReady = true;    // True/false conditions

Summary

Data types are fundamental to C++ programming:

 

Choose the right size for your data

Use int and double for most situations

Memory efficiency matters in large programs

Actual sizes may vary by system, but these are typical values

Understanding data types helps you write efficient, correct programs and avoid common errors like integer overflow or precision loss.

No comments:

Post a Comment