C89,混合变量声明和code [英] C89, Mixing Variable Declarations and Code

查看:181
本文介绍了C89,混合变量声明和code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,想知道到底为什么C89编译器将你倾倒,当你尝试混合变量声明和code,像这样的例子:

I'm very curious to know why exactly C89 compilers will dump on you when you try to mix variable declarations and code, like this for example:

rutski@imac:~$ cat test.c
#include <stdio.h>

int
main(void)
{
    printf("Hello World!\n");
    int x = 7;
    printf("%d!\n", x);
    return 0;
}
rutski@imac:~$ gcc -std=c89 -pedantic test.c
test.c: In function ‘main’:
test.c:7: warning: ISO C90 forbids mixed declarations and code
rutski@imac:~$ 

是的,你可以住在离-pedantic避免这样的事情。但是,那么你的code是不再符合标准。而作为能够回答可能这个职位已经是任何人知道,这不仅仅是一个理论问题。像微软的C编译器的平台执行这一快速标准下的任何和所有的情况。

Yes, you can avoid this sort of thing by staying away from -pedantic. But then your code is no longer standards compliant. And as anybody capable of answering this post probably already knows, this is not just a theoretical concern. Platforms like Microsoft's C compiler enforce this quick in the standard under any and all circumstances.

定的C如何古代是,我会想象这个特性是由于一些历史遗留问题可以追溯到上世纪70年代的非凡硬件限制,但我不知道的细节。还是我完全错了吗?

Given how ancient C is, I would imagine that this feature is due to some historical issue dating back to the extraordinary hardware limitations of the 70's, but I don't know the details. Or am I totally wrong there?

推荐答案

C标准说你不可,因为它没有在其中C89标准规范了早期的C编译器允许的。它是一个基团足够步骤来创建可用于编写操作系统及其实用程序的语言。或许根本没有考虑这个概念 - 在当时没有其他语言不允许它(帕斯卡,大陵,PL / 1,FORTRAN,COBOL),所以C没有必要无论是。它可能使编译器稍硬处理(大),和原来的编译器是由昆明植物64 code和64昆明植物研究所数据空间允许的的 PDP 11 系列(大机器;对那些利特勒只允许64昆明植物研究所为code和数据,据我所知)。因此,额外的复杂性是不是一个好主意。

The C standard said "thou shalt not", because it was not allowed in the earlier C compilers which the C89 standard standardized. It was a radical enough step to create a language that could be used for writing an operating system and its utilities. The concept probably simply wasn't considered - no other language at the time allowed it (Pascal, Algol, PL/1, Fortran, COBOL), so C didn't need to either. And it probably makes the compiler slightly harder to handle (bigger), and the original compilers were space-constrained by the 64 KiB code and 64 KiB data space allowed in the PDP 11 series (the big machines; the littler ones only allowed 64 KiB for both code and data, AFAIK). So, extra complexity was not a good idea.

这是C ++,允许声明和变量进行交织,但C ++不是,也从来,C.然而,C99终于用C赶上++(这是一个非常有用的功能)。可悲的是,虽然微软从未付诸实施C99。

It was C++ that allowed declarations and variables to be interleaved, but C++ is not, and never has been, C. However, C99 finally caught up with C++ (it is a useful feature). Sadly, though, Microsoft never implemented C99.

这篇关于C89,混合变量声明和code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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