开发者

C++ preprocessor directive limits

I have a C++ pre-processor directive that is something like this:

#if (SOME_NUMBER开发者_JAVA技巧 != 999999999999999)
// do stuff
#endif

999999999999999 is obviously greater than 232, so the value won't fit into a 32-bit integer. Will the preprocessor correctly use a 64-bit integer to resolve the comparison, or will it truncate one or both of the values?


Try to use the LL suffix:

#if (SOME_NUMBER != 999999999999999LL)
// do stuff
#endif

In my gcc this work fine:

#include <iostream>

#define SOME_NUMBER 999999999999999LL

int main()
{

#if (SOME_NUMBER != 999999999999999LL)
    std::cout << "yes\n";
#endif

    return 0;
}

With or without the LL suffix.


You can try using the UINT_MAX constant defined in "limits.h":

#if (SOME_NUMBER != UINT_MAX)
// do stuff
#endif

UINT_MAX value varies depending on the integer size.


Preprocessor arithmetic works as normal constant expressions (see the Standard, 16.1/4), except that int and unsigned int are treated as if they were long and unsigned long. Therefore, if you have a 64-bit type, you can use it as normal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜