Data Types in C

Data types define the type of data a variable can store. Each data type occupies a different amount of memory and has its own range. Understanding data types helps you choose the right storage for values in your program.

Why Do We Need Data Types?

Categories of Data Types in C

1. Basic Data Types  
2. Derived Data Types  
3. User-defined Data Types

1. Basic Data Types

Integer (int)

Stores whole numbers without decimals.

int age = 25;

Float

Stores decimal numbers (single precision).

float price = 99.75;

Double

Stores large decimal numbers (double precision).

double distance = 12345.6789;

Character (char)

Stores a single character.

char grade = 'A';

2. Derived Data Types

int marks[5];     // array  
int *ptr;         // pointer  
void test();      // function

3. User-defined Data Types

struct Student {
    int roll;
    char name[20];
};

Size of Data Types

char      → 1 byte  
int       → 4 bytes  
float     → 4 bytes  
double    → 8 bytes  
long int  → 8 bytes  
short int → 2 bytes  

Sizes may vary depending on the compiler.

Signed & Unsigned Types

Signed → can store + and - values
Unsigned → only + values, larger range

signed int x = -10;
unsigned int y = 50;

Format Specifiers

%d → int  
%f → float  
%c → char  
%lf → double  
%u → unsigned int  
%ld → long int  

Practical Example

#include <stdio.h>

int main() {

    int age = 25;
    float salary = 45000.50;
    double distance = 98765.432;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Distance: %.3lf\n", distance);
    printf("Grade: %c\n", grade);

    return 0;
}

Practical Example 2 – Size of Data Types

#include <stdio.h>

int main() {

    printf("Size of int: %lu bytes\n", sizeof(int));
    printf("Size of float: %lu bytes\n", sizeof(float));
    printf("Size of double: %lu bytes\n", sizeof(double));
    printf("Size of char: %lu byte\n", sizeof(char));

    return 0;
}

Practical Example 3 – Signed vs Unsigned

#include <stdio.h>

int main() {

    signed int a = -20;
    unsigned int b = 20;

    printf("Signed: %d\n", a);
    printf("Unsigned: %u\n", b);

    return 0;
}

Practical Example 4 – Unsigned Overflow Behavior

#include <stdio.h>

int main() {

    unsigned int x = 0;
    x = x - 1;   // causes wrap-around

    printf("Unsigned Overflow Result: %u\n", x);

    return 0;
}

Practical Example 5 – Range Difference

#include <stdio.h>

int main() {

    signed int s = 32767;        // maximum for 16-bit (example)
    unsigned int u = 65535;      // maximum for 16-bit unsigned

    printf("Signed Max Example: %d\n", s);
    printf("Unsigned Max Example: %u\n", u);

    return 0;
}

Practical Example 6 – Negative Values in Unsigned

#include <stdio.h>

int main() {

    unsigned int num = -10;   // negative assigned to unsigned

    printf("Storing -10 in unsigned gives: %u\n", num);

    return 0;
}

Practice Questions

  1. What are data types? Why are they necessary?
  2. Differentiate between int, float, and double.
  3. What is the size of basic data types?
  4. Write a program to demonstrate signed and unsigned types.
  5. Write 5 differences between structure and union.

Practice Task

Create a program that displays values of different data types (int, float, double, char) and prints their sizes using sizeof().