ANSI C vs other C standards

ubiquibacon picture ubiquibacon · Sep 24, 2010 · Viewed 23.2k times · Source

On several compilers I have used (all gcc but various versions) I get a C99 mode error for things like declaring int i inside the for loop expression instead of before it (if I do not use the std=c99 option). After reading here I understand that the gcc options -ansi, -std=c89, and -std=iso9899:1990 all evaluate to the ANSI C standard, but I don't understand why/if I should pick the c89 standard versus a newer standard like c99 (which is the newest I assume).

Also, I see multiple versions of the iso type standards for the C language, the first of which (from my understanding) is a direct port of the ANSI standard. Is is safe to say that iso will update their standard for C but the original ANSI standard for C will always be the same?

Bonus question:

I can actually figure this one out myself, I just haven't taken the time to do it yet, so if someone knows off the top of their head then that is great, otherwise no biggie, I'll figure it out later :)

I have a fairly new print of the book The C Programming Language (ANSI). My book always shows for loops like this:

int i;

for(i = 0; i < foo; i++)

but many people (most of who have more programming talent in their little finger) write their for loops like this:

(int i = 0; i < foo; i++)

Is it correct to say if I write the loop the first way then i should be accessible to the entire function, but if I write it the second way then i is only accessible to the for loop REGARDLESS of what standard I compile with? Another way of asking this same question, if I compile with the c89 standard will the i of both for loops be accessible to the entire function and if I compile with the c99 standard will the i of the first for loop be accessible to the entire function while the i of the second for loop will be accessible by only the for loop?

Answer

James McNellis picture James McNellis · Sep 24, 2010

C was standardized as C89 by ANSI. It was then standardized by ISO as C90; there are no technical differences between C89 and C90.

C99 is a revision of the C standard; it was developed by the ISO C committee and standardized by both ISO and ANSI. Both C89 and C99 are ANSI standards. In common parlance, the phrase "ANSI C" usually refers to C89; the K&R 2nd ed. book covers only C89, not C99.

Why would you choose to use an old version of C? Well, there are at least two reasons. First, you may have a compiler that doesn't support C99 (for example, Microsoft Visual C++ only supports C89). Second, there's a lot of legacy code out there that uses things from C89 that are not allowed in C99 (you can see more at the question "C99 backward compatibility"; that also links to the C99 rationale document that describes the differences).

As for the "bonus question," you can't declare a variable in a for-statement in C89; you can in C99. In C89, the first part of a for-statement is an expression.