Can the type difference between constants 32768 and 0x8000 make a difference?

后端 未结 5 1942
攒了一身酷
攒了一身酷 2021-01-04 04:30

The Standard specifies that hexadecimal constants like 0x8000 (larger than fits in a signed integer) are unsigned (just like octal constants), whereas decimal constants like

5条回答
  •  孤独总比滥情好
    2021-01-04 05:02

    Assuming int is 16 bits and long is 32 bits (which is actually fairly unusual these days; int is more commonly 32 bits):

    printf("%ld\n", 32768);  // prints "32768"
    printf("%ld\n", 0x8000); // has undefined behavior
    

    In most contexts, a numeric expression will be implicitly converted to an appropriate type determined by the context. (That's not always the type you want, though.) This doesn't apply to non-fixed arguments to variadic functions, such as any argument to one of the *printf() functions following the format string.

提交回复
热议问题