Type Casting in C
On this page, we will learn about type casting, how to use it, types of type casting, implicit type casting, explicit type casting, widening type casting, narrowing type casting, sign conversion type casting, and an example of type casting in C.
What is type casting in C?
Type casting in C refers to the conversion of one data type to another. This is sometimes necessary when performing arithmetic or other operations, as C requires that operands have the same data type.
Type casting can be implicit or explicit. Implicit type casting occurs when the compiler automatically converts one data type to another. For example, if you add an integer and a float, the integer will be implicitly converted to a float before the addition is performed. Explicit type casting, on the other hand, is when the programmer manually converts one data type to another using the cast operator.
The cast operator is denoted by enclosing the desired data type in parentheses and placing it before the operand to be converted. For example, if you want to convert an integer variable x to a float, you can use the following syntax:
float y = (float) x;
This tells the compiler to treat the integer value stored in x as a float, and then assign it to the variable y.
It is important to note that type casting can result in a loss of precision or even data loss, depending on the data types being converted. Therefore, it should be used with caution and only when necessary.
What are the types of type casting in C?
In C, there are different types of type casting, each with their own specific purpose. In this article, we will explore the different types of type casting in C.
- Implicit Type Casting: Implicit type casting, also known as type promotion, is when the compiler automatically converts a value of a smaller data type to a larger data type. This is done to prevent data loss or errors during computation. For example:
int a = 5; float b = 3.2; float sum = a + b; // Implicit type casting: a is promoted to float
- Explicit Type Casting: Explicit type casting is when the programmer manually converts a value of one data type to another data type using a type cast operator. The syntax of the type cast operator is as follows:
(type_name) expression
For example:float a = 3.2; int b = (int) a; // Explicit type casting: a is cast to int
- Widening Type Casting: Widening type casting is when a value of a smaller data type is converted to a larger data type, such as converting an int to a long. This is done automatically by the compiler during implicit type casting.
- Narrowing Type Casting:
Narrowing type casting is when a value of a larger data type is converted to a smaller data type, such as converting a long to an int. This type of type casting can result in data loss or errors if the value is too large to fit in the smaller data type. Narrowing type casting must be done explicitly by the programmer using the type cast operator. For example:
long a = 123456789; int b = (int) a; // Narrowing type casting: a is cast to int, potential data loss
- Sign Conversion Type Casting: Sign conversion type casting is when a value of an unsigned data type is converted to a signed data type or vice versa. This type of type casting must be done explicitly by the programmer using the type cast operator. For example:
unsigned int a = 4294967295; // Max value for unsigned int int b = (int) a; // Sign conversion type casting: a is cast to int, potential data loss due to overflow
Let's see an example
#include<stdio.h>
int main(){
int a = 3;
int b = 4;
int total = a + b;
float median1 = total / 2;
printf("%f \n", median1);//'\n' is used to go to new line.
float median2 = (float)total / 2; //type casting.
printf("%f \n", median2);
printf("%.2f \n", median2); // .2f used for printing upto two decimal point.
return 0;
}
Output:
3.000000
3.500000
3.50
Overall, type casting is a useful tool in C programming that allows for greater flexibility and precision when working with different data types.