ANSI C vs other C standards

余生长醉 提交于 2019-11-29 22:57:28

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.

James McNellis

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!