Updated March 31, 2023
Introduction to Type Conversion in C
Type conversion in C is defined as if we assign any data type to another data type and then call it “Type conversion”. Any programming language with lower data type values can automatically be cast into upper data type values. In this situation, there is no loss of data, whereas in the case of upper data type value into lower data type value there may chance of loss of data. Lower data type to upper data type can automatically be done by C compiler but upper data type to lower data typecasting, we must need explicit type conversion. Which is known as “Explicit conversion”. Let’s take an example of long value into int value is an explicit typecasting.
Why explicit conversion require bigger data types to smaller data types?
Let’s consider a small analogy then you will understand very clearly, there are 2 water bottles, one is 1 liter and the other one is 2 liters. We can easily pour 1 liter of water into 2 liters of the water bottle without overflowing. Same way if we try to pour 2 liters of water bottle water into 1 liter then there may be a chance of overflowing of water if a 2-liter water bottle contains more than 1 liter of water. So in this case, a 1-liter water bottle is the lower data type, and a 2-liter water bottle is the upper data type. Even if there is a chance of overflowing water still we want to pour 2 liters of water into a 1-liter water bottle we can pour so that customers must accept to do so. In the same way, developers have a clear-cut idea even if we try to convert the upper data type into a lower data type there may be a loss of data, so he must accept it.
Types of Conversions in C?
There are 2 types of casting in C.
- Implicit type conversion
- Explicit type conversion
1. Implicit type conversion
smaller data type to bigger data type conversion is said to be “Implicit type conversion “. This is automatically done by the C compiler. There is no loss of data.
Implicit type conversion:
Implicit type conversion diagram:
Syntax:
Bigger_dataType variableName=smaller_dataType_Value;
2. Explicit type conversion
bigger data type to smaller data type conversion is said to be “Explicit type conversion”. This is not automatically done by the C compiler. There may be a loss of data. This must be done by the developer explicitly.
Explicit type conversion:
Syntax:
Smaller_dataType variableName=(Smaller_dataType)Bigger_dataType_Value;
Examples of Type Conversion in C
Here are the following examples mentioned below
Example #1 – Implicit type conversion
Code:
//include the basic c libraries
#include<stdio.h>
int main() //main method to run the application
{
//declaring and initializing variable
int first = 214; //int variable
char character = 'p'; // character variable
// implicit conversion of character into integer
// ASCII value of 'p' is 112
int second= character;
// implicit conversion of integer into float
float third = first;
//display the implicit type conversion output
printf("Implicit conversion of character into integer is=> %d\n",second);
printf("Implicit conversion of integer into float is=> %f\n",third);
return 0;
}
Output:
Example #2 – Explicit type conversion
Code:
//include the basic c libraries
#include<stdio.h>
int main() //main method to run the application
{
//declaring and initializing variable
double firstDouble = 214.14; //double variable
float secondFloat=222.22; //float variable
// explicit conversion of double into integer
int intValue=(int)firstDouble;
// explicit conversion of double into float
float floatValue = (float)firstDouble;
//display the implicit type conversion output
printf("explicit conversion of double into integer is=> %d\n",intValue);
printf("explicit conversion of double into float is=> %f\n",floatValue);
return 0;
}
Output:
Example #3 – Auto explicit conversion
Code:
//include the basic c libraries
#include<stdio.h>
double getSquareArea(int a, int b);
int main() //main method to run the application
{
//declaring and initializing variable
double firstDouble = 214.14; //double variable
double secondDouble=222.22; //float variable
// we are passing arguments as double values
int mul=getSquareArea(firstDouble,secondDouble);
//display the implicit type conversion output
printf("Area of square is=> %d\n",mul);
return 0;
}
// Even we are not converting also compiler automatically convert double to int and take only inter part to multiplication
double getSquareArea(int a, int b)
{
return a*b;
}
Output:
Conclusion
Type conversion in C, there are 2 types of type castings are there, the 1st one is implicit type casting and the second one is explicit typecasting. Implicit type casting is automatically done by the compiler but explicit type casting developers must perform because in this case there may be a chance to lose data. The Latest C version explicit conversion is also done by the compiler.
Recommended Articles
This is a guide to Type Conversion in C. Here we discuss the Types of Conversions in C and Examples along with the codes and outputs. You may also have a look at the following articles to learn more-