简体   繁体   English

使用extern在C编程中进行声明和定义

[英]Declaration and definition in C programming with extern

This may seem simple to one's eye but, this question is itching me in many ways. 在您看来,这似乎很简单,但是,这个问题在很多方面都困扰着我。

  1. my question is about declaration and defenition on variables in c. 我的问题是关于c中变量的声明和定义。

there are actually many explanation in internet regarding this one and there is not just one solution to this issue as many view points are placed in this issue. 互联网上实际上对此有很多解释,并且对此问题不仅有一种解决方案,因为在此问题上有很多观点。 i want to know the clear existance of this issue. 我想知道这个问题的明确存在。

int a;

just take this is this a declaration or definition?, this one when i use printf , it has 0 as value and address as 2335860 . 只是拿这个是一个声明还是定义?,当我使用printf ,这个是0值和地址是2335860 but if this declaration then how come memory is allocated for this. 但是,如果有此声明,那么将为此分配内存。

int a;
int a;

when i do this it says previous declaration of 'a' was here and redeclaration of 'a' with no linkage. 当我这样做时,它说先前的“ a”声明在这里,而对“ a”的重新声明没有链接。

  1. some sources say redeclaration is permitted in c and some say dont what is the truth? 一些消息来源说允许在c中进行重新声明,而另一些消息则说事实不对?

int a; just take this is this a declaration or definition? 只是这是一个声明还是定义?

int a; if written in global scope is a tentative definition. 如果是在全球范围内编写,则为暂定定义。 Which means if no other definitions are available in current compilation unit, treat this as definition or else this is a declaration. 这意味着如果当前编译单元中没有其他定义可用,请将其视为定义,否则这是一个声明。

From 6.9.2 External object definitions in C11 specs: C9.2规范中的6.9.2外部对象定义开始

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. 如果翻译单元包含一个或多个标识符的临时定义,并且翻译单元不包含该标识符的外部定义,则该行为就好像该翻译单元包含该标识符的文件范围声明,且复合类型为转换单元末尾的,初始化器等于0。

 int i4; // tentative definition, external linkage static int i5; // tentative definition, internal linkage 

So you are effectively doing multiple declarations but getting the address and value because of tentative definition rule. 因此,您有效地执行了多个声明,但是由于临时定义规则而获得了地址和值。

some sources say redeclaration is permitted in c and some say dont what is the truth? 一些消息来源说允许在c中进行重新声明,而另一些消息则说事实不对?

Redeclaration is permitted in C . C允许重新声明。 But redefinition is not. 但是重新定义不是。

Related question: What is the difference between a definition and a declaration? 相关问题: 定义和声明之间有什么区别?

there are actually many explanation in internet 互联网上实际上有很多解释

Prefer a good book instead of internet to get the hold of language. 更喜欢一本好书而不是互联网来掌握语言。 You can choose a good book from: The Definitive C Book Guide and List 您可以从以下内容中选择一本好书: 权威C书籍指南和清单

int a is a definition and can be used in place of declaration. int a是一个定义,可以代替声明使用。 A variable can have many declarations but must have only one definition. 一个变量可以有很多声明,但是只能有一个定义。 In case of 的情况下

int a;
int a;  

there are two definition of a in the same scope. 有两种定义a在同一范围内。 Providing linkage to one of them will make your compiler happy 提供与其中之一的链接将使您的编译器满意

int a;
extern int a; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM