开发者

How can I fix the cause of this compiler warning?

The following code is producing a warning when I compile it on 32-bit systems: 1087: warning: integer constant is too large for "long" type; how can I fix this so I don't get that warning and it works correctly on 32-bit?

A valid input for this is:

unsigned char str[] = "\x00\x17\x7c\x3a\x67\x4e\xcb\x01";

and the mypow function returns unsigned long long.

     unsigned long long high, low, nano;
     high = // line 1087
         (str[7]&0xff) * mypow(2,56) +
         (str[6]&0xff) * mypow(2,48) +
         (str[5]&0xff) * mypow(2,40) +
         (str[4]&0xff) * mypow(2, 32);
     low =
         (str[3]&0xff) * mypow(2,24) +
         (str[2]&0xff) * mypow(2,16) +
         (str[1]&0xff) * mypow(2,8) +
         (str[0]&0xff);
     nano = ((开发者_如何学Gohigh + low)/10000000) - (unsigned long long)11644473600;
     return localtime((time_t*)&nano);


If you are using a constant in your code that won't fit inside 32 bits, add an LL to the end of it so the compiler knows that it's supposed to be a 'long long' type. For example, the constant at the end of the nano = line should be 11644473600LL. The current code casts the constant to a long long, but the constant itself is a regular long since there is no explicit LL suffix.


It thinks the 1000000 is a 32-bit value. Be explicit about it being long:

nano = ((high + low)/10000000L) - 11644473600L;

EDIT: ULL would be the proper suffix.


I believe you need to do 11644473600ULL


The most likely correct answer to your question is that you must be using your C compiler in C89/90 mode (or maybe in C++ mode?). In both C89/90 and C++ the largest integer type is long. There's no such type as long long in these languages. Yet, some compilers support the long long as a non-standard extension. When you use a constant that is too large for long type in your code the compiler has to either reject your code or promote the constant to the non-standard long long type. In the latter case the warning is issued in order to inform you about the non-standard type being implicitly introduced into your code.

While the solution with LL or ULL suffix might work to suppress the warning, if you really want to use the long long type in your code I'd still recommend you to compile your code in C99 mode, where this type is standard. In C99 mode the warning should disappear by itself, regardless of any suffixes.


you should use the stdint.h defined integer types for cross platform portability.

for example you should try using uint64_t instead of unsigned long long. That way you know exactly how many bits you are getting and it is the same on all platforms.


Use -std=c99 on the gcc command line and the warning will go away, because C99 has well-defined behavior for integer constants this large.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜