r/learnprogramming • u/_suriyan_24 • Jul 18 '26
Can anyone explain exactly why I got these random values for a C simple program?
#include <stdio.h>
int main() {
float celsius = 20.5;
float fahrenheit = 0;
fahrenheit = (celsius \* 9/5) + 32;
printf("%d", fahrenheit);return 0;
}
#I know its coz of the %d %f mistake but whyyy
24
Upvotes
46
u/teraflop Jul 18 '26
The rules-lawyer answer is that using the wrong format specifier for
printfcauses "undefined behavior". You're not supposed to do it under any circumstances, and if you do, all bets are off. Your program can misbehave in arbitrary ways.Practically, the reason is that one of two things happens, depending on your system's ABI:
printfis expecting an integer passed in a CPU register, but you're passing a float in a different register, so it's reading random leftover garbageprintfis expecting a floating-point value on the stack, but you're passing an integer on the stack, so it's reinterpreting those floating-point bits as integer bits7
u/PuzzleMeDo Jul 18 '26
A floating point number is stored with the various bits representing a sign (+-), an exponent, and a mantissa. This is a completely different data structure from an integer. If you tell it, "Display this binary data as though it was an integer," it will print something weird.
4
u/Basic_Reporter9579 Jul 18 '26
It should be float values?
fahrenheit = (celsius * 9.0/5.0) + 32.0;1
u/bestjakeisbest Jul 18 '26
What value did you expect to get?
1
u/DTux5249 Jul 18 '26
why is there a '\' before your multiplication operator? That shouldn't be anywhere here.
2
u/ScholarNo5983 Jul 19 '26
I assume this line of code contained a typo:
as I had to change it to this to get the code to compile and link:
First point. You should always run the compiler with full warning enabled, as this will help catch these kind of errors:
Now if I compile, link and run this code I don't see any random values:
So at least for the Microsoft C compiler generating debug level of code, the numbers are wrong, but they remain predictable.
3
u/lurgi Jul 18 '26
9/5is1, because of integer division. You can fix that by making one (or both) of the values floating point.And in the future, please say what result you got and what result you expected. For all I know you are actually seeing a compilation error and I'm helping with the wrong thing.
1
u/SmokeMuch7356 Jul 18 '26
printfdoesn't know the number or types of arguments you pass after the format string; it only knows what to expect based on that format string. You told it to expect anint, so it looked forsizeof (int)bytes either on the stack or a specific register, and interpreted those bytes as anintvalue.If it's looking at a register, it won't be looking at the right one and will just read garbage.
If it's looking on the stack, it a) won't read the right number of bytes, and b) won't interpret them correctly.
1
u/MisterGerry Jul 18 '26
"these random values" - which random values? You didn't show the output.
1
u/JGhostThing Jul 19 '26
I think that the bigger problem is using \* .
1
u/noodle-face Jul 20 '26
You didn't tell us what values you're getting
0
u/HashDefTrueFalse Jul 18 '26 edited Jul 18 '26
int main()isn't a proper main signature before C23 (IIRC). Useint main(void)or add the usual parameters.%dtells printf to look for an integer in the integer register corresponding to the relevant argument. Most platforms use separate hardware for integer and floating point math. Withfahrenheitbeing a float, it's loaded into the relevant floating point register. Thus, the value you get from printf is whatever happened to be in the unset integer register at the time. Use the correct specifier e.g.%.2fto point printf at the correct register containing the bytes copied fromfahrenheit.All from a glance. There might be more I've missed.
Edit: I seem to have answered the question "how do I fix my code?" instead of the one OP asked. Oops. Explanation added to final point.
0
u/[deleted] Jul 18 '26
[deleted] — view deleted comment