如何让gcc抱怨char与256的比较

Jay*_*iya 2 c gcc gcc-warning

我在codegolf.stackexchange上找到以下代码来打印ASCII字符的代码表:

#include <stdio.h>

int main(){
    char i;
    for(i = 0; i < 256; i++){
        printf("%3d 0x%2x: %c\n", i, i, i);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

由于chars中存储了单个字节,因此它们始终存在< 256且循环永远不会终止.我想在编译时发现这一点.

很好,clang给出以下警告:

a.c:5:18: warning: comparison of constant 256 with expression of type 'char' is always true [-Wtautological-constant-out-of-range-compare]
for(i = 0; i < 256; i++){
           ~ ^ ~~~
Run Code Online (Sandbox Code Playgroud)

但是,既没有gcc也没有gcc -Wall任何形式的警告.是否有任何一组命令行选项可以打开这种警告?或者在gcc中不可能吗?

Mik*_*ski 6

-Wtype-limits(或-Wextra)应触发此警告