C Data Types

Understanding different types of data in C programming

šŸŽÆ What are Data Types?

Data types specify the type of data that a variable can store. C has several built-in data types for different kinds of information.


// Different data types in C
int age = 25;           // Integer
float height = 5.9f;    // Floating point
char grade = 'A';       // Character
                                    

Memory Usage:

int: 4 bytes
float: 4 bytes
char: 1 byte

Categories of Data Types

šŸ”¢

Integer Types

Store whole numbers

int num = 42;
short s = 100;
long l = 1000000L;
šŸŽˆ

Floating Types

Store decimal numbers

float f = 3.14f;
double d = 3.14159;
šŸ”¤

Character Type

Store single characters

char ch = 'A';
char symbol = '@';
šŸŽ­

Derived Types

Arrays, pointers, structures

int arr[5];
int *ptr;

šŸ”¹ Integer Data Types

Integer data types store whole numbers without decimal points in varying sizes and ranges. Common integer types include char (1 byte), short (2 bytes), int (4 bytes typically), and long (4-8 bytes). Each type accommodates different value ranges for memory efficiency. Selecting appropriate integer types prevents overflow, minimizes memory usage, and improves program performance. Understanding integer representation helps avoid arithmetic errors and security vulnerabilities.

#include <stdio.h>

int main() {
    // Different integer types
    char small_num = 127;           // 1 byte (-128 to 127)
    short medium_num = 32000;       // 2 bytes (-32,768 to 32,767)
    int regular_num = 2000000;      // 4 bytes (-2B to 2B approx)
    long big_num = 1000000000L;     // 8 bytes (very large range)
    
    // Display sizes and values
    printf("char: %d (size: %zu bytes)\n", small_num, sizeof(char));
    printf("short: %d (size: %zu bytes)\n", medium_num, sizeof(short));
    printf("int: %d (size: %zu bytes)\n", regular_num, sizeof(int));
    printf("long: %ld (size: %zu bytes)\n", big_num, sizeof(long));
    
    return 0;
}

Output:

char: 127 (size: 1 bytes)
short: 32000 (size: 2 bytes)
int: 2000000 (size: 4 bytes)
long: 1000000000 (size: 8 bytes)

šŸ”¹ Floating-Point Data Types

Floating-point types store numbers with decimal points for scientific and mathematical computations. Common floating-point types are float (4 bytes, ~6-7 decimal digits precision), double (8 bytes, ~15-16 decimal digits precision), and long double (extended precision). Choose based on required precision and memory constraints. Understanding floating-point representation prevents precision loss, rounding errors, and enables accurate scientific calculations and graphics programming.

#include <stdio.h>

int main() {
    // Floating-point types
    float price = 19.99f;           // 4 bytes, ~7 decimal digits
    double precise = 3.14159265359; // 8 bytes, ~15 decimal digits
    long double extra = 2.718281828459045L; // 16 bytes, ~19 digits
    
    // Display with different precisions
    printf("float: %.2f (size: %zu bytes)\n", price, sizeof(float));
    printf("double: %.10f (size: %zu bytes)\n", precise, sizeof(double));
    printf("long double: %.15Lf (size: %zu bytes)\n", extra, sizeof(long double));
    
    // Demonstrating precision difference
    float f = 1.23456789f;
    double d = 1.23456789;
    
    printf("\nPrecision comparison:\n");
    printf("float:  %.10f\n", f);
    printf("double: %.10f\n", d);
    
    return 0;
}

Output:

float: 19.99 (size: 4 bytes)
double: 3.1415926536 (size: 8 bytes)
long double: 2.718281828459045 (size: 16 bytes)

Precision comparison:
float: 1.2345678806
double: 1.2345678900

šŸ”¹ Character Data Type

The char type stores single characters and small integers, occupying one byte in memory. Use char variable = 'A'; for character storage or numeric values from -128 to 127. Characters correspond to ASCII values enabling numeric operations and comparisons. The char type forms the foundation of string handling since strings are character arrays. Understanding character representation enables text processing and character manipulation tasks.

#include <stdio.h>

int main() {
    // Character variables
    char letter = 'A';
    char digit = '5';
    char symbol = '@';
    
    // Characters are actually small integers (ASCII values)
    char ascii_A = 65;  // Same as 'A'
    
    printf("Character: %c (ASCII: %d)\n", letter, letter);
    printf("Digit: %c (ASCII: %d)\n", digit, digit);
    printf("Symbol: %c (ASCII: %d)\n", symbol, symbol);
    printf("ASCII 65: %c\n", ascii_A);
    
    // Character arithmetic
    char next_letter = letter + 1;
    printf("Next letter after %c is %c\n", letter, next_letter);
    
    return 0;
}

Output:

Character: A (ASCII: 65)
Digit: 5 (ASCII: 53)
Symbol: @ (ASCII: 64)
ASCII 65: A
Next letter after A is B

šŸ”¹ Data Type Ranges and Limits

Each data type accommodates specific ranges of values determined by its bit size and representation method. char ranges from -128 to 127, int typically -2,147,483,648 to 2,147,483,647, and unsigned types double the positive range. The limits.h header provides standard constants defining exact ranges for your system. Understanding type limits prevents overflow, ensures data integrity, and enables writing portable code across different computing platforms.

Common Data Type Ranges:

Type Size Range
char 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes ±3.4E±38 (~7 digits)
double 8 bytes ±1.7E±308 (~15 digits)
#include <stdio.h>
#include <limits.h>
#include <float.h>

int main() {
    printf("Data Type Limits:\n");
    printf("char: %d to %d\n", CHAR_MIN, CHAR_MAX);
    printf("short: %d to %d\n", SHRT_MIN, SHRT_MAX);
    printf("int: %d to %d\n", INT_MIN, INT_MAX);
    printf("float: %E to %E\n", FLT_MIN, FLT_MAX);
    
    return 0;
}

🧠 Test Your Knowledge

Which data type is best for storing a person's age?