我目前正在Linux Mint我的机器上运行GCC-5.3因为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);
}
Run Code Online (Sandbox Code Playgroud)
在编译时,会收到以下警告:
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]
}
^
Run Code Online (Sandbox Code Playgroud)
如果我们看到这部分警告:
warning: string length ‘510’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]
Run Code Online (Sandbox Code Playgroud)
在某种程度上清楚-pedantic标志是一个问题在这里所以我决定不使用它,并避免它,-ansi因为新的(最后)标准也避免C11.
现在,如果我编译相同的程序GCC-5.3:
gcc-5 -Wall -pedantic program.c -o program
Run Code Online (Sandbox Code Playgroud)
程序编译好,没有警告.
现在基于以下问题在C和C++中返回void类型,如果我尝试编译以下程序:
#include <stdio.h>
#include <string.h>
void f(void);
void f2(void);
int main(void){
f();
}
void f(void){
}
void f2(void){
return f();
}
Run Code Online (Sandbox Code Playgroud)
具有以下内容:
gcc-5 -Wall -pedantic program.c -o program
Run Code Online (Sandbox Code Playgroud)
我明白了:
program.c: In function ‘f2’:
program.c:16:16: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
return f();
^
Run Code Online (Sandbox Code Playgroud)
但是没有"迂腐"的旗帜就可以编好.这对我来说很困惑.
这告诉我,我确实需要-pedantic国旗,但我并不痛.
所以,我的问题是,我们是否需要 -pedantic 再使用C11?
Kei*_*son 11
如果您需要它,您需要它.如果你不这样做,你就不会.
gcc的-pedantic选项告诉它严格执行您要求的C标准规则.这会导致其他警告消息(如果使用则会导致致命错误-pedantic-errors).
问题是,您是否希望编译器警告您违反C标准要求的代码?
如果您希望您的代码尽可能便携,请使用-pedantic并密切关注它告诉您的任何内容.如果您希望代码依赖于非标准功能,请不要使用-pedantic- 但是您可能会冒不得使用其他编译器和/或不同目标系统编译代码的风险.
您遇到的具体消息是针对在C90和C11之间发生变化的事情.C11要求编译器在字符串文字中支持至少4095个字符; C90只需要509.(实际上,对于gcc,实际限制不是固定的,而是由编译时的可用内存强加的.标准中描述限制的方式并不那么简单,但我不会介入那个.)不过,你很少需要一个字符串文字.
C99添加了%zu打印类型值的格式size_t.如果您希望您的代码可以移植到C99之前的实现,那么您将需要避免使用它; 例如,你可以使用printf("%lu\n", (unsigned long)sizeof foo).实际上,大多数当前的实现都支持%zu.
在使用返回类型定义的函数中,不允许return使用带有表达式的语句,甚至是类型的表达式.这是你应该要的警告(恕我直言).voidvoid
底线:-pedantic如果您想严格执行C标准的规则,请使用.如果您不想这样做,请不要使用-pedantic.(但考虑编译代码-pedantic至少偶尔会清除它检测到的任何实际错误,除了你可能不关心的警告.)
最初的C C89/C90标准只要求编译器允许字符串文字长达509个字节.使用长度超过509字节的字符串编译为C90标准的代码不是最大可移植的; 符合标准的编译器可以拒绝代码.它在实践中不太可能成为问题,但理论上可能会发生.
限制在C99中提高到4095字节(并且在C11中保持不变).因此,您必须使用更长的字符串来使用C99或C11来限制极限.
GCC 4.x编译器默认使用C90标准.GCC 5.x编译器默认使用C11标准.因此,-pedantic在GCC 4.x下编译时,不能最大程度地移植到C90的代码将生成警告,但不会生成与GCC 5.x相同的警告,除非该构造也不能移植到所有C11编译器 - 除非它违反也是C11编译时限制之一.
该-pedantic标志有它的用途.例如,昨天有人遇到问题因为他们正在使用:
void *p = malloc(sizeof(*p));
Run Code Online (Sandbox Code Playgroud)
根据标准,这是错误的代码,但GCC(5.3.0专门测试,但其他5.x和4.x版本表现相同)允许它,解释sizeof(*p)为1.这对其他编译器来说是不可移植的.使用-pedantic报告问题; 不使用-pedantic不.
| 归档时间: |
|
| 查看次数: |
3520 次 |
| 最近记录: |