【发布时间】:2016-06-29 21:41:41
【问题描述】:
我目前正在使用GCC-5.3 在我的机器上运行Linux Mint,因为默认包含C11。
我开始为自己学习C 只是为了好玩,如果我没记错的话,当时GCC 的版本是4.8。
如果有人在以下程序中使用GCC-4.8 和-pedantic 标志,那么无论如何:
#include <stdio.h>
#include <string.h>
int main(void){
char *arr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
size_t length = strlen(arr);
printf("Length of Arr = %zu\n",length);
}
在编译时会收到以下警告:
program.c: In function ‘main’:
program.c:5:5: warning: string length ‘510’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]
char *arr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
^
program.c:7:5: warning: ISO C90 does not support the ‘z’ gnu_printf length modifier [-Wformat=]
printf("Length of Arr = %zu\n",length);
^
program.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
如果我们看到这部分警告:
warning: string length ‘510’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]
不知何故,-pedantic 标志在这里是一个问题,所以我决定不使用它并避免它,就像避免 -ansi 一样,因为新的(最后一个)标准 C11。
现在如果我用GCC-5.3编译相同的程序:
gcc-5 -Wall -pedantic program.c -o program
程序编译良好,没有警告。
如果我尝试编译以下程序,现在基于以下问题Return void type in C and C++:
#include <stdio.h>
#include <string.h>
void f(void);
void f2(void);
int main(void){
f();
}
void f(void){
}
void f2(void){
return f();
}
以下内容:
gcc-5 -Wall -pedantic program.c -o program
我明白了:
program.c: In function ‘f2’:
program.c:16:16: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
return f();
^
但是没有 ´-pedantic` 标志也能正常编译。这让我很困惑。
这表明我确实需要-pedantic 标志,但我不痛。
所以,我的问题是,我们是否需要使用 -pedantic anymore 和C11?
【问题讨论】: