有人能够解释一下以下代码的输出吗:
void myprint(unsigned long a)
{
printf("Input is %lx\n", a);
}
int main()
{
myprint(1 << 31);
myprint(0x80000000);
}
使用 gcc main.c
命令可以输出:
Input is ffffffff80000000
Input is 80000000
为什么 (1 << 31)
被视为有符号的,而 0x80000000
则被视为无符号的?
0x80000000
。 - Jabberwockyint
时会导致未定义行为。 - too honest for this sitemyprint
函数中使用 1UL 而不是 1。myprintfunction(1UL << 31);
默认情况下,没有限定符的值被表示为整数。 - campescassiano