2010-02-16 18 views
8

¿Hay alguna diferencia si puedo compilar el programa siguiente utilizando c89 c99 vs? Obtengo el mismo resultado. ¿Hay realmente una diferencia entre los dos?C89 C99 vs compilador GCC

#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 
+5

"gcc -std = c89" y "gcc -std = c99" no se ajustan plenamente a las normas respectivas. Agregue "-pedantic" o "-pedantic-errors" para obtener algo que se aproxime a la conformidad total. –

Respuesta

6

En teoría, debería haber una diferencia. El uso de "//" para demarcar un comentario no es parte de C89, por lo que si se hace cumplir las reglas C89 correctamente, que produciría un error de compilación (con -ansi -pedantic, podría hacer eso, pero no me acuerdo de Por supuesto).

Eso da una idea del carácter general, sin embargo: si un programa se compila como C89, que va, también es posible compilar como C99, y dan exactamente los mismos resultados. C99 generalmente le compra algunas características nuevas que no están presentes en C89, por lo que puede usar (por ejemplo) matrices de longitud variable, que no están permitidas en C89.

Puede que tenga que pedir la aplicación de reglas pedantes para ver todas las diferencias - C99 está destinado a estandarizar las prácticas existentes, y algunas de las prácticas existentes son las extensiones gcc, algunas de las cuales están habilitadas por defecto.

+2

+1, Buena captura; '//' los comentarios pueden ser la única parte de C99 que vale la pena tomar. '-ansi -pedantic' viene con un error:' main.c: 5: Error: la expresión esperada antes '/' token' –

+1

no veo cómo '' // comentarios son más útiles, es sólo dos menos caracteres ... es como decir que C debería usar '.' para acceder a miembros de punteros a estructuras en lugar de' -> 'porque ahorra tipeo. –

+0

@JoeD '' // comentarios sólo captan una línea, lo que significa que son mucho menos propensos a ser capturado de forma inadvertida por otro conjunto de observaciones o causar problemas. Además, tu argumento sobre '.' es un poco tonto; usar el mismo operador para lo que es fundamentalmente la misma operación es probablemente una buena idea. – Alice

1

en este foro http://www.velocityreviews.com/forums/t287495-p2-iso-c89-and-iso-c99.html encontré esto:

resumen: 99 es estándar, cuenta con nuevas palabras clave, materia nueva serie, números complejos, funciones de biblioteca y tal. Se completan más compiladores, ya que han tenido todo este tiempo para que así sea.

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.

28
  • // comentarios no son una parte del C89, pero están bien en C99,
  • caerse de main() sin devolver ningún valor es equivalente a return 0; en C99, pero no así en C89. De 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.

comportamiento tanto, su código ha indefinido en C89, y el comportamiento bien definido en C99.

Cuestiones relacionadas