2010-02-16 248 views
8

如果我使用c89和c99編譯以下程序,是否有區別?我得到相同的輸出。兩者之間真的有區別嗎?C89 vs c99 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」和「gcc -std = c99」並不完全符合各自的標準。添加「pedantic」或「pedantic錯誤」以使某些內容接近完全一致。 –

回答

6

理論上應該有一個區別。使用「//」來標註註釋並不是C89的一部分,因此如果它正確執行了C89規則,那會產生編譯器錯誤(使用-ansi -pedantic,可能會這樣做,但我不記得當然)。

這給出了一般字符的概念:如果程序編譯爲C89,它通常也會編譯爲C99,並給出完全相同的結果。 C99主要爲您購買一些C89中不存在的新功能,因此您可以使用(例如)可變長度數組,這在C89中是不允許的。

您可能不得不要求強制執行規則以查看所有差異 - C99旨在標準化現有的實踐,並且一些現有做法是gcc擴展,其中一些默認啓用。

+2

+1,良好的捕獲; ''//評論可能是C99值得考慮的唯一部分。 '-ansi -pedantic'確實出現了一個錯誤:'main.c:5:錯誤:在'/'token之前的預期表達式 –

+1

我不明白'//'註釋如何更有用,它只有兩個更少的字符......這就像說C應該使用'.'來訪問結構指針的成員而不是' - >',因爲它可以節省輸入。 –

+0

@JoeD'''註釋只會捕捉一條線,這意味着它們不太可能被另一組註釋或其他問題無意中捕獲。此外,你關於'。.'的論點有點傻,使用相同的操作員進行基本相同的操作可能是一個好主意。 – Alice

1

在這個論壇http://www.velocityreviews.com/forums/t287495-p2-iso-c89-and-iso-c99.html我發現這一點:

摘要:99是規範的,具有新的關鍵字,新數組的東西,複數,庫函數和這樣。更多的編譯器是完整的,因爲他們已經完成了這些工作。

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
  • //意見是不是C89的一部分,但在C99都OK,
  • 脫落的main()不返回任何值相當於C99 return 0;,但不是那麼C89。從N1256(PDF),5.1.2.2.3p1:在C89

    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.

所以,你的代碼是未定義行爲,並在C99良好定義的行爲。