ANSI C VS其他C类标准 [英] ANSI C vs other C standards

查看:199
本文介绍了ANSI C VS其他C类标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用(均 GCC ,但各种版本),我得到的东西 C99模式错误多个编译类似于声明 INT I 里面的for循环前pression,而不是之前(如果我​​不使用性病= C99 选项)。在这里阅读据我所知, GCC 选项 -ansi -std = C89 -std = iso9899:1990 所有评估的ANSI C标准的,但我不明白为什么/如果我要挑 C89 与标准像 C99 一个新的标准(这是我最新的假设)。

另外,我看到了异的多个版本键入C语言标准,这(从我的理解)第一个是ANSI标准的直接端口。 是有把握地说, ISO 将更新其对C标准,但对于C原来的ANSI标准永远是一样的吗?

奖金的问题:

我其实可以算一出来我自己,我只是还没有花时间去做它,所以如果有人知道了他们的头顶,那么这是伟大的,否则根本不算什么,我就看着办吧后来:)

我有这本书的一个相当新的打印 C程序设计语言(ANSI)。我的书总是给出了这样的循环:

  INT I;对于(i = 0; I<富;我++)

但很多人(多数谁在他们的小指更多的编程天赋)写自己对这样的循环:

 (INT I = 0; I<富;我++)

是不是正确的说,如果我写的循环中的第一种方式则 I 应该是全功能访问,但如果我把它写了第二种方式则 I 仅在for循环的无论何种什么标准我编译访问?问同样的问题,如果我用 C89 标准编制将 I 两者的for循环是可访问的另一种方法对整个函数,如果我与 C99 标准编制将 I 第一循环是可访问全功能,而 I 第二循环的将是可访问仅在for循环?


解决方案

  

我不明白为什么/如果我应该
  挑C89标准相对较新
  标准像C99(这是最新
  我假设)。


有两个原因:

1)GCC的C99支持并不相当齐全,而其C89支持。这就是为什么C89(GNU扩展)是默认的。所以,如果你使用gcc是编程的绝对坚持己见的一个标准,挑选C89。

2)微软的编译器真的不支持C99的。因此,如果你想写可以移植的C code,C89是一个通用的标准。


  

时是安全的说,ISO将更新
  他们为C,但原标准
  对于C ANSI标准永远是
  一样的吗?


没有,ISO C99也被批准为ANSI标准。命名为ANSI被连接到C89仅是一个不幸的历史事件。这就是说,C89永远是C89,它只是没有最新的ANSI C标准。


  

是不是正确的说,如果我写的
  循环中的第一种方式那么我应该
  访问整个功能,但
  如果我把它写了第二种方式则是我
  唯一可访问的for循环
  不管我编译什么标准
  用?


您可以不写它C89(即具有 -pedantic 坚持标准)的第二种方式,因此不存在不管是什么标准。的C与GNU扩展的版本不标准,他们是方言(至少这就是男人页面调用它们)。在C89的第二个循环是不合法的,在C99中第二个框框 I 来循环的范围。很显然,在这两种情况下,第一个循环给 I 范围较广。

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

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?

解决方案

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.

这篇关于ANSI C VS其他C类标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆