Type Conversion and Type Casting

Last Updated: Jan 2026


We now know several C data types:

char
int
float
double

But when we use different types together, C sometimes needs to convert one type into another.

Type conversion means changing a value from one data type to another.

For example:

int
 ↓
double

or:

double
 ↓
int

Example:

int x = 10;
double y = x;

Conceptually:

10 (int)
    ↓
10.0 (double)

Here an int value is converted to double.

Understanding this is essential because it explains many common C behaviors, especially:

5 / 2

versus:

(double)5 / 2

So There Are Two Main Types of Conversion

Type Conversion
│
├── Implicit Conversion
│
└── Explicit Conversion / Cast

Implicit Conversion

You don't explicitly tell C to perform the conversion. C performs it automatically according to its type-conversion rules.

Hierarchy of Conversion Rule : char → int → long → float → double

Example:

#include <stdio.h>

int main() {

    int x = 10;
    double y = x;

    printf("%f\n", y);

    return 0;
}

Output:

10.000000

The conversion happened automatically.


Another Example

float temperature = 36.5f;
double value = temperature;

Conceptually:

36.5f
 ↓
36.5

The conversion from float to double generally doesn't lose the value represented by the float.


Explicit Type Casting

Sometimes you want to tell C exactly what conversion you want. You can use a cast:

The general form is:

(type) expression

Example:

double x = 10.5;

int y = (int)x;

Here:

(int)x

means:

Convert x to int for this expression.


Examples:

(int)3.14
(double)10
(float)20
(char)65

Example:

#include <stdio.h>

int main() {

    double price = 99.99;

    int value = (int)price;

    printf("%d\n", value);

    return 0;
}

Output:

99

The original variable:

price

is still a double. The cast creates a converted value for that expression.



Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR