Chapter 05 of 37

Data Types

When we create a variable, the computer needs to know what kind of value that variable will store.

For example, age might store an integer, while price might store a decimal number.

That's where data types come in.

What is a Data Type?

A data type tells the compiler what type of data a variable can store and how that data should be handled.

For example:

int age = 20;
float price = 99.50;
char grade = 'A';

Here:

  • int stores an integer.

  • float stores a decimal number.

  • char stores a single character.

Think of data types like different types of containers. You wouldn't use a water bottle to store a large amount of furniture. Similarly, the computer uses different data types for different kinds of values.


Types of Data Types in C

C data types can broadly be divided into these categories:

Category

Examples

Basic / Primary

int, char, float, double

Derived

Arrays, Pointers, Functions

User-defined

struct, union, enum

Void

void

For now, let's focus on the basic data types.

1. int

int is used to store whole numbers.

int age = 20;
int marks = 85;

It can store values such as:

10
25
-5
100

But not decimal values like 10.5.


2. float

float is used to store decimal numbers.

float price = 99.50;
float height = 5.8;

It is useful when we need fractional values.


3. double

double is also used for decimal numbers but generally provides more precision than float.

double distance = 12345.6789;

So, when more precision is required, double is often preferred.


4. char

char is used to store a single character.

char grade = 'A';

A character is written inside single quotes.

Examples:

char letter = 'B';
char symbol = '#';
char digit = '5';

Remember that '5' is a character, while 5 is an integer.


5. void

void means no value or no type.

It is commonly used with functions that don't return a value.

For example:

void display()
{
    printf("Hello");
}

We'll understand void properly when we learn functions.


Common Data Types at a Glance

Data Type

Used For

Example

int

Whole numbers

int age = 20;

float

Decimal numbers

float price = 99.5;

double

More precise decimals

double pi = 3.14159;

char

Single character

char grade = 'A';

void

No value/type

void display()

The exact size and range of some C data types can depend on the compiler and platform, so don't memorize a fixed size for every type just yet. We'll cover sizes, ranges, and modifiers separately.

Simple Example

#include <stdio.h>

int main()
{
    int age = 20;
    float height = 5.8;
    char grade = 'A';

    printf("%d\n", age);
    printf("%f\n", height);
    printf("%c\n", grade);

    return 0;
}

Here, each variable uses a different data type because each stores a different kind of value.

In Simple Words

A data type tells C what kind of data a variable is going to store.

For example:

20       → int
5.8      → float
'A'      → char
3.14159  → double

Understanding data types is important because almost every C program you write will use variables to store some kind of data.