Type Conversion in C
Type Conversion in C
Type Conversion in C
Type Conversion in C
The C compiler attempts data type conversion, especially when dissimilar data types
appear in an expression. There are certain times when the compiler does the
conversion on its own (implicit type conversion) so that the data types are
compatible with each other. On other occasions, the C compiler forcefully performs
the conversion (explicit type conversion), which is carried out by the type cast
operator.
While performing implicit or automatic type conversions, the C compiler follows the
rules of type promotions. Generally, the principle followed is as follows −
Integer Promotion
Integer promotion is the process by which values of integer type "smaller" than int
or unsigned int are converted either to int or unsigned int.
Example
#include <stdio.h>
int main(){
int i = 17;
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 1/8
6/16/24, 11:00 AM Type Conversion in C
int sum;
sum = i + c;
return 0;
}
Output
When you run this code, it will produce the following output −
Here, the value of sum is 116 because the compiler is doing integer promotion and
converting the value of "c" to ASCII before performing the actual addition operation.
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 2/8
6/16/24, 11:00 AM Type Conversion in C
Example
#include <stdio.h>
int main(){
char a = 'A';
float b = a + 5.5;
printf("%f", b);
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 3/8
6/16/24, 11:00 AM Type Conversion in C
70.500000
When the above code runs, the char variable "a" (whose int equivalent value is 70) is
promoted to float, as the other operand in the addition expression is a float.
C provides a typecast operator. You need to put the data type in parenthesis before
the operand to be converted.
Note that if type1 is smaller in length than type2, then you don’t need such explicit
casting. It is only when type1 is greater in length than type2 that you should use the
typecast operator.
Example
#include <stdio.h>
int main(){
int x = 10, y = 4;
float z = x/y;
printf("%f", z);
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 4/8
6/16/24, 11:00 AM Type Conversion in C
2.000000
While we expect the result to be 10/4 (that is, 2.5), it shows 2.000000. It is because
both the operands in the division expression are of int type. In C, the result of a
division operation is always in the data type with larger byte length. Hence, we have
to typecast one of the integer operands to float, as shown below −
Example
#include <stdio.h>
int main(){
int x = 10, y = 4;
float z = (float)x/y;
printf("%f", z);
return 0;
}
Output
2.500000
If we change the expression such that the division itself is cast to float, the result
will be different.
Typecasting Functions in C
The standard C library includes a number of functions that perform typecasting.
Some of the functions are explained here −
The atoi() function converts a string of characters to an integer value. The function is
declared in the stdlib.h header file.
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 5/8
6/16/24, 11:00 AM Type Conversion in C
Example
The following code uses the atoi() function to convert the string "123" to a number
123 −
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("%d\n", num);
return 0;
}
Output
123
You can use the itoa() function to convert an integer to a null terminated string of
characters. The function is declared in the stdlib.h header file.
Example
The following code uses itoa() function to convert an integer 123 to string "123" −
#include <stdio.h>
#include <stdlib.h>
int main(){
itoa(num,str, 10);
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 6/8
6/16/24, 11:00 AM Type Conversion in C
printf("%s\n", str);
return 0;
}
Output
123
In function arguments and return values − You can apply the typecast operator
to formal arguments or to the return value of a user-defined function.
Example
Here is an example −
#include <stdio.h>
#include <stdlib.h>
int main(){
int x = 10, y = 4;
float z = divide(x, y);
printf("%f", z);
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 7/8
6/16/24, 11:00 AM Type Conversion in C
return (float)a/b;
}
Output
When you run this code, it will produce the following output −
2.500000
Employing implicit or explicit type conversion in C helps in type safety and improved
code readability, but it may also lead to loss of precision and its complicated syntax
may be confusing.
https://www.tutorialspoint.com/cprogramming/c_type_conversion.htm 8/8