I want to divide two hex numbers:
for example: 88555680/10000
Now, from online hex calculators, I find:
And in python I find:
>>> hex(int("0x88555680",16)//int("0x10000", 16))
'0x8855'
Here is my C code:
# include <stdio.h>
int main() {
int a = 0x88555680;
int b = 0x10000;
int c = a/b;
printf("a%x, b%x, c%x\n", a, b, c);
}
With output:
a88555680, b10000, cffff8856
In C, I find 8856, while with Python, and online calculators, I find 8855
Question: Why is the C output not 8855? Why is it 8856? Should it not be 8855.something and then truncated towards 0, thus 8855?
I'm very new to C, it might be an obvious error.
On common systems int
is a 32-bit type whose max value is 0x7FFFFFFF
. Your value of 0x88555680
is out of range for int
. The compiler should warn you about this; if you don't see a warning then I recommend adjusting compiler settings.
Also your code causes undefined behaviour by using %x
to print an int
. The %x
specifier may only be used for unsigned int
.
To fix your code in this case use unsigned int
instead of int
for all 3 variables. If you want to deal with larger numbers you can use unsigned long long
, or uint64_t
. (In those cases you will need to update the printf format specifiers too).