Tuesday, February 3, 2009

GCC / ANSI C stanadards..

Hi friends ,
i want to share one more thing with you people..

first of all something about ANSI C standard,

It depicts that when we write code in C , it should be according to the standards described by ANSI. for example -: all the variables should be declared before their scope begins .

the code according to the Ansi C standard -:
int main()
{
int i ,j;
printf("%d %d",i,j);

}


int main()
{
int i=29;
printf("%d ", i);

{
int j=30; /* Even this would work fine as the definition says about scope ,not about program*/
printf("%d",j);
}
}


the code which is not according to the ANSI C standard -:

int main()
{

printf("Hello World ");
int j ; /* This is incorrent according to ANSI C standard */
}



Now the questions is , the most popular C compilers like GCC , do they follow the rules of ANSI C standards ?

because the the second example also gets compiled by Gcc.



Well , GCC supports ANSI C standard ,but by default they provide some optimization over standard C. That is why the later version of the code gets compiled . But what if you want that ur codde is according to the ANSI standard. Actually there are options for this in GCC .

$ gcc -pedantic test.c

The above will generate the rules violations as warning but will generate the object code as well as the the executable.

$ gcc -pedantic-errors test.c

This will generate the violations as errors .


this is it for today ..
Enjoy.

No comments:

Post a Comment