ANSI C 与其他 C 标准

发布于 2024-09-24 13:48:04 字数 1158 浏览 10 评论 0原文

在我使用过的几个编译器(所有 gcc,但不同版本)上,我收到一个 C99 模式 错误,例如在 for 循环内声明 int i表达式而不是之前的表达式(如果我不使用 std=c99 选项)。阅读此处后,我了解到gcc 选项 -ansi-std=c89-std=iso9899:1990 全部计算为 ANSI C 标准,< strong>但我不明白为什么/是否应该选择 c89 标准而不是像 c99 这样的较新标准(我认为这是最新的)。

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

额外问题:

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

我有一本相当新的《C 编程语言 (ANSI)》一书。我的书总是显示这样的 for 循环:

int i;

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

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

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

如果我用第一种方式编写循环,那么我这样说是否正确整个函数应该可以访问,但是如果我用第二种方式编写它,那么 i 只能由我编译的标准的 for 循环访问?询问同一问题的另一种方式是,如果我使用 c89 标准进行编译,整个函数都可以访问两个 for 循环的 i ,并且如果我使用 c89 标准进行编译>c99 标准将第一个 for 循环的 i 可供整个函数访问,而第二个 for 循环的 i 将仅由 for 访问环形?

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

Also, I see multiple versions of the iso type 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 iso will update their standard for C but the original ANSI standard for C will always be the same?

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:

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:

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

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? Another way of asking this same question, if I compile with the c89 standard will the i of both for loops be accessible to the entire function and if I compile with the c99 standard will the i of the first for loop be accessible to the entire function while the i of the second for loop will be accessible by only the for loop?

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

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

发布评论

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

评论(4

最美的太阳 2024-10-01 13:48:04

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

有几个原因:

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

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

可以肯定地说 ISO 将会更新
他们的 C 标准,但原来的
C 的 ANSI 标准将永远是
一样吗?

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

如果我写的话,这样说是否正确
以第一种方式循环然后我应该是
整个函数都可以访问,但是
如果我用第二种方式写,那么我就是
只能通过 for 循环访问
不管我编译什么标准
与?

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

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

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

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.

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.

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?

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.

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?

You can't write it the second way in C89 (i.e. with -pedantic to 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 i to the loop. Obviously in both cases, the first loop gives i a wider scope.

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

七颜 2024-10-01 13:48:04

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

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

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

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

C was standardized as C89 by ANSI. It was then standardized by ISO as C90; there are no technical differences between C89 and 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.

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

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.

許願樹丅啲祈禱 2024-10-01 13:48:04

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

但我的默认立场是只要有机会就使用 C99。事实上,它是最新开发的标准是一个原因(我有一种错误的感觉,即“更新更好”)。

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

C99 有效:

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

C89、ANSI 及更早版本:

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

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

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

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

C99 valid:

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

C89, ANSI and older:

int i;
for (i = 0; i < 100; i++)
{
   doSomething();
}
心意如水 2024-10-01 13:48:04

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

是否使用 C99 功能的问题分为一个实用部分:

  • 对于哪些平台我必须使用
    编译我的代码
  • 我会获得什么功能(根据我的口味,很多干净性和易于编程便携式)

以及情感/意识形态部分。对于后者,除了应用恐龙原理之外,没有其他治疗方法。

The C99 implementation of gcc is 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.

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

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

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