Returning a long long value
-
24-10-2019 - |
Question
#include <stdio.h>
int main(void)
{
long long x = test();
printf("%lld\n", x);
return 1;
}
long long test()
{
return 1111111111111111111;
}
The output is 734294471 . If I replace the call to test()
by a the number, the output is as I expect. I checked the value of x using a debugger and it wasn't set the to value returned by the function. What is going wrong?
I am using Visual Studio 2010 with the Visual C++ compiler.
Solution
You need to declare test
before you call it, otherwise C assumes it returns int
.
OTHER TIPS
IIRC, a long long constant in C/C++ is suffixed by 'LL'.
long long test() {
return 1111111111111111111LL;
}
Your compiler is treating your constant as a 32-bit long (if you take your constant modulo 2^32, you get 734294471.)
Try adding LL to your return value:
long long test()
{
return 1111111111111111111LL;
}
Add the suffix LL to your literal and see what happens. Presumably the compiler conberts the literal to an int. Are you getting any warnings from the compiler?