Type Casting in C Tutorial

In this section, we will learn what the type casting is and how to use it in C.

Note: we’re assuming you already familiar with the data-types in C language.

What is Type Casting in C? (Type Conversion in C)

Casting in programming is about producing another value with a different type compared to the one that is already existing. It’s basically a conversion from one data-type to another.

For example, let’s say we have a variable of type double with the value 3.333 and we want to cast it to integer type.

The word `cast` in this sentence means we want to produce a new value out of 3.33 that fits in an integer variable.

We know that an integer-type value does not have a fraction. So in order to store a double value in a variable of type integer, behind the scene the fraction part first should be dropped and then the whole-part of the number can be stored in an integer type variable.

So the integer variable will hold the value `3` in this case.

Let’s look at this casting from the memory-perspective:

The amount of memory that is allocated to an integer variable is far less than the amount of memory allocated to a double variable. For example, depending on the underlying system, an integer value might take up to 32 bits of memory, but a value of type double at least takes about 64 bits of memory.

So it’s obvious we can’t cram 64 bits’ memory into 32 bits! For this reason, part of a double-value should be dropped before being assigned to a variable of type integer.

Notes:

  • Anytime a cast happens from a type with larger memory space to a type with lower memory space (like `int` to `short` or `double` to `int`) there’s a chance of losing data.

Imagine it this way: a lot of people are in a big room, now for some reason they need to leave the room and enter to another room that is significantly smaller than the first one.

So in such case, if the bigger room was filled with people, then some of them will stay out because there’s not enough space for all in the smaller room. (Of course, if the number of people wasn’t high in the bigger room, they all might be fitted in the smaller room as well. Now imagine we have a variable of type `int` and its value is 10, and now we want to assign this value to another variable of type `short`. A variable of type `int` takes more memory than a variable of type `short` but because the number in the int-variable is small enough (10 in this case), it can be stored in the variable of type `short` without any loss of data).

In short, it’s not just the amount of memory space but the actual value in the variable that is a deciding factor of whether a lost in data might happen when we’re casting.

  • On the other hand, when we want to cast a type with a lower memory space to a type with higher memory space (like `int` to `double`) we’re rest assured that there’s no lost in data. This is like entering from a small room to a bigger room. No-matter how filled with people is the smaller room, the bigger room has more than enough space for all the people of the smaller room.

Types of Casting

There are two types of casting in C:

  • Implicit Casting
  • Explicit Casting

Implicit Casting in C

By default, when we cast a variable with a lower memory space to a variable with a higher memory space, the cast will be automatically done behind the scene and so there’s no work on our side (developers side).

This is known as implicit casting.

Example: implicit casting in C

#include <stdio.h>

int main() {

    int iVar = 100;
    double dVar = iVar;

    printf("The value of the dVar is: %f\n",dVar);
    
    return 0;
}
Output: The value of the dVar is: 100.000

In this example we’ve assigned the value of `iVar` to `dVar` which is of type double and so this casting is from a variable with lower memory space to a variable with higher memory space and because of this, there’s no loss of data.

Explicit Casting in C

When we want to cast a variable with higher memory space to a variable with lower memory space, because there’s a chance of data lost, we need to explicitly tell the compiler that we are aware of this data lost but still insist on this casting to happen.

This is called Explicit-Casting.

Example: explicit casting in c

#include <stdio.h>

int main() {

    double dVar = 20.43;
    int iVar = (int) dVar;


    printf("The value of the dVar is: %d\n",iVar);

    return 0;
}

Output:

The value of the dVar is: 20

The important part of the example above is this:

int iVar = (int) dVar;

Here, before assigning the value of `dVar` to `iVar` we used explicit casting `(int)`. This explicit casting `(int)` tells the compiler that we know there’s a chance of data lost, but we still insist on doing so. So the compiler will convert the double-value (64 bits) to integer-value (32bits) and then will store the result in `iVar`.

Notes:

  1. Within the parentheses, we use the name of the data-type we want the target value be cast into.
  2. Also, we can use this explicit casting on lower-memory-space variable to larger-memory-space variable (like `int` to `double`) but that’s optional.
  3. Any time we want to cast a value explicitly, we put the target data-type into parentheses and that parentheses will be set on the left side of the target value.

Example:

(int) 23.222;

(short) 223433;

(float) 233.421;
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies