To convert the value of a variable to another type, you place the type you want to cast the value to in parentheses in front of the variable.
Thus, the statement to calculate the result correctly will be the following:
result = (float)my_variable/150*my_variable;
Example
#include <stdio.h>
//from ww w. ja v a 2 s. c o m
int main(void)
{
double result = 0.0;
int a = 5;
int b = 8;
result = (double)(a + b)/2 - (a + b)/(double)(a*a + b*b);
printf("%f", result);
return 0;
}
The code above generates the following result.
automatic type conversions
#include <stdio.h>
int main(void)
{ //w ww .j a va2 s .c o m
char ch;
int i;
float fl;
fl = i = ch = 'C';
printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl);
ch = ch + 1;
i = fl + 2 * ch;
fl = 2.0 * ch + i;
printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl);
ch = 1107;
printf("Now ch = %c\n", ch);
ch = 80.89;
printf("Now ch = %c\n", ch);
return 0;
}
The code above generates the following result.