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

4

4 回答 4

4

IIRC,C/C++ 中的 long long 常量后缀为“LL”。

long long test() {
    return 1111111111111111111LL;
}

您的编译器将您的常量视为 32 位长(如果您将常量模 2^32,则得到 734294471。)

于 2011-01-24T22:28:45.763 回答
4

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

于 2011-01-24T22:26:48.490 回答
1

尝试将 LL 添加到您的返回值:

long long test()
{   
    return 1111111111111111111LL;
} 
于 2011-01-24T22:29:28.117 回答
0

将后缀 LL 添加到您的文字中,看看会发生什么。据推测,编译器将文字转换为 int。您是否从编译器收到任何警告?

于 2011-01-24T22:30:05.540 回答