I'm fairly new to programming, and am starting to get used to C. I apologize if this is a repeat question (I don't know the underlying process i.e. what to search for).
I'm working with a simple program to get used to the nuances of data types:
main(){
int i;
float a,b;
i = 2;
a = 2.0;
b = 4.0;
printf("%d %1.1f", i/b,a/b);
}
I expected the program to print 0 0.5 (since a and b are both floats and I am printing their ratio as a float), yet the program printed 0 0.0 (I'm using gcc -o). However, when I reverse the printf order (without switching the order of corresponding variables), that is:
printf("%1.1f %d", i/b,a/b);
The print result is 0.5 0. I'm not exactly sure what's happening here. It appears that in the first program b is being converted to int in i/b and fails to be converted to float in a/b. However, in the second variant, b doesn't have any trouble printing out as two different types. Can ints not be coerced into floats? Can someone explain this or point me in the right direction?
Thanks!