문제

#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.

도움이 되었습니까?

해결책

You need to declare test before you call it, otherwise C assumes it returns int.

다른 팁

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top