让我们考虑以下计划test.c:
#include <stdio.h>
struct test {
unsigned int a:5;
};
int main () {
unsigned int i;
struct test t = {1};
for (i = 0; i < t.a << 1; i++)
printf("%u\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时gcc -Wsign-compare test.c生成以下警告(使用gcc 4.8.1测试):
test.c:9:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < t.a << 1; i++)
^
Run Code Online (Sandbox Code Playgroud)
clang -Wsign-compare test.c 产生以下内容(使用clang 3.2进行测试):
test.c:9:19: warning: comparison of integers of different signs: 'unsigned …Run Code Online (Sandbox Code Playgroud)