Type Conversion in C
Type Conversion in C
A. Done by the compiler on its own, without any external trigger from the user.
B. Generally takes place when in an expression more than one data type is
present. In such conditions type conversion (type promotion) takes place to
avoid loss of data.
C. All the data types of the variables are upgraded to the data type of the
variable with the largest data type.
Implicit type conversion is also called automatic type conversion. Some of its
few occurrences are mentioned below:
● Conversion Rank
● Conversions in Assignment Expressions
● Conversion in other Binary Expressions
● Promotion
● Demotion
C
#include <stdio.h>
int main()
// value of 'a' is 97
x = x + y;
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
Output
x = 107, z = 108.000000
(type) expression
Type indicated the data type to which the final result is converted.
Example no 2
C
#include<stdio.h>
int main()
{
double x = 1.2;
Output
sum = 2
Example no 3
C
#include <stdio.h>
int main() {
float a = 1.5;
int b = (int)a;
return 0;
Output
a = 1.500000
b = 1