我只是一个很棒的编程难题。为什么是一样的?
#include <stdio.h>
#include <limits.h>
int main(int argc, char *argv[])
{
unsigned int x = ULONG_MAX;
char y = -1;
if (x == y) printf("That is same.");
return 0;
}
我认为 unsigned int 被转换为 signed char,因此它将是 -1。它可能是有符号和无符号类型比较的标准。我不知道……
最佳答案
在 signed char
和 unsigned int
之间的较量中,unsigned int
获胜!
是这样的
此处 -1
将被转换为 unsigned int
,即 ULONG_MAX
,因此 if()
条件为是的。
在 C 语言中,大小很重要。变量总是转换为其中最大的大小。
关于两个变量的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10419280/