为什么即使我评论了标题并使用了它的功能,我也不会收到错误?

时间:2012-11-02 03:59:40

标签: c gcc ctype

在以下程序中,我对语句#include <ctype.h>进行了评论,希望在使用isupperisgraph等函数时会抛出错误。但令我惊讶的是,没有任何错误被抛出。为什么会这样? wikipedia页面在标题类型ctype.h中列出了这些函数。

#include <stdio.h>
//#include <ctype.h>

int main() {
char ch;
for(;;) {
   ch = getc(stdin);
   if( ch == '.') break;
   int g = isgraph(ch);
   if(isupper(ch) != 0) printf("Is in upper case\n");
}   
return 0;   
 }

注意:我正在使用gcc在linux(fedora)上编译。

1 个答案:

答案 0 :(得分:3)

默认情况下,gcc以相当宽松的模式运行。您可以通过添加警告来获得警告,例如:

 gcc -Wall -c yourfile.c

要求所有主要警告。 (您可以添加更多警告:-Wextra添加一堆。)您也可以指定-std=c99(也许-pedantic)以获得更多警告。

C99要求在使用函数之前定义或声明函数。

$ gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -c warn.c
warn.c:4:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
warn.c: In function ‘main’:
warn.c:4:5: warning: old-style function definition [-Wold-style-definition]
warn.c:9:4: warning: implicit declaration of function ‘isgraph’ [-Wimplicit-function-declaration]
warn.c:10:4: warning: implicit declaration of function ‘isupper’ [-Wimplicit-function-declaration]
warn.c:9:8: warning: unused variable ‘g’ [-Wunused-variable]
$

这是GCC 4.7.1(在Mac OS X 10.7.5上)的输出,带有我使用的标准编译选项集 - 在源代码中运行,存储在文件warn.c中。

相关问题