33

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?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • 1
    What makes you say most people write loops like that? It's certainly not valid C by the c89 standard. – Carl Norum Sep 24 '10 at 02:13
  • @Carl Norum I know it isn't valid in `c89` (assuming that by "that" you mean `int i` inside the for loop declaration). Almost everyone I have seen writes their loops like that. Has your observation been different? – ubiquibacon Sep 24 '10 at 02:15
  • this has to be duplicate of something, http://stackoverflow.com/questions/288441/variable-declaration-placement-in-c – Anycorn Sep 24 '10 at 02:15
  • @aaa carp the "bonus question" part of my question is kind of similar to that question, but I would not even call that a duplicate. My main question is totally different. – ubiquibacon Sep 24 '10 at 02:18
  • in my defense, I did not vote for closing, just making a suggestion. – Anycorn Sep 24 '10 at 02:20
  • 8
    @typoking: I've never seen anyone write loops in C like that. And I certainly never do it myself because I usually make use of the side effects of my loops (using the final value of `i` to tell where/why the loop stopped). – R.. GitHub STOP HELPING ICE Sep 24 '10 at 02:21
  • some C programs are not c99 compatible, my guess is gcc chooses to default to lowest common denominator. you free however to program in C99 standard – Anycorn Sep 24 '10 at 02:23
  • @R.. good point about using `i` after the loop has ended if it is needed. – ubiquibacon Sep 24 '10 at 02:28
  • @aaa carp also a good point about choosing the lowest common denominator, but should I worry about the "errors" (read at link in my question) in the ANSI standard (c89) that caused ISO to make several updated standards? – ubiquibacon Sep 24 '10 at 02:30
  • 2
    @typoking: I'd be more worried about the fact that there are very few compilers that actually support all of C99. For the most part, C99 wasn't about fixing errors in C89; it was about adding functionality to the language. – James McNellis Sep 24 '10 at 02:32
  • @James McNellis yeah, aaa carp was basically saying the same thing about support. I'll just research what the `c89` errors were in an effort to avoid them. If anyone knows a place where all those errors are listed it would be nice to have a link. – ubiquibacon Sep 24 '10 at 02:43
  • [The Rationale for the C Standard](http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf) makes for an interesting read (not cover-to-cover, but as a reference for various things). It usually identifies differences between C89 and C99 pretty clearly. – James McNellis Sep 24 '10 at 02:48
  • 2
    @typoking: if you're talking about the errors corrected in the Technical Corrigenda, then forget about them. In all cases, when people talk about C89 they mean *after* the corrections, not the document that was actually published by ANSI in 1989. `gcc`, and any other up to date compiler, should incorporate the TCs, although there's no harm in checking the docs. – Steve Jessop Sep 24 '10 at 02:49
  • @Steven Jessop yes, I was confused. The `gcc` language standards page talks about errors being corrected with the Technical Corrigenda documents. I misread and thought that errors were found in `c89` AFTER the Technical Corrigenda corrections, and those errors were later corrected in the `c99` standard. The error part is clear to me now (I guess it is like James McNellis said, the changes from `c89` to `c99` were more about adding functionality than correcting errors). – ubiquibacon Sep 24 '10 at 02:56
  • "c99 (which is the newest I assume)" - that's false, there is `c11` – el.pescado - нет войне Jan 04 '14 at 17:18
  • @el.pescado the c11 standard didn't exist when I asked this question :-) – ubiquibacon Jan 06 '14 at 04:35
  • @ubiquibacon your're tight, sorri, I didn't notice that;) – el.pescado - нет войне Jan 06 '14 at 14:24

4 Answers4

22

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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • thanks for your answer, it helped my understanding of the issue for sure, but I have to give the win to Steve Jessop for the break down and clarifying that `c89` being known as `ANSI` is just a "historical accident". – ubiquibacon Sep 24 '10 at 13:09
22

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.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 3
    ahh, that is good to know (about GNU extensions being "dialects" instead of standards). – ubiquibacon Sep 24 '10 at 02:40
  • 1
    Microsoft compiler now supports C99 :) – Étienne Nov 17 '13 at 12:06
  • @Étienne: do you have a source for that? I know that MS has been adding some C99 features, I didn't realise that they had announced complete (or even substantially complete) support. – Steve Jessop Nov 17 '13 at 13:49
  • 1
    @SteveJessop They don't support everything, but quite a lot: http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx – Étienne Nov 17 '13 at 15:48
3

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();
}
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
1

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177