C中的暂定定义背后的基本原理是什么? [英] What is the rationale behind tentative definitions in C?

查看:111
本文介绍了C中的暂定定义背后的基本原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下程序.这会带来任何编译错误吗?

Consider following program. Will this give any compilation errors?

#include <stdio.h>
int s=5;
int s;
int main(void)
{
     printf("%d",s);
}

乍一看,似乎编译器会给出变量重新定义错误,但根据C标准,程序是完全有效的. (请参见 http://ideone.com/Xyo5SY 上的实时演示).

At first glance it seems that compiler will give variable redefinition error but program is perfectly valid according to C standard. (See live demo here http://ideone.com/Xyo5SY).

暂定定义是没有存储类说明符和初始化程序的任何外部数据声明.

A tentative definition is any external data declaration that has no storage class specifier and no initializer.

C99 6.9.2/2

C99 6.9.2/2

声明对象的标识符,该对象的文件范围不包含 初始化程序,没有存储类说明符或带有 存储类规范静态,构成一个暂定的定义.如果一个 翻译单元包含一个或多个暂定定义 标识符,翻译单元不包含用于 那个标识符,那么行为就好像翻译 单元包含该标识符的文件范围声明,并带有 截至翻译单元末尾的复合类型,带有 初始值设定项等于0.

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

我的问题是,允许暂定定义的理由是什么?在C中有什么用吗?为什么C允许暂定定义?

My question is, what is rationale for allowing tentative definitions? Is there any use of this in C? Why does C allow tentative definitions?

推荐答案

创建临时定义是一种桥接C89之前存在的不兼容模型的方法. C99原理部分6.9.2外部对象定义如下:

Tentative definitions was created as a way to bridge incompatible models that existed pre-C89. This is covered in the C99 rationale section 6.9.2 External object definitions which says:

在C90之前,实施方面的差异很大 使用内部链接引用标识符(请参见第6.2.2节). C89 委员会发明了暂定定义的概念来解决这个问题 情况.临时定义是可能会或可能不会的声明 充当定义:如果稍后在 翻译单元,那么暂定定义仅充当 宣言.如果不是,则暂定定义将作为实际定义 定义.为了一致起见,相同的规则适用于 具有外部链接的标识符,尽管它们并不严格

Prior to C90, implementations varied widely with regard to forward referencing identifiers with internal linkage (see §6.2.2). The C89 committee invented the concept of tentative definition to handle this situation. A tentative definition is a declaration that may or may not act as a definition: If an actual definition is found later in the translation unit, then the tentative definition just acts as a declaration. If not, then the tentative definition acts as an actual definition. For the sake of consistency, the same rules apply to identifiers with external linkage, although they're not strictly necessary.

和C99基本原理的6.2.2部分说:

and section 6.2.2 from the C99 rationale says:

用于具有外部链接的对象的定义模型是 一个重大的C89标准化问题.基本的问题是要决定 对象的哪些声明定义了对象的存储,以及 仅引用现有对象.一个相关的问题是 是否允许多个存储定义,或者仅是一个 可以接受的. C89之前的实现方式至少表现出四种不同 型号,按限制程度递增的顺序列在此处:

The definition model to be used for objects with external linkage was a major C89 standardization issue. The basic problem was to decide which declarations of an object define storage for the object, and which merely reference an existing object. A related problem was whether multiple definitions of storage are allowed, or only one is acceptable. Pre-C89 implementations exhibit at least four different models, listed here in order of increasing restrictiveness:

这篇关于C中的暂定定义背后的基本原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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