linux下GNU C和standard C(ANSI/ISO)区分实例
时间:2014-10-30 12:03 来源:linux.it.net.cn 作者:it
第一:符合ansi标准的实例:
[root@localhost ansi]# vim ansi.c
#include <stdio.h>
int main(void)
{
const char asm[] = "6502";
printf("The string asm is '%s'\n",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\n",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 once
pi.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.out
2、加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
实验结束
(责任编辑:IT)
第一:符合ansi标准的实例: [root@localhost ansi]# vim ansi.c
#include <stdio.h>
int main(void) { const char asm[] = "6502"; printf("The string asm is '%s'\n",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\n",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 once pi.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.out 2、加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 实验结束 (责任编辑:IT) |