C语言 C89 vs c99 GCC 编译器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2270899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:30:01  来源:igfitidea点击:

C89 vs c99 GCC compiler

cgcccompiler-constructionc99c89

提问by user69514

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?

如果我使用 c89 和 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 

回答by Alok Singhal

  • //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 mainfunction is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exitfunction with the value returned by the mainfunction as its argument; reaching the }that terminates the mainfunction returns a value of 0.

  • //注释不是 C89 的一部分,但在 C99 中是可以的,
  • main()不返回任何值的情况下脱落相当于return 0;在 C99 中,但在 C89 中并非如此。从N1256(pdf), 5.1.2.2.3p1:

    如果main函数的返回类型是与兼容的类型int,则从初始调用到主函数的返回相当于以exit函数返回的值main作为参数调用该函数;到达}终止main函数的 返回值 0。

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

所以你的代码在 C89 中有未定义的行为,在 C99 中有明确定义的行为。

回答by Jerry Coffin

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).

理论上,应该有一个区别。使用“//”来划分注释不是 C89 的一部分,所以如果它正确地执行了 C89 规则,那将产生编译器错误(使用 -ansi -pedantic,它可能会这样做,但我不记得了当然)。

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.

不过,这给出了一般特征的概念:如果程序编译为 C89,它通常也会编译为 C99,并给出完全相同的结果。C99 主要为您购买了一些 C89 中不存在的新功能,因此您可以使用(例如)可变长度数组,这在 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.

您可能必须要求执行迂腐的规则才能看到所有差异——C99 旨在标准化现有实践,并且一些现有实践是 gcc 扩展,其中一些默认启用。

回答by Artem

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

在这个论坛http://www.velocityreviews.com/forums/t287495-p2-iso-c89-and-iso-c99.html我发现了这个:

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.

总结:99 是标准化的,有新的关键字,新的数组内容,复数,库函数等等。更多的编译器是 c89 完整的,因为他们有这么长时间来使它们如此。

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.

A) ANSI X3.159-1989。这是 1989 年的原始 C 标准,日期为 1989 年 12 月,带有基本原理。该语言的主体在第 3 节中描述,“C 库”——stdio、函数等——在第 4 节中。

B) ISO 9899:1990。这是最初的 ISO C 标准。“ANSI”是美国国家标准协会,所以国际人群必须有自己的标准,有自己的、不同的、编号系统。他们只是简单地采用了 ANSI 1989 年的标准,删除了基本原理,并重新编号了这些部分(改为称它们为“条款”)。除了极少数例外,您可以只添加三个,以便大部分语言都在第 - er,“子句” - 6 部分中描述,而在第 7 部分中的“C 库”部分中进行了描述。

C) ISO 9899:1999。这是新奇的“C99”标准,包括可变长度数组、灵活数组成员、“restrict”和“_Bool”等新关键字、“static”关键字的新语义、创建匿名聚合的新语法、新复数类型、数百个新的库函数等等。

新的 ISO 标准立即被 ANSI“重新采用”。我还没有看到任何关于此的官方“ANSI 认可”声明,但鉴于通常的编号系统,我希望这是 ANSI 标准编号 X3.159-1999。(编号系统很明显:一个标准,一旦出来,就会得到一个数字——X. 代表 ANSI,或者只是一个数字——以及一个表示出版年份的后缀。对现有标准的更新重复使用号,与新的一年。)

尽管 X3.159-1989 和 9899:1990 的年份和章节编号不同,但它们实际上是相同的,因此“C89”和“C90”实际上指的是同一种语言。因此,您可以说“C89”或“C90”并表示相同的意思,即使对于那些了解所有微妙之处的人也是如此。

最初的 1990 年 ISO 标准也有几个小的修订:“规范性附录 1”和两个“技术勘误表”(编号;给出技术勘误表 1 和 TC2)。这两个 TC 被认为是针对标准措辞中的小故障的“错误修复”,而 NA1 是实际的“更改”。在实践中,TC 并没有真正影响用户,而 NA1 增加了一系列人们可以使用的功能,因此 NA1 确实更重要。NA1 于 1994 年问世,因此人们可能会将“由 NA1 修改的 ISO 9899:1990”称为“C94”。我也看过它叫“C95”。