r/C_Programming • u/Guilty-Tomorrow-6134 • Jul 05 '26
Question How does explicit conversion work with assignment operators
I am studying c, I was messing around trying to figure out how explicit conversion works with assignment operators and came up with this:
#include <stdio.h>
int main()
{
int num1 = 5;
int num2 = 2;
float sum1 = num1 / num2;
float sum2 = (float) num1 / num2;
int sum3 = 5;
sum3 /= 2;
float sum4 = 5;
sum4 /= 2;
printf("sum1: %.2f\nsum2: %.2f\nsum3: %.2f\nsum4: %.2f", sum1, sum2, sum3, sum4);
return 0;
}
OUTPUT:
sum1: 2.00
sum2: 2.50
sum3: 2.50
sum4: 2.00
it seems sum3 converts to float automatically without explicit conversion when using assignment operators, but what confuses me is how sum4 manages to be 2 when its only difference was that it was declared as a float which if anything should make it behave better than sum3, is it something to do with the format specifier being wrong for sum3?
2
u/TheThiefMaster Jul 05 '26
Floating point values are often passed into the function in dedicated registers*. So my guess is that when you specify %f to print "sum3", it's actually taking the 3rd float argument, not the 3rd argument generally. This ends up printing sum4 (which is in fact 2.5).
Something similar happens trying to print sum4 which picks up a 2.0 from a register it wasn't intending to use as a function argument but happens to contain 2.0.
* the specifics of how arguments are passed to variadic functions vary by architecture, OS, and even compiler.
TLDR don't use the wrong specifier.
7
u/Severe-Reality5546 Jul 05 '26
The guess about wrong format specifiers is correct. The format string must match the types of values being passed. When they don't match, you're invoking undefined behavior, and the output is unpredictable.
1
u/Guilty-Tomorrow-6134 Jul 05 '26
I had a friend of mine run the program on his pc and the output he got was always 2.5 for sum3 but sum4 could be 2.5, 0.000000%, -nan% or just copy the value of sum1 no matter what it was...
1
u/Guilty-Tomorrow-6134 Jul 05 '26
is there some way you could get explicit conversion to work with assignment operators?
int sum3 = 5;
sum3 /= (float) 2;
would something like this work?
2
u/o4ub Jul 05 '26
There must be a mistake, sum3 is an integer, the result cant be 2.5, while sum4 is a float, so there is no reason for the result to be 2. Recompile, rerun, check the code if it persists.
2
u/sciencekm Jul 05 '26 edited Jul 05 '26
What you are seeing are just whatever artifacts are in the floating point registers after they were used in computations before calling printf. This is because of the mismatch between the printf specifiers and the value parameters.
In Linux, integers are passed on the registers and stack. The floating point values are passed int the xmm registers and the stack.
In your case, before calling printf:
sum1 was copied to xmm0
sum2 was copied to xmm1
sum3 (an integer) was copied to EDI
sum4 was copied to xmm2
Inside printf, it outputs the first two values correctly.
For the third value, it expects a floating point value in xmm2, but that is where sum4 went, and so you see sum4 as the third value printer.
The fourth value (another float) is taken from xmm3, which is simply junk and whatever was the last value there when it was used.
Looking at the asm output, xxm3 was used as temp when sum1 was computed. And thus you see sum1 as the fourth output.
Edit: Let me add that in other environment (like Windows), you will get a different effect because the calling convention is different. In any case, your output format specifiers should match your parameters to get correct results.