int 与 unsigned int 之间不能比较大小
#include <stdio.h>
int main(){
int a = -1;
int b = 1;
unsigned int c = 1;
printf("%d\n",a<b);
printf("%d\n",a<c);
return 0;
}
运行上述代码,结果:
1
0
可见 int类型是不能与unsigned int 类型的数据直接比较的。
这就产生了一些隐患。
常用的计数类型的库函数例如 strlen() 返回值都是unsigned int 类型。
他们的返回值是统统不能和负数的 int 类型数据正常比较的,返回值会出错。
解决方案就是将 unsigned int 类型的函数返回值强转为 int
大家出现这样的问题不要慌哈!