2010-02-16 16 views
8

C'è una differenza se compilo il seguente programma usando c89 vs c99? Ottengo lo stesso risultato. C'è davvero una differenza tra i due?C89 vs compiler GCC c99

#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" e "gcc -std = c99" non sono completamente conformi ai rispettivi standard. Aggiungi "-pedantic" o "-pedantic-errors" per ottenere qualcosa che si avvicini alla piena conformità. –

risposta

6

In teoria, ci dovrebbe essere una differenza. Usare "//" per demarkare un commento non fa parte di C89, quindi se applicasse correttamente le regole C89, ciò produrrebbe un errore del compilatore (con -ansi -pedantic, potrebbe farlo, ma non ricordo per sicuro).

Ciò dà comunque un'idea del carattere generale: se un programma viene compilato come C89, verrà generalmente compilato anche come C99 e darà esattamente gli stessi risultati. C99 acquista principalmente nuove funzionalità che non sono presenti in C89, quindi puoi usare (per esempio) array di lunghezza variabile, che non sono consentiti in C89.

Potrebbe essere necessario chiedere l'applicazione pedante delle regole per vedere tutte le differenze però - C99 ha lo scopo di standardizzare la pratica esistente, e alcune delle pratiche esistenti sono le estensioni gcc, alcune delle quali sono abilitate di default.

+2

+1, buona presa; '//' I commenti potrebbero essere l'unica parte del C99 che vale la pena di prendere. '-ansi -pedantic' ha un errore:' main.c: 5: errore: espressione attesa prima di '/' token' –

+1

Non vedo come '//' i commenti siano più utili, sono solo due meno caratteri ... è come dire che C dovrebbe usare '.' per accedere ai membri dei puntatori alle strutture piuttosto che' -> 'perché salva la digitazione. –

+0

@JoeD '//' I commenti acquisiscono solo una riga, il che significa che è molto meno probabile che vengano catturati inavvertitamente da un altro gruppo di commenti o che causino problemi. Inoltre, la tua argomentazione su "." È una specie di sciocco; utilizzare lo stesso operatore per ciò che è fondamentalmente la stessa operazione è probabilmente una buona idea. – Alice

1

su questo forum http://www.velocityreviews.com/forums/t287495-p2-iso-c89-and-iso-c99.html ho trovato questo:

sintesi: 99 è standardizzato, ha nuove parole chiave, nuove cose serie, numeri complessi, funzioni di libreria e così via. Più compilatori sono completi da quando hanno avuto tutto questo tempo per renderli tali.

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
  • // commenti non sono una parte di C89, ma sono OK in C99,
  • caduta di main() senza restituire alcun valore è equivalente a return 0; in C99, ma non così in C89. Da 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.

comportamento Così il vostro codice è indefinito in C89, e il comportamento ben definito in C99.

Problemi correlati