ANSI C vs other C standards
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
(w开发者_运维知识库hich 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?
I don't understand why/if I should pick the c89 standard versus a newer standard like c99 (which is the newest I assume).
A couple of reasons:
1) gcc's C99 support is not quite complete, whereas its C89 support is. That's why C89 (with GNU extensions) is the default. So if you're an absolute stickler for programming to a standard using gcc, pick C89.
2) Microsoft's compiler doesn't really support C99 at all. So if you want to write portable C code, C89 is a common 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?
No, ISO C99 was also ratified as an ANSI standard. The name "ansi" being attached to C89 only is an unfortunate historical accident. That said, C89 will always be C89, it's just not the most recent ANSI C standard.
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?
You can't write it the second way in C89 (i.e. with -pedantic
to adhere to the standard), so there is no "regardless of what standard". The versions of C with GNU extensions aren't standards, they're "dialects" (at least that's what the man page calls them). In C89 the second loop isn't legal, in C99 the second one confines the scope of i
to the loop. Obviously in both cases, the first loop gives i
a wider scope.
In fact, gcc doesn't like the second loop in C89 even with GNU extensions enabled.
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.
I don't have an indepth explaination for the C standards.
But my default stance has been to use C99 when given the chance. The fact that it was the latest developed standard is one reason (I have a misguided sense that "newer is better").
The main reason is so I can declare variables in for loops.
C99 valid:
for (int i = 0; i < 100; i++)
{
doSomething();
}
C89, ANSI and older:
int i;
for (i = 0; i < 100; i++)
{
doSomething();
}
The C99 implementation of gcc is not yet completed, but fairly usably in everyday programmers life. I don't have the reference at hand, but for gcc there is a statement somewhere that they will switch to C99 (or merely to their dialect gnu99) the day that this implementation is considered to be terminated.
The question of using C99 features or not, is divided in a pragmatic part:
- for which platforms I do have to compile my code
- what features would I gain (for my taste a lot of cleanness and ease to program portable)
and an emotional / ideological part. For the later, there is no cure but the application of the dinosaur principle.
精彩评论