I was born in the modern world, so I don't often need to deal with this sort of thing, but could someone explain how to get the correct number in the following code. Here is one attempt of many:
#define X 2527
#define Y 2463
#define Z 3072
main()
{
long int c = X*Y*Z;
printf("%ld",c);
}
I'm just trying to print a long integer but it is always printing the wrong result. Am I getting integer overflows - if so how can I prevent them? Or is it my choice of printf formatter?
Overflow is ok, because you trying 34 bit number write to 32 bit variable (long int
).
Use long long int
and %lld
in format string.
#define X 2527LL
#define Y 2463LL
#define Z 3072LL
main()
{
long long int c = X*Y*Z;
printf("%lld",c);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With