gpt4 book ai didi

c - c中的无符号整数比较

转载 作者:行者123 更新时间:2023-12-04 16:04:48 26 4
gpt4 key购买 nike

int main()
{
unsigned int c = -1;
char *s = "Abc";
char *x = "defhe";
((strlen(s)-strlen(x))>c)? printf(s): printf(x);
}

c的值为4294967295(strlen(s)-strlen(x))的值为4294967294 它应该打印 x 但它打印的是 s 值。我不明白为什么会这样

最佳答案

the value of c is 4294967295 and the value of (strlen(s)-strlen(x)) is 4294967294

(strlen(s)-strlen(x)) 产生 4294967294 不一定是真的。这取决于您系统上 SIZE_MAX 的值。

如果 SIZE_MAX18446744073709551615(通常在 64 位系统上),则 (strlen(s)-strlen(x)) 将是 18446744073709551614 明显大于 4294967295(假设 UINT_MAX4294967295)。因此,您会看到 printf(s); 被打印出来。

使用 printf() 语句查看值并理解:

printf("%zu %u\n", strlen(s)-strlen(x), c);

关于c - c中的无符号整数比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42228787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com