---
title: "Type Casting in C Tutorial"
url: https://weworkworldwide.com/tutorials/type-casting-in-c-tutorial-3/
description: "Type Casting in C Tutorial: In this section we will learn what the type casting is and how to use it in C. What is Type Casting in C? (Type Conversion in…"
date: 2026-10-31T09:00:03+00:00
source: https://weworkworldwide.com/llms.txt
---

# 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 *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 exists. 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 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.**

## 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 or not 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 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

``` line-numbers
#include <stdio.h>
 
 int main() {
 
 int iVar = 100;
 double dVar = iVar;
 
 printf("The value of the dVar is: %fn",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 about this data lost but still insist on this casting to happen.

``` line-numbers
This is called Explicit-Casting.
```

## Example: explicit casting in c

``` line-numbers
#include <stdio.h>
 
 int main() {
 
 double dVar = 20.43;
 int iVar = (int) dVar;
 
 
 printf("The value of the dVar is: %dn",iVar);
 
 return 0;
 }
Output:
The value of the dVar is: 20
```

The important part of the example above is this:

``` line-numbers
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 in doing so. So the compiler will convert the double-value (64 bits) to integer-value (32bits) and then will store the result in `iVar`.

## Note: 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:

``` line-numbers
(int) 23.222;
(short) 223433;
(float) 233.421;
```
