【问题标题】:Why do not I get an error even when I have commented the header and used its function?为什么即使我已经注释了标题并使用了它的功能,我也没有收到错误消息?
【发布时间】:2012-11-02 03:59:39
【问题描述】:

在下面的程序中,我注释了#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) 上编译。

【问题讨论】:

  • C 隐式声明函数。

标签: c gcc ctype


【解决方案1】:

默认情况下,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 中隐藏的源代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 2020-10-13
    • 2022-06-12
    • 2021-11-06
    • 2021-01-29
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多