Type Casting in C & Types of Types Casting [ With Examples ]
Today we are going to learn in detail about what is Type Casting in C language. But before that, you need to know what are variables and what are Data types. You can read about variables and data types from here -: What is variable and What is data types.
What is Type Casting in C Language
Type Casting is a way to change the data types of a variable. Using Type Casting in C language, we can convert a variable from one Data Type to another data type and when we do this,then it is called Type Casting or Type Conversion.
Let’s understand Type Casting with these examples.
Example of Type Casting in C Language
#include <stdio.h>
void main ()
{
int x; // Integer variable x
float y = 10.30; // Float variable y
x = y; // Type Cast float type Value into integer type
printf ("x = %d", x);
}
Output -:
x = 10;
In the above example, we first created a variable x of int data type by int x, then float y made another variable y, and assigned a value of 10.30 to the variable y.
After that, we assigned the value of the y variable to the x variable by doing x = y, Which brought the value of y variable to the variable x.
Only integer type data come in the variable created with the help of int data type, so when we pass the value of variable y created from float data type to int variable x by x = y. So instead of going 10.30 value in variable x, only 10 value will be gone which will cause loss of .30 data.
So one thing to note here is that when we do Type Casting, then we should cast the value of lower data type to a variable of higher data type
For example, the value of a variable created with int data type should be floated or cast to a variable made of double data type. So that such data loss can be prevented.
Such type casting is called Implicit Type Casting or Implicit Type Conversion, which I will tell you in more detail later in this post.
Let’s look at another example to understand Type Casting better.
Example of Type Casting In C
Without Type Casting
#include <stdio.h>
void main ()
{
int a = 10;
int b = 3;
float c;
c = a/b;
printf ("c = %f", c);
}
Output -:
c = 3.000000
With Type Casting
#include <stdio.h>
void main ()
{
int a = 10;
int b = 3;
float c;
c = (float)a/b;
printf (c = " %f", c);
}
Output -:
c = 3.333333
After Type Casting, we got kind of result that we wanted. we have not lost any kind of data. This type of casting is called Explicit Type Casting.
Types of Type Casting in C Language
There are two types of Type Casting in C language.
- Implicit type casting
- Explicit Type Casting
Implicit Type Casting
In Implicit Type Casting, we don’t need any operator for Type Casting. Implicit Type Casting is automatically done by the compiler. Such type casting is also called Type Conversion.
In implicit type casting, when we convert a variable from a lower data type to a higher type, then there is no data loss.
But when we typecast a variable of higher type in lower type then there is a risk of data loss, so whenever we are casting implicit type, then we type the variable from lower type to higher type itself, so that somehow there is no risk of data loss.
Let us understand this thing with an example.
Example of Implicit Type Casting -:
Converting a variable into lower data type to higher data type
#include <stdio.h>
void main ()
{
int x = 34;
float y;
y = x;
printf ("y = %f", y);
}
Output -:
y = 34.000000
In this example, We cast the value of int type variable x to float data type, store it in float variable y, print the value of y and the result came to 34.000000 which means that no data was lost in this type casting
Let’s see another example of Explicit Type Casting, in which we will type higher variable type in lower type.
Example of Implicit Type Casting -:
converting a variable into a higher data type to a lower data type
Without Type Casting
#include <stdio.h>
void main ()
{
int a = 5
int b;
float c = 22.55;
b = a + c;
printf ("b = %d", b);
}
Output –
b = 27
In this example, we have created two variables of int data type and assigned 5 values to the first variable a, and declare the second variable b by dropping it.
Then created another variable of float type c, assigning a value of 22.55. After that b = a + c and adding the value of both a and c variable and store it in b variable. Then the value of b is printed by the printf () function. Whose result was 27.
While its result should have come to 27.55, but it was not done, due to its lack of data in this type casting.
So if you don’t want data loss in any way, then always typecast the variable from lower type to higher type. Which will not cause data loss like in the earlier example, we saw that there is no loss of data on converting variable type from lower type to higher type.
Explicit Type Casting
Explicit Type Casting is user defined typecasting because the user himself forces this type casting. Explicit Type Casting is not automatically done by the compiler. In this, the “()” operator is used for Type Casting.
Let’s see an example of Explicit Type Casting.
Example of Explicit Type Casting
#include <stdio.h>
void main ()
{
int x = 111;
int y = 31;
float z;
z = (float)x/y; // Explicit Type Casting
printf ("z = %f", z);
}
Output -:
z = 3.580645
Read More -:
- Data Types in C
- Format specifier in C
- Escape Sequence in C
- Tokens in C
- Compilation Process in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that you have found the answer of your question and you will not have to search about the Types casting or type conversion in C Language
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Type casting in C Language
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.