Data Types in C

In C programming, data types are like containers that tell the computer what kind of data you want to store and how much memory space to allocate for it. Think of them as different sized boxes - some are perfect for small items, while others are designed for larger ones.

Data types are crucial because they:

  • Determine how much memory your variable will use
  • Define what operations you can perform on the data
  • Help prevent errors in your program
  • Make your code more efficient

Following are the Basic Data Types in C

1. Integer (int)

The int data type is used to store whole numbers (positive, negative, or zero).

Memory Size: Typically 4 bytes (32 bits)
Value Range: -2,147,483,648 to 2,147,483,647
Format Specifier: %d

#include <stdio.h>

int main() {
    int age = 25;
    int temperature = -10;
    int score = 0;
    
    printf("Age: %d\n", age);
    printf("Temperature: %d°C\n", temperature);
    printf("Score: %d\n", score);
    
    return 0;
}

When to use: Perfect for counting, indexing, and storing whole numbers like age, quantity, or scores.

2. Floating Point (float)

The float data type stores decimal numbers with single precision.

Memory Size: 4 bytes (32 bits)
Value Range: Approximately ±3.4 × 10^38 (with ~7 decimal digits precision)
Format Specifier: %f

#include <stdio.h>

int main() {
    float price = 19.99;
    float pi = 3.14159;
    float weight = 65.5;
    
    printf("Price: $%.2f\n", price);
    printf("Pi: %.5f\n", pi);
    printf("Weight: %.1f kg\n", weight);
    
    return 0;
}

When to use: Great for measurements, prices, and calculations where you need decimal precision but don't require extreme accuracy.

3. Double Precision (double)

The double data type stores decimal numbers with double precision - it's like float's bigger, more accurate sibling.

Memory Size: 8 bytes (64 bits)
Value Range: Approximately ±1.7 × 10^308 (with ~15-17 decimal digits precision)
Format Specifier: %lf (for scanf) or %f (for printf)

#include <stdio.h>

int main() {
    double precise_pi = 3.141592653589793;
    double scientific_constant = 6.022e23;
    double bank_balance = 1234567.89;
    
    printf("Precise Pi: %.15f\n", precise_pi);
    printf("Avogadro's number: %.3e\n", scientific_constant);
    printf("Balance: $%.2f\n", bank_balance);
    
    return 0;
}

When to use: Essential for scientific calculations, financial applications, or any situation requiring high precision.

4. Character (char)

The char data type stores a single character or small integer values.

Memory Size: 1 byte (8 bits)
Value Range: -128 to 127 (or 0 to 255 if unsigned)
Format Specifier: %c

#include <stdio.h>

int main() {
    char grade = 'A';
    char initial = 'J';
    char symbol = '@';
    
    printf("Grade: %c\n", grade);
    printf("Initial: %c\n", initial);
    printf("Symbol: %c\n", symbol);
    
    // You can also store ASCII values
    char ascii_value = 65; // This is 'A'
    printf("ASCII 65 is: %c\n", ascii_value);
    
    return 0;
}

When to use: Perfect for storing single characters, flags, or small integer values.

Memory Size Comparison

Data TypeSize (bytes)Example Values
char1'A', 'z', '5'
int442, -100, 0
float43.14, -2.5
double83.141592653589

Format Specifiers Quick Reference

Data TypePrintfScanf
int%d%d
float%f%f
double%f%lf
char%c%c

Choosing the Right Data Type

Think of choosing data types like picking the right tool for a job:

  • Need to count something? → Use int
  • Working with money or measurements? → Use float or double
  • Need high precision for scientific work? → Use double
  • Storing a single letter or symbol? → Use char

Remember: using the right data type makes your program more efficient and helps prevent bugs. It's like using the right size container - you wouldn't store a single marble in a huge box, nor would you try to fit a basketball in a small envelope!