怀疑在c中使用extern关键字...任何一个帮助 [英] doubt in usage of extern keyword in c...any one help

查看:106
本文介绍了怀疑在c中使用extern关键字...任何一个帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我对extern关键字的理解,它将仅用于声明并且不会为该变量分配内存,除了在初始化时将分配内存。

so,

1)

upto me what i understood about extern keyword is , it will just used for declaration and wont allocate memory for that variable with exception that memory will be allocated when it initialized.
so,
1)

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




output : 10





2)

现在,



2)
now,

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



但是当我在主要给extern时我得到错误,说extern变量无法初始化。那么为什么当我在外面宣布它时它不显示错误。我用谷歌搜索但我没有明确的想法。

任何人都可以说我的理由吗?


but when i give extern with in main i am getting error saying extern variable cannot be initialized. then why it does not shows error when i declare it outside. i googled but i didnot get clear idea.
can any one say me the reason ?

推荐答案

extern 表示变量声明在另一个文件中可用,并且在编译文件链接在一起之前,实际位置将不会被知道。

你不能使用它在函数声明中,因为它总是引用一个全局变量而不是本地变量。



见这里: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/ [ ^ ]
extern says that the variable is declared to be available in a different file, and that the actual location will not be known until the compiled files are linked together.
You can't use it inside a function declaration because it always refers to a global variable rather than a local one.

See here: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/[^]


了解背后的原因对你来说似乎很奇怪的编译器行为,你必须更好地学习变量范围(可以看看这里 [ ^ ]和此处 [ ^ ]。

C程序中的变量有3个主要范围:全局,本地或参数

范围是实际上位置,严格限定,变量可以存在和使用。

全局范围意味着变量可以在程序中的任何地方访问,从内部和外部函数,以及程序中包含的任何程序模块(文件)。

但是作用域的另一个非常重要的属性是它允许对具有相同名称但不同的变量进行屏蔽范围。是的,您可以在同一段代码中,在同一个文件中,甚至在同一个函数中有更多具有相同名称的变量,但所有变量必须具有不同的范围,当然每个范围< b>必须是可以很好识别的或编译器会抛出错误。

请考虑以下代码:

To understand the reasons behind the compiler behavior that seems so strange to you, you have to learn better about variable scopes (can have a look here[^] and here[^]).
The variables in a C program have 3 main scopes: Global, Local or parameter.
The scope is in effect the location, strictly circumscribable, where a variable can exist and be used.
A global scope means that the variable can be accessed anywhere in your program, from inside and outside functions, and from any program module (file) that is included in your program.
But another very important property of the scope is that it allows the 'masking' of variables having same name, but different scope. Yes you can have more variables having the same name in the same piece of code, in the same file and even in the same function, but all of them must have different scopes, and of course each scope must be well identifiable or the compiler will throw errors.
Consider following code:
int var = 10;    //Global scope

void foo(void)
{
    printf ("Inside foo. var=%d\n", var);    //Will access global variable
}

void bar(int var)
{
    printf ("Inside bar. var=%d\n", var);    //Will access parameter scope variable
}

int main(int argc, char *argv[])
{
    int var = 20;    //Local scope

    do
    {
        int var = 5;    //Block scope (local inner scope)
        printf ("Inside do-while. var=%d\n", var);    //Will access inner local scope
    } while(0);

    printf ("Inside main. var=%d\n", var);    //Will access local scope

    foo();    //Will access global scope

    bar(7);   //Will access parameter scope
}



这里我们有3个范围:示例第一行中的全局范围,2个本地范围(一个在函数中作为第一个函数行,一个在do-while块中作为内部局部范围),最后一个参数范围。

编译器策略只是使用最接近的声明。

现在如果你运行这个例子,你会得到:


Here we have 3 scopes: a global one in the very first line of the example, 2 local scopes (one in the function as very first function line, and one in the do-while block as inner local scope), and finally a parameter scope.
The compiler strategy is simply to use the most closer declaration.
Now if you run the example you will get:

Inside do-while. var=5
Inside main. var=20
Inside foo. var=10
Inside bar. var=7



正如您所看到的那样,第一个输出行显示了do-while循环中的值,该值是对该点的更接近的声明在哪里我们访问变量。

在第二个输出行,我们在主函数体中声明了值,再一个是更接近的值。

在第三个输出行,我们访问全局值,因为本地块中没有其他声明,更接近范围是全局值。

在最后一个输出行,我们有一个覆盖全局值的参数值。



现在我们回到全局声明:在函数作用域外声明的每个变量都自动创建为全局变量(除非你将它限制为当前模块,文件,使用 静态 限定符),可在所有模块的整个程序中使用。

请注意,本地和参数变量声明始终可以从代码中看到与全局声明t不同,使用它hat可以在一个模块中声明,并在另一个没有跟踪的地方使用。

但是我们怎么能告诉编译器我们想要使用的变量存在于程序的某个地方?在我们将所有模块链接在一起之前,每个模块都不会知道这样的变量,因为只有在那时我们才能访问模块上定义的所有符号。

C语言有一个限定符, extern ,告诉编译器这样的变量存在于某个地方并且它没有抱怨,因为链接器将在以后解决所有...;)编译器在找到这个时限定符相信我们并接受编译单元没有错误。



现在:你不能在函数内声明全局变量或所有作用域将不适用,所以:


As you can see the first output line shows the value in the do-while loop that is the closer declaration to the point where we access the variable.
On the second output line we have the value declared in the main function body, again the closer one.
On third output line we access the global value because there is no other declaration in the local block, and the closer scope is the global one.
On the last output line we have the parameter value that overrides the global one.

Now we go back the the global declaration: each variable declared outside a function scope is automatically created as global variable (unless you limit it to current module, file, using the static qualifier), and is available over the whole program in all modules.
Please note that local and parameter variables declarations are always visible from the code that will use it, unlike the global declarations that can be declared in a module and used in another where there is no trace of it.
But how can we tell to the compiler that a variable we want to use exist somewhere in the program? Each module will not know of such a variable until when we will link all modules together, because only at that time we have access to all symbols defined over the modules.
The C language have a qualifier, extern, that tells to the compiler that such a variable exists somewhere and it have not to complain, because the linker will solve all later... ;) The compiler when found this qualifier believe us and accept to compile the unit with no errors.

Now: you cannot declare global variables inside a function or all scoping will not be applicable , so:

void main()
{
    extern int n=10;
    printf("%d",n);
}



这是一个错误!! 因为 extern 限定符告诉编译器我们想要的声明一个全局变量。

in:


It's an error!! Because the extern qualifier tells to the compiler that we want declare a global variable.
While in:

#include<stdio.h>
extern int n=10;
void main()
{
    printf("%d",n);
}</stdio.h>



限定符 extern 是多余的!!



我希望现在澄清你的想法:)



PS 更清楚,如果你写:


The qualifier extern is redundant!!

I hope this clearified your mind now :)

P.S. to be more clear, if you write:

void main()
{
    extern int a;
    printf("%d",a);
}



在这种情况下, extern int a; 用于通知编译器在程序的某个地方存在一个全局变量'a'。 告诉编译器该变量存在!

但如果你写:


In this case "extern int a;" is used to inform the compiler that exists a global variable , 'a', somewhere in the program. Just informs the compiler that the variable exist!
But if you write:

void main()
{
    extern int a = 10;
    printf("%d",a);
}



您不仅告知编译器(声明变量),而且您还想在函数内创建和初始化全局变量。而且,正如我之前所说,这是一个错误!!!!


You not only inform the compiler (declaring the variable), but you want also create and initialize the global variable inside a function. And, as I told before, this is an error!!!!


这篇关于怀疑在c中使用extern关键字...任何一个帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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