Welcome 微信登录

首页 / 操作系统 / Linux / Lnux下GNU C和standard C(ANSI/ISO)区分实例

第一:符合ansi标准的实例:[root@localhost ansi]# vim ansi.c  #include <stdio.h> int main(void){const char asm[] = "6502";        printf("The string asm is "%s" ",asm);        return 0;}首先用GNU C的标准来编译[root@localhost ansi]# gcc -Wall -O ansi.c -o ansi  //出现错误ansi.c: In function "main":ansi.c:5: error: expected identifier or "(" before "asm"ansi.c:6: error: expected expression before "asm"对比用STANDARD C的标准来编译[root@localhost ansi]# gcc -Wall -O -ansi -pedantic ansi.c -o ansi   //成功[root@localhost ansi]# ./ansi The string asm is "6502"第二:符合GNU C标准但是不兼容标准C标准的实例[root@localhost ansi]# vim pi.c #include <stdio.h>#include <math.h> int main(void){        printf("The value of pi is %f ",M_PI);        return 0;}首先用STANDARD C的标准来编译1、使用标准C来编译同时使用标准C的库[root@localhost ansi]# gcc -Wall -O -ansi -pedantic pi.c -o pi   //出现错误pi.c: In function "main":pi.c:6: error: "M_PI" undeclared (first use in this function)pi.c:6: error: (Each undeclared identifier is reported only oncepi.c:6: error: for each function it appears in.)2、使用标准C来编译但是使用GNU C的库(通过宏定义来实现) [root@localhost ansi]# gcc -Wall -o -ansi -D_GNU_SOURCE pi.c -o laji //成功[root@localhost ansi]# ./laji The value of pi is 3.141593对比用GNU C的标准来编译[root@localhost ansi]# gcc -Wall -O pi.c -o pi  //成功[root@localhost ansi]# ./pi The value of pi is 3.141593第三:两者都可以,但是会出现警告的实例[root@localhost ansi]# vim v.c  #include <stdio.h> int main(int argc,char* argv[]){int i,n=argc;double x[n];        for(i=0;i<n;i++)                x[i] = i;        return 0;}用GNU C的标准来编译[root@localhost ansi]# gcc -Wall -O v.c [root@localhost ansi]# ls a.       Out用STANDARD C的标准来编译1、不加pedantic参数[root@localhost ansi]# gcc -Wall -O -ansi v.c [root@localhost ansi]# ls a.out a.out2、加pedantic的参数[root@localhost ansi]# gcc -Wall -O -ansi -pedantic v.c v.c: In function "main":v.c:6: warning: ISO C90 forbids variable-size array "x"[root@localhost ansi]# ls a.out a.       out实验结束Linux 下GCC的于处理器CPP使用实例Ubuntu 11.10外接显示器遇到的问题相关资讯      Lnux基础教程  本文评论 查看全部评论 (0)
表情: 姓名: 字数