简体   繁体   English

澄清C中的外部值

[英]Clarification about extern value in C

In C an extern variable gets the initial value of "zero". 在C中,外部变量的初始值为“零”。 however after reading about this particular class category.It says that declearing variable as an extern, means the value only passed the "decleration stage".How come a variable can get an initial value without procceding to the initialization stage and allocating a memory for the particular variable? 但是,在阅读了有关该特定类的类别之后。它说,将变量清除为外部变量,意味着该值仅通过了“ decleration stage”。为什么变量可以在不进行初始化阶段的情况下获得初始值,也不为该变量分配内存。特定变量? Please consider the following code: 请考虑以下代码:

extern int var; 
int main(void) 
{ 
var = 10; 
return 0; 
}

I understand why a problemetic sitution arises: we tried to initialize a value to a variable which is not allocated in the memory(the variable doesn't really exist). 我知道为什么会出现问题:我们尝试将值初始化为未在内存中分配的变量(该变量实际上不存在)。 My point is: why it's memory doesn't exist and followoing "extern's" terminology the variable has an initial value of "zero". 我的观点是:为什么它的内存不存在,并且遵循“外部”的术语,变量的初始值为“零”。 In my mind it means that the memory does actually exist if it has an itial value.... 在我看来,这意味着如果内存具有itial值,则它确实存在。

For this code to be valid, another part of the program must contain the definition of var , eg: 为了使此代码有效,程序的另一部分必须包含var的定义,例如:

int var = 0;

which is where the initial value comes from. 初始值来自哪里。 If you don't provide an initializer then it behaves as if you initialized with = {0} (that is the rule for static storage duration variables). 如果不提供初始化程序,则它的行为就像使用= {0}初始化(这是静态存储持续时间变量的规则)。

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

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