C89 与 c99 GCC 编译器

发布于 2024-08-21 15:24:33 字数 285 浏览 6 评论 0原文

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

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌化 2024-08-28 15:24:33
  • // 注释不是 C89 的一部分,但在 C99 中可以,
  • main() 脱离而不返回任何值相当于 return 0; code> 在 C99 中,但在 C89 中则不然。来自 N1256 (pdf),5.1.2.2 .3p1:

    <块引用>

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

因此,您的代码在 C89 中具有未定义的行为,而在 C99 中具有明确定义的行为。

  • // 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.

最美的太阳 2024-08-28 15:24:33

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

不过,这给出了一般特征的想法:如果一个程序编译为 C89,它通常也会编译为 C99,并给出完全相同的结果。 C99 主要为您提供了一些 C89 中不存在的新功能,因此您可以使用(例如)可变长度数组,这在 C89 中是不允许的。

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

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.

混浊又暗下来 2024-08-28 15:24:33

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

摘要:99 是标准化的,有新的关键字、新的数组内容、复数、库函数等等。更多的编译器已经完成了 c89,因为他们有足够的时间来完成它们。

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

B) ISO 9899:1990。这是最初的 ISO C 标准。 “ANSI”是
美国国家标准协会,因此国际人群
有自己的标准和自己不同的编号
系统。他们只是采用了 ANSI 1989 标准,删除了
基本原理,并对各部分重新编号(称为“条款”
反而)。除了极少数例外,您可以只添加三个,这样
大部分语言在章节中描述
-- 呃,“子句” -- 6,以及第 7 节中的“C 库”部分。

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

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

虽然X3.159-1989和9899:1990有不同的年份和部分
编号,它们实际上是相同的,所以“C89”和“C90”实际上
指同一种语言。因此你可以说“C89”或“C90”
即使对于那些了解所有微妙之处的人来说,也意味着同样的事情。

对最初的 1990 ISO 也进行了一些小的修改
标准:《规范性附录1》和两份《技术勘误表》
(编号;提供技术勘误 1 和 TC2)。这两个 TC 分别是
被认为是针对措辞中的小故障的“错误修复”
标准,而NA1是一个实际的“改变”。实际上,TC 并不
确实影响了用户,而 NA1 添加了一系列功能
人们都可以使用,所以NA1确实更有意义。 NA1出来了
1994 年,因此人们可以将“ISO 9899:1990 经 NA1 修改”称为
“C94”。我也见过它叫“C95”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文