#include<stdio.h>
#include<conio.h>
int main()
{
short a=5;
long int b=123456;
float c=234.56;
double d=234567.78695;
printf("E=%12f",((a+b)*c)/d); // 12 space//
getch();
}
/* float contains 32 bits(7 digits)
double contains 64 bits(15 gigits)*/
In the above program, we first declare and initialize variables of different data types, short, long int, float, and double. The variable ‘a’ is of type short and is initialized with the value 5, ‘b’ is of type long int and is initialized with the value 123456, ‘c’ is of type float and is initialized with the value 234.56, and ‘d’ is of type double and is initialized with the value 234567.78695.
Then we use the printf statement to display the result of the expression ((a+b)*c)/d. The %12f format specifier is used to display the result as a floating-point number with 12 spaces.
In this program, we are demonstrating type conversion. The short variable ‘a’ is implicitly converted to long int, as it is being added to the long int variable ‘b’. Similarly, the result of this addition, which is a long int, is then implicitly converted to a float as it is being multiplied with the float variable ‘c’. Finally, the result of this multiplication, which is a float, is then implicitly converted to a double as it is being divided by the double variable ‘d’.
It is important to note that in C, when two variables of different data types are used in an expression, the variable with a smaller range is automatically converted to a variable with a larger range, to avoid loss of data. This process is called type promotion or type coercion.
Thanks