ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
不符合ANSI/ISO的源代码
/*
*  pedant.c - use -ansi, -pedantic or -pedantic-errors
*/
#include <stdio.h>
void main(void)
{
  long long int i = 0l;
  printf("This is a non-conforming c program\n");
}
使用gcc pedant.c -o pedant这个命令时,编译器会警告main函数的返回类型无效:
$gcc pedant.c -o pedant
pedant.c:In function 'main':
pedant.c:7 warning:return type of 'main' is not 'int'
现在给gcc加上-ansi选项:
$gcc -ansi pedant.c -o pedant
$gcc pedant.c -o pedant
pedant.c: In function 'main'
pedant.c:7 warning: return type of 'main' is not 'int'
gcc再次给出了同样的警告信息,并忽略了无效的数据类型.

-ansi,-pedantic以及-pedantic-errors编译选项并不能保证被编译的程序的ANSI/ISO兼容性,它们仅被用来帮助程序员离这个目标更近.

$gcc -Wall pedant.c -o pedant
pedant.c:7: warning: return type of 'main' is not 'int'
pedant.c: In function 'main':
pedant.c:8 warning: unused variable 'i'
-Wall系列选项的另一个极端是-w选项,它能关闭所有的警告信息。-W{warning}选项的作用是打开warning所指出的用户感兴趣的特殊警 告信息,比如隐身函数说明(-Wimplicit-function-declaration)或者隐身声明返回类型的函数(-Wreturn- type).前者的作用在于它提示出定义了一个函数却没有事先声明或者忘记了包含适当的头文件.后者的作用在于指出声明的函数可能没有指定它的返回类型, 此时默认的返回类型为int.

提示:如果只想检查语法而实际上不做如何编译工作,那么可以调用带有-fsyntax-only参数的GCC.

选项                          说明
-Wcomment                   如果出现了注解嵌套(在第一个/*之后又出现了第二个/*)则发出警告
-Wformat                     如果传递给printf及及其相关函数 的参数和对应的格式字符串中指定的类型不平配则发出警告
-Wmain                       如果main的返回类型不 是int或者调用main时使用的参数数目不正确则不出警告
-Wparentheses               如果在出现了赋值(例如,(n=10))的地方使用了括号,而那里根据前后关系推断应该是比较(例如,(n==100))而非赋值的时候,或者如果括号能够解决运算优先级问题的时候,发出警告
-Wswitch                     如果swtich语句少了它的一个或多 个枚举值的case分支(只有索引值是enum类型时才适用)则发出警告
-Wunused                     如果声明的变量没有使用,或者函数声明 为static类型但却从未使用,则发出警告
-Wuninitialized             如果使用的自动变量没有初始化则发出警告
-Wundef                     如果在#if宏中使用了未定义的变量作判断则发出警告
-Winline                     如果函数不能被内联则发出警告
-Wmissing-declarations      如果定义了全局函数但却没有在任何头文件中声明它,则发出警告
-Wlong-long                 如果使用了long long类型则发出警告
-Werror                     将所有的警告转变为错误


常见的编程错误:
/*
*  blunder.c - Mistakes caught by -W(warning)
*/
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int i, j;
  printf("%c\n", "not a character");  /* -Wformat */
  if(i = 10)                          /* -Wparenthesses */
    printf("oops\n");
  if(j != 10)                         /* -Wuninitialized */
    printf("another oops\n");
  /* /* */                            /* -Wcomment */
  no_decl();                          /* -Wmissing-declaration */
  return (EXIT_SUCCESS);
}

void no_decl(void)
{
  printf("no_decl\n");
}
$gcc blunder.c -o blunder
blunder.c:27: warning: type mismatch with previous implicit declaration
blunder.c:21: warning: previous implicit declaration of 'no_decl'
blunder.c:27: warning: 'no_decl' was previously implicitly declared to return 'int'
gcc只发出了和隐式声明no_decl函数有关的警告.它忽略了其他潜在的错误,这些错误包括:
.传递给printf的参数类型(一个字符串)和格式化字符串定义的类型(一个字符)不匹配.这会产生一个-Wformat警告.
.变量i和j使用前都没有初始化.它们中的任何一个都产生-Wuninitialized警告.
.在根据前后关系推断应该对变量i的值做比较的地方,却对变量i进行赋值.这应该会产生一个-Wparentheses警告.
.在注释嵌套开始的地方应该会产生一个-Wcomment警告.
posted on 2005-08-04 22:03 ivaneeo 阅读(5172) 评论(0)  编辑  收藏 所属分类: gcc小试牛力

只有注册用户登录后才能发表评论。


网站导航: