/* Week 3 code sample for CPS125. Ferworn Fall 06 Casting types We discussed that C understands things like order of operations and it will let you do mixed type (floats and ints together) operations on data. Normally C will attempt to turn anything that is an int into a float when it has to. Sometimes this can lead to trouble as C often has different ideas about what "has to" means. Remember the type conversion program? Here it is again and it still won't work. /* Temp Conversion: write a program to convert temperatures from C to F F = 32+ 9/5(C) #include <stdio.h> void main() { float f,c; printf("Enter the temperature in degree C\n"); scanf("%f", &c); f = 32 + 9/5 * c; printf("The F temp is %f\n",f); } We can fix it by changing the ints to floats or we can tell C to do it for us using something called a "cast". A cast is an explicit instruction to C telling it to coerce or change to a type. You do it by putting the type (in round brackets) of what you want to change the data to before the actual data. See how it works in the new temp conversion program below. */ #include <stdio.h> int main() { float f,c; printf("Enter the temperature in degree C\n"); scanf("%f", &c); f = (float) 32 + (float) 9/5 * c; printf("The F temp is %f\n",f); }