第一:符合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
實驗結束