Some of the primitive sata types in C are int, float, double, char etc. We know that int is a small data type and double is a large data type. We say this by comparing the range of the data types. The data type, it’s storage and range is given in following table.
|char||1 byte||-128 to 127 or 0 to 255|
|unsigned char||1 byte||0 to 255|
|signed char||1 byte||-128 to 127|
|int||2 or 4 bytes||-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647|
|unsigned int||2 or 4 bytes||0 to 65,535 or 0 to 4,294,967,295|
|short||2 bytes||-32,768 to 32,767|
|unsigned short||2 bytes||0 to 65,535|
|long||8 bytes||-9223372036854775808 to 9223372036854775807|
|unsigned long||8 bytes||0 to 18446744073709551615|
Implicit Type Conversion
C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to a proper type so that the expressions can be evaluated without losing any significant. This automatic type Conversion is known as Implicit Type Conversion.
During evaluation, it binds to very strict rule of type conversion. If the operands are of different types then the lower type automatically gets converted into higher type before the operation proceeds. The result is of higher type.
Consider the following expression in C,
x = x + y;
printf(“%d”, x) ;
Below is the sequence of rules that are applied while evaluating the expressions.
All short and char are automatically converted to int. Then,
- If one of the operands is long double, then the other will be converted i to long double and the result will be in Long double.
- Else, If ine of the operands is double, then the other will be converted into double and the result will be in double.
- Else, if one of the operands is float, then the other will be converted into float and the result will be in float.
- Else, if one of the operands is unsigned long int, then the other will be converted into unsigned long int and the result will be in unsigned long int.
- Else, if one of the operands is long int and the other is unsigned int, then 1. If unsigned int can be converted into long int, then unsigned int operand will be converted and the result will be in long int. 2. Else, both Operands will be converted into unsigned long int and the result will be unsigned long int.
- Else, if one of the operands is long int, the other will be converted into long int and the result will be in long int.
- Else, if one of the operands is unsigned int, the other will be converted into unsigned int and the result will be in unsigned int.
This automatic type conversion of lower data types into higher is called Implicit Type conversion. In implicit Type conversion there is no data loss occurs.
Explicit type conversion
The final result of an expression is converted to the type of the variable on the left of the assignment sign before assigning the value to it. However the following changes may occur during the final assignment.
- Float to int causes truncation of fractional part.
- Double to float causes rounding of digits.
- Long int to int causes dropping off the excess higher order bits.
So here in the above example, the right side expression of the assignment operator is evaluated and will be of type double. But this double value has to be assigned to the variable x, which is of type int, which is lower data type compared to double. The process to convert higher data types to lower types is called explicit type conversion. I explicit type conversion some data loss occurs as said in above there points.
so, to convert higher data types to lower, the syntax is:
lower_data_type_variable = (lower_data_type)higher_data_type_variable;
y = (int)d; //this gives double value in int by truncating fractional part.
printf(“%d”, y) ;