Data Types in C

In C programming, a data type defines the kind of value a variable can store. Every variable must have a data type declared before it can be used. The data type tells the compiler how much memory to allocate and what kind of operations can be performed on that variable.

Think of a data type like a container — a glass holds water, a box holds books, and a bag holds clothes. Each container is designed for a specific type of content. Similarly, each data type in C is designed to store a specific kind of data.

Classification of Data Types in C

Data types in C are divided into four main categories:

  1. Basic (Primary) Data Types
  2. Derived Data Types
  3. User-Defined Data Types
  4. Void Data Type

Data Types in C
│
├── Basic (Primary)
│   ├── int
│   ├── float
│   ├── double
│   └── char
│
├── Derived
│   ├── Array
│   ├── Pointer
│   └── Function
│
├── User-Defined
│   ├── struct
│   ├── union
│   └── enum
│
└── Void
    └── void

Basic Data Types

These are the fundamental data types built into the C language.

1. int — Integer Type

Used to store whole numbers (numbers without decimal points). This includes both positive and negative numbers.

TypeSize (Typical)Range
int4 bytes-2,147,483,648 to 2,147,483,647
short int2 bytes-32,768 to 32,767
long int4 or 8 bytes-2,147,483,648 to 2,147,483,647 (or larger)
unsigned int4 bytes0 to 4,294,967,295

#include <stdio.h>

int main()
{
    int apples = 10;
    int temperature = -5;
    unsigned int students = 350;

    printf("Apples: %d\n", apples);
    printf("Temperature: %d\n", temperature);
    printf("Students: %u\n", students);

    return 0;
}

Output


Apples: 10
Temperature: -5
Students: 350

2. float — Floating Point Type

Used to store decimal numbers (numbers with a fractional part). It provides up to 6–7 decimal places of precision.

TypeSizePrecision
float4 bytesUp to 6–7 decimal digits

#include <stdio.h>

int main()
{
    float price = 49.99;
    float temperature = 36.6;

    printf("Price: %.2f\n", price);
    printf("Temperature: %.1f\n", temperature);

    return 0;
}

Output


Price: 49.99
Temperature: 36.6

3. double — Double Precision Floating Point

Used when more decimal precision is needed than float can offer. It uses 8 bytes and provides up to 15–16 decimal places.

TypeSizePrecision
double8 bytesUp to 15–16 decimal digits
long double12 or 16 bytesUp to 18–19 decimal digits

#include <stdio.h>

int main()
{
    double pi = 3.14159265358979;
    printf("Pi = %.14f\n", pi);
    return 0;
}

Output


Pi = 3.14159265358979

4. char — Character Type

Used to store a single character such as a letter, digit, or symbol. A character is enclosed in single quotes. Internally, C stores characters as their ASCII integer values.

TypeSizeRange
char1 byte-128 to 127 (signed) or 0 to 255 (unsigned)

#include <stdio.h>

int main()
{
    char grade = 'A';
    char symbol = '#';

    printf("Grade: %c\n", grade);
    printf("ASCII value of A: %d\n", grade);
    printf("Symbol: %c\n", symbol);

    return 0;
}

Output


Grade: A
ASCII value of A: 65
Symbol: #

Format Specifiers for Data Types

Format specifiers are used with printf() and scanf() to indicate the type of data being read or printed.

Data TypeFormat SpecifierExample
int%dprintf("%d", 10);
unsigned int%uprintf("%u", 300u);
float%fprintf("%f", 3.14);
double%lfprintf("%lf", 3.14159);
char%cprintf("%c", 'A');
string%sprintf("%s", "Hello");
long int%ldprintf("%ld", 100000L);
short int%hdprintf("%hd", 50);

sizeof() Operator

The sizeof() operator returns the size (in bytes) of a data type or variable on the current system.


#include <stdio.h>

int main()
{
    printf("Size of int    : %zu bytes\n", sizeof(int));
    printf("Size of float  : %zu bytes\n", sizeof(float));
    printf("Size of double : %zu bytes\n", sizeof(double));
    printf("Size of char   : %zu bytes\n", sizeof(char));

    return 0;
}

Output (on most 64-bit systems)


Size of int    : 4 bytes
Size of float  : 4 bytes
Size of double : 8 bytes
Size of char   : 1 byte

Type Modifiers in C

Type modifiers change the storage capacity or the range of basic data types. The four type modifiers in C are:

  • signed — Can store both positive and negative numbers (default for int and char)
  • unsigned — Stores only positive numbers, doubles the upper range
  • short — Uses less memory (2 bytes for int)
  • long — Uses more memory (4 or 8 bytes for int)

#include <stdio.h>

int main()
{
    signed int a = -100;
    unsigned int b = 300;
    short int c = 50;
    long int d = 1000000;

    printf("signed int   : %d\n", a);
    printf("unsigned int : %u\n", b);
    printf("short int    : %hd\n", c);
    printf("long int     : %ld\n", d);

    return 0;
}

Output


signed int   : -100
unsigned int : 300
short int    : 50
long int     : 1000000

Void Data Type

The void data type means "no value" or "empty type". It is commonly used in:

  • Functions that return nothing: void printMessage() { ... }
  • Functions that take no parameters: int main(void) { ... }
  • Void pointers: A general-purpose pointer that can point to any data type

Type Conversion in C

Sometimes a value of one data type needs to be converted to another. This is called type conversion.

Implicit (Automatic) Type Conversion

The compiler automatically converts smaller types to larger types to avoid data loss.


#include <stdio.h>

int main()
{
    int num = 5;
    float result;

    result = num;  // int automatically converted to float
    printf("Result: %.1f\n", result);  // Output: 5.0

    return 0;
}

Explicit Type Conversion (Casting)

The programmer manually converts one type to another using a cast operator.


#include <stdio.h>

int main()
{
    int a = 7;
    int b = 2;
    float result;

    result = (float)a / b;  // casting 'a' to float before division
    printf("Result: %.2f\n", result);  // Output: 3.50

    return 0;
}

Summary of Basic Data Types

Data TypeSizeUsed ForFormat Specifier
char1 byteSingle character%c
int4 bytesWhole numbers%d
float4 bytesDecimal numbers (low precision)%f
double8 bytesDecimal numbers (high precision)%lf
voidNo value / generic purpose

Summary

Data types are the backbone of any C program. Choosing the correct data type ensures that the program uses memory efficiently and handles data correctly. The four primary data types — int, float, double, and char — cover most everyday programming needs. Type modifiers like unsigned, long, and short provide additional flexibility. Understanding data types thoroughly is essential before moving on to variables, operators, and more complex C concepts.

Leave a Comment

Your email address will not be published. Required fields are marked *