C89 vs c99 GCC compiler

2019-02-09 07:02发布

问题:

Is there a difference if I compile the following program using c89 vs c99? I get the same output. Is there really a difference between the two?

#include <stdio.h>

    int main ()
    {
      // Print string to screen.
      printf ("Hello World\n");
    }

gcc -o helloworld -std=c99 helloworld.c 
vs
gcc -o helloworld -std=c89 helloworld.c 

回答1:

  • // comments are not a part of C89 but are OK in C99,
  • falling off of main() without returning any value is equivalent to return 0; in C99, but not so in C89. From N1256 (pdf), 5.1.2.2.3p1:

    If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

So your code has undefined behavior in C89, and well-defined behavior in C99.



回答2:

In theory, there should be one difference. Using "//" to demark a comment isn't part of C89, so if it enforced the C89 rules correctly, that would produce a compiler error (with -ansi -pedantic, it might do that, but I don't remember for sure).

That gives an idea of the general character though: if a program compiles as C89, it'll generally also compile as C99, and give exactly the same results. C99 mostly buys you some new features that aren't present in C89, so you can use (for example) variable length arrays, which aren't allowed in C89.

You may have to ask for pedantic rules enforcement to see all the differences though -- C99 is intended to standardize existing practice, and some of the existing practice is gcc extensions, some of which are enabled by default.



回答3:

on this forum http://www.velocityreviews.com/forums/t287495-p2-iso-c89-and-iso-c99.html i found this:

summary: 99 is standardized, has new keywords, new array stuff, complex numbers, library functions and such. More compilers are c89 complete since they've had all this time to make them so.

A) ANSI X3.159-1989. This is the original 1989 C standard, dated December 1989, with Rationale. The main body of the language is described in section 3, and the "C library" -- stdio, functions, and so on -- in section 4.

B) ISO 9899:1990. This is the original ISO C standard. "ANSI" is the American National Standards Institute, so the international crowd have to have their own standards with their own, different, numbering system. They simply adopted ANSI's 1989 standard, removed the Rationale, and renumbered the sections (calling them "clauses" instead). With very few exceptions you can just add three, so that most of the language is described in section -- er, "clause" -- 6, and the "C library" part in section 7.

C) ISO 9899:1999. This is the newfangled "C99" standard, with its Variable Length Arrays, Flexible Array Members, new keywords like "restrict" and "_Bool", new semantics for the "static" keyword, new syntax to create anonymous aggregates, new complex-number types, hundreds of new library functions, and so on.

The new ISO standard was immediately "back-adopted" by ANSI. I have not seen any official "ANSI-sanctioned" claim about this, but given the usual numbering systems, I would expect this to be ANSI Standard number X3.159-1999. (The numbering system is pretty obvious: a standard, once it comes out, gets a number -- X. for ANSI, or just a number for ISO -- and a suffix indicating year of publication. An update to an existing standard reuses the number, with the new year.)

Although X3.159-1989 and 9899:1990 have different years and section numbering, they are effectively identical, so "C89" and "C90" really refer to the same language. Hence you can say either "C89" or "C90" and mean the same thing, even to those aware of all the subtleties.

There were also several small revisions to the original 1990 ISO standard: "Normative Addendum 1", and two "Technical Corrigenda" (numbered; giving Technical Corrigendum 1 and TC2). The two TCs are considered to be "bug fixes" for glitches in the wording of the standard, while NA1 is an actual "change". In practice, the TCs do not really affect users, while NA1 adds a whole slew of functions that people can use, so NA1 really is more significant. NA1 came out in 1994, so one might refer to "ISO 9899:1990 as modified by NA1" as "C94". I have seen it called "C95", too.