C语言 ANSI C 与其他 C 标准

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3783762/
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 06:30:49  来源:igfitidea点击:

ANSI C vs other C standards

cgccansi-c

提问by ubiquibacon

On several compilers I have used (all gccbut various versions) I get a C99 modeerror for things like declaring int iinside the for loop expression instead of before it (if I do not use the std=c99option). After reading hereI understand that the gccoptions -ansi, -std=c89, and -std=iso9899:1990all evaluate to the ANSI C standard, but I don't understand why/if I should pick the c89standard versus a newer standard like c99(which is the newest I assume).

在我使用过的几个编译器(gcc除了各种版本之外的所有版本)上,我收到C99 mode错误消息,例如int i在 for 循环表达式内部而不是在它之前声明(如果我不使用该std=c99选项)。阅读此处我了解gcc选项-ansi-std=c89-std=iso9899:1990所有评估均符合 ANSI C 标准,但我不明白为什么/是否应该选择c89标准而不是较新的标准c99(我认为这是最新的标准)。

Also, I see multiple versions of the isotype standards for the C language, the first of which (from my understanding) is a direct port of the ANSI standard. Is is safe to say that isowill update their standard for C but the original ANSI standard for C will always be the same?

此外,我看到iso了 C 语言类型标准的多个版本,其中第一个(根据我的理解)是 ANSI 标准的直接移植。 可以肯定地说这iso会更新他们的 C 标准,但 C 的原始 ANSI 标准将始终相同吗?

Bonus question:

奖金问题:

I can actually figure this one out myself, I just haven't taken the time to do it yet, so if someone knows off the top of their head then that is great, otherwise no biggie, I'll figure it out later :)

我实际上可以自己解决这个问题,我只是还没有花时间去做,所以如果有人知道他们的头顶,那就太好了,否则没什么大不了的,我稍后会弄清楚:)

I have a fairly new print of the book The C Programming Language (ANSI). My book always shows for loops like this:

我有这本书的相当新的印刷版The C Programming Language (ANSI)。我的书总是显示这样的循环:

int i;

for(i = 0; i < foo; i++)

but many people (most of who have more programming talent in their little finger) write their for loops like this:

但是很多人(大多数人的小指上有更多的编程天赋)像这样编写他们的 for 循环:

(int i = 0; i < foo; i++)

Is it correct to say if I write the loop the first way then ishould be accessible to the entire function, but if I write it the second way then iis only accessible to the for loop REGARDLESSof what standard I compile with? Another way of asking this same question, if I compile with the c89standard will the iof both for loops be accessible to the entire function and if I compile with the c99standard will the iof the first for loop be accessible to the entire function while the iof the second for loop will be accessible by only the for loop?

它是正确的说,如果我写的循环中的第一种方式则i应该可以访问到全功能的,但如果我把它写了第二种方式则i是唯一能够在for循环REGARDLESS什么标准我编译的?提出同样问题的另一种方式,如果我用c89标准编译,i整个函数是否可以访问两个 for 循环的 ,如果我用c99标准编译,i整个函数可以访问第一个 for 循环i的第二个 for 循环只能由 for 循环访问?

采纳答案by Steve Jessop

I don't understand why/if I should pick the c89 standard versus a newer standard like c99 (which is the newest I assume).

我不明白为什么/如果我应该选择 c89 标准而不是像 c99 这样的较新标准(这是我假设的最新标准)。

A couple of reasons:

几个原因:

1) gcc's C99 support is not quite complete, whereas its C89 support is. That's why C89 (with GNU extensions) is the default. So if you're an absolute stickler for programming to a standard using gcc, pick C89.

1) gcc 的 C99 支持不是很完整,而它的 C89 支持是完整的。这就是为什么 C89(带有 GNU 扩展)是默认的。因此,如果您是使用 gcc 按照标准编程的绝对坚持者,请选择 C89。

2) Microsoft's compiler doesn't really support C99 at all. So if you want to write portable C code, C89 is a common standard.

2)微软的编译器根本不支持C99。所以如果你想编写可移植的 C 代码,C89 是一个通用标准。

Is is safe to say that iso will update their standard for C but the original ANSI standard for C will always be the same?

可以肯定地说,iso 会更新他们的 C 标准,但 C 的原始 ANSI 标准将始终相同吗?

No, ISO C99 was also ratified as an ANSI standard. The name "ansi" being attached to C89 only is an unfortunate historical accident. That said, C89 will always be C89, it's just not the most recent ANSI C standard.

不,ISO C99 也被批准为 ANSI 标准。C89 的名字“ansi”只是一个不幸的历史意外。也就是说,C89 将永远是 C89,它只是不是最新的 ANSI C 标准。

Is it correct to say if I write the loop the first way then i should be accessible to the entire function, but if I write it the second way then i is only accessible to the for loop REGARDLESS of what standard I compile with?

如果我用第一种方式编写循环,那么我应该可以访问整个函数,但是如果我用第二种方式编写它,那么我只能访问 for 循环,而不管我编译的标准是什么?

You can't write it the second way in C89 (i.e. with -pedanticto adhere to the standard), so there is no "regardless of what standard". The versions of C with GNU extensions aren't standards, they're "dialects" (at least that's what the man page calls them). In C89 the second loop isn't legal, in C99 the second one confines the scope of ito the loop. Obviously in both cases, the first loop gives ia wider scope.

你不能在 C89 中用第二种方式写它(即-pedantic遵守标准),所以没有“不管什么标准”。带有 GNU 扩展的 C 版本不是标准,它们是“方言”(至少手册页是这样称呼它们的)。在 C89 中,第二个循环是不合法的,在 C99 中,第二个循环将范围限制i在循环中。显然,在这两种情况下,第一个循环提供i了更广泛的范围。

In fact, gcc doesn't like the second loop in C89 even with GNU extensions enabled.

事实上,即使启用了 GNU 扩展,gcc 也不喜欢 C89 中的第二个循环。

回答by James McNellis

C was standardized as C89 by ANSI. It was then standardized by ISO as C90; there are no technical differences between C89 and C90.

C 被 ANSI 标准化为 C89。然后被 ISO 标准化为 C90;C89 和 C90 之间没有技术差异。

C99 is a revision of the C standard; it was developed by the ISO C committee and standardized by both ISO and ANSI. Both C89 and C99 are ANSI standards. In common parlance, the phrase "ANSI C" usually refers to C89; the K&R 2nd ed. book covers only C89, not C99.

C99是C标准的修订版;它由 ISO C 委员会开发并由 ISO 和 ANSI 标准化。C89 和 C99 都是 ANSI 标准。通俗地说,短语“ANSI C”通常指的是 C89;K&R 第 2 版。本书仅涵盖 C89,而不涵盖 C99。

Why would you choose to use an old version of C? Well, there are at least two reasons. First, you may have a compiler that doesn't support C99 (for example, Microsoft Visual C++ only supports C89). Second, there's a lot of legacy code out there that uses things from C89 that are not allowed in C99 (you can see more at the question "C99 backward compatibility"; that also links to the C99 rationale document that describes the differences).

为什么你会选择使用旧版本的 C?嗯,至少有两个原因。首先,您的编译器可能不支持 C99(例如,Microsoft Visual C++ 仅支持 C89)。其次,有很多遗留代码使用了 C99 中不允许的 C89 中的内容(您可以在问题“C99 向后兼容性”中看到更多信息;该问题还链接到描述差异的 C99 基本原理文档)。

As for the "bonus question," you can't declare a variable in a for-statement in C89; you can in C99. In C89, the first part of a for-statement is an expression.

至于“奖金问题”,您不能在 C89 的 for 语句中声明变量;你可以在C99。在 C89 中,for 语句的第一部分是表达式。

回答by Brock Woolf

I don't have an indepth explaination for the C standards.

我没有对 C 标准的深入解释。

But my default stance has been to use C99 when given the chance. The fact that it was the latest developed standard is one reason (I have a misguided sense that "newer is better").

但我的默认立场是有机会时使用 C99。它是最新开发的标准这一事实是一个原因(我有一种“越新越好”的误导性感觉)。

The main reason is so I can declare variables in for loops.

主要原因是我可以在 for 循环中声明变量。

C99 valid:

C99 有效:

for (int i = 0; i < 100; i++)
{
   doSomething();
}

C89, ANSI and older:

C89、ANSI 及更早版本:

int i;
for (i = 0; i < 100; i++)
{
   doSomething();
}

回答by Jens Gustedt

The C99 implementation of gccis not yet completed, but fairly usably in everyday programmers life. I don't have the reference at hand, but for gcc there is a statement somewhere that they will switch to C99 (or merely to their dialect gnu99) the day that this implementation is considered to be terminated.

gccC99 实现尚未完成,但在日常程序员生活中相当有用。我手头没有参考资料,但对于 gcc,某处有一条声明,他们将在该实现被视为终止的那一天切换到 C99(或仅切换到他们的方言 gnu99)。

The question of using C99 features or not, is divided in a pragmatic part:

是否使用C99特性的问题,分为实用部分:

  • for which platforms I do have to compile my code
  • what features would I gain (for my taste a lot of cleanness and ease to program portable)
  • 我必须为哪些平台编译我的代码
  • 我会获得哪些功能(按照我的口味,很多清洁和易于编程的便携性)

and an emotional / ideological part. For the later, there is no cure but the application of the dinosaur principle.

和情感/意识形态部分。对于后者,除了应用恐龙原理外,别无他法。