Typecasting
Typecasting
Example
What is Typecasting in C?
Typecasting is converting one data type into another one. It is also called
as data conversion or type conversion. It is one of the important concepts
introduced in 'C' programming.
#include<stdio.h>
int main(){
short a=10; //initializing variable of short data type
int b; //declaring int variable
b=a; //implicit type casting
printf("%d\n",a);
printf("%d\n",b);
}
Output
10
10
1. In the given example, we have declared a variable of short data type
with value initialized as 10.
2. On the second line, we have declared a variable of an int data type.
3. On the third line, we have assigned the value of variable s to the
variable a. On third line implicit type conversion is performed as the
value from variable s which is of short data type is copied into the
variable a which is of an int data type.
#include <stdio.h>
main() {
int number = 1;
char character = 'k'; /*ASCII value is 107 */
int sum;
sum = number + character;
printf("Value of sum : %d\n", sum );
}
Output:
Here, compiler has done an integer promotion by converting the value of 'k'
to ASCII before performing the actual addition operation.
#include <stdio.h>
main() {
int num = 13;
char c = 'k'; /* ASCII value is 107 */
float sum;
sum = num + c;
printf("sum = %f\n", sum );}
Output:
sum = 120.000000
First of all, the c variable gets converted to integer, but the compiler
converts num and c into "float" and adds them to produce a 'float' result.
We cannot perform implicit type casting on the data types which are not
compatible with each other such as:
1. Converting float to an int will truncate the fraction part hence losing
the meaning of the value.
2. Converting double to float will round up the digits.
3. Converting long int to int will cause dropping of excess high order
bits.
In all the above cases, when we convert the data types, the value will lose
its meaning. Generally, the loss of meaning of the value is warned by the
compiler.
In this case, after the division performed on variables var1 and var2 the
result stored in the variable "result" will be in an integer format. Whenever
this happens, the value stored in the variable "result" loses its meaning
because it does not consider the fraction part which is normally obtained in
the division of two numbers.
It requires a type casting operator. The general syntax for type casting
operations is as follows:
(type-name) expression
Here,
#include<stdio.h>
int main()
{
float a = 1.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %f\n", a);
printf("Value of b is %d\n",b);
return 0;
}
Output:
Value of a is 1.200000
Value of b is 2
Summary
Typecasting is also called as type conversion
It means converting one data type into another.
Converting smaller data type into a larger one is also called as type
promotion.
'C' provides an implicit and explicit way of type conversion.
Implicit type conversion operates automatically when the compatible
data type is found.
Explicit type conversion requires a type casting operator.
Keep in mind the following rules for programming practice when dealing
with different data type to prevent from data loss :