---
title: "casting (type conversion)"
url: https://weworkworldwide.com/tutorials/casting-type-conversion-2/
description: ""You cannot convert people beyond your own conversion" James E. Faust In this section we're assuming you already familiar with data-types. Casting in progr"
date: 2026-09-16T09:00:29+00:00
source: https://weworkworldwide.com/llms.txt
---

# casting (type conversion)

“You cannot convert people beyond your own conversion” James E. Faust

In this section we’re assuming you already familiar with *data-types*.

Casting in programming is about converting one data-type to another.

In programming, there are times that we want to assign the value of one data-type variable to another variable that has different data-type.

For example:

``` line-numbers
int iVar = 100;
double dVar = iVar;
```

Here we tried to assign the value stored in `iVar` variable to the `dVar` variable.

This process is called casting. Basically when we cast, it means we’re trying to change the data-type of a value (Converting its data-type).

At first it might seems not a big deal and we’re just simply moving data around but there are some cautions that we need to be aware of:

Data types have different amount of memory space. For example a variable of type `int` has 4 bytes of memory, but a variable of type `short` has 2 bytes of memory.

But, what does it have to do with casting?

Well, passing values between different data-types is about passing the entire memory space from one data-type to another. For example if the source value is of type `int` and the target variable is of type `short`, we’re basically trying to store 4 bytes of memory space to only 2 bytes of memory space!

Confusing? Let’s see an example then:

``` line-numbers
#include <iostream>
using namespace std;
int main(){
int age = 2500000;
short sVar = age;
cout<<sVar;
return 0;
}
```

Output:

9632

In this example, we’ve created the variable `age`. The type of this variable is `int` and so 4 bytes of memory space is allocated to this variable. Then we’ve declared the variable `sVar` which is of type `short`. So the memory space that is allocated to the `sVar` variable is 2 bytes.

We also initialized the `sVar` variable with the value that is assigned to the `age` variable.

This is where things get interesting: when we assigned the value of the `age` variable to the `sVar` variable, behind the scene, the entire content of the memory space of the `age` variable which is 4 bytes (32 bits) was assigned to the `sVar` variable which has only half of the space (2 bytes or 16 bits) compare to the `age` variable.

It is not possible to cram 32 bits of data to 16 bits memory space and for that reason parts of the data in this casting (conversion) will be dropped (more specifically 16 bits of it). And that’s why we saw a different (corrupted) value when the `sVar` printed to the output.

In short: when assigning a value with lower-memory-space to a variable with larger-memory-space (like `short-type` to `int-type`) there’re no worries because there are enough memory-space in the target variable and the entire content of the source can be stored there without any data-lost. But on the contrary, when assigning a value of larger-memory-space to a variable with lower-memory-space, there’s a chance of data-lost.

By default when we cast a value with lower memory space to a variable with higher memory space, the cast will be automatically done behind the scene and so there’s no work on our side. This is called implicit casting.

Example:

``` line-numbers
#include <iostream>
using namespace std;
int main(){
short sVar = 10;
int iVar = sVar;
cout<<iVar;
return 0;
}
```

Output:

10

## Explicit Casting:

But when we want to assign the value of a variable with lower-memory space to a variable with larger-memory-space, we need to explicitly tell the compiler that we are aware of the data lost but still insisting on doing so.

This is called Explicit Casting.

Example:

``` line-numbers
#include <iostream>
using namespace std;
int main(){
int age = 2500000;
short sVar = (short) age;
return 0;
}
```

As the example above shows, before assigning the value of the `age` variable to the `sVar` variable, we used explicit casting via parentheses and the type of the target-data-type in that parentheses (`short` in this case) all set on the left side of the value.

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

(int) 23.222; // Now the value is treated as integer.

(short) 223433; // Now the value is treated as short.

(float) 233.421; // Now the value is treated as float.

There are other types of casing as well. For example dynamic_cast, upcasting, downcasting etc. which you’ll learn about them in later sections.
