c 中的外部和全局

发布于 2024-08-29 03:21:45 字数 782 浏览 2 评论 0原文

谁能告诉我在 C 程序中使用 EXTERN 或 GLOBAL 变量是否有任何特殊要求? 如果我从 gloabl 更改为 extern,我看不出像下面这样的程序有任何区别。

#include <stdio.h>
#include <stdlib.h>
int myGlobalvar = 10;

int main(int argc, char *argv[])
{
  int myFunc(int);
  int i;
  i = 12;
  myGlobalvar = 100;
  printf("Value of myGlobalvar is %d ,  i = %d\n", myGlobalvar, i);
  i = myFunc(10);
  printf("Value of passed value : %d\n",i);
  printf("again Value of myGlobalvar is %d ,  i = %d\n", myGlobalvar, i);
  system("PAUSE");  
  return 0;
}
int myFunc(int i)
{
    i = 20 + 1000;
//extern int myGlobalvar;
myGlobalvar = 20000;
//    printf("Value of passed value : %d",i);
return i;
}

如果取消注释 extern int myGlobalvar,则该值不会更改。

两者之间有什么正确区别吗?

有人可以纠正我吗?

Can anyone please tell me is there any special requirement to use either EXTERN or GLOBAL variables in a C program?
I do not see any difference in a program like below, if I change from gloabl to extern.

#include <stdio.h>
#include <stdlib.h>
int myGlobalvar = 10;

int main(int argc, char *argv[])
{
  int myFunc(int);
  int i;
  i = 12;
  myGlobalvar = 100;
  printf("Value of myGlobalvar is %d ,  i = %d\n", myGlobalvar, i);
  i = myFunc(10);
  printf("Value of passed value : %d\n",i);
  printf("again Value of myGlobalvar is %d ,  i = %d\n", myGlobalvar, i);
  system("PAUSE");  
  return 0;
}
int myFunc(int i)
{
    i = 20 + 1000;
//extern int myGlobalvar;
myGlobalvar = 20000;
//    printf("Value of passed value : %d",i);
return i;
}

If uncomment extern int myGlobalvar, the value does not change.

Is there any correct difference between both?

Can anyone please correct me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

还不是爱你 2024-09-05 03:21:45

关键字 extern 的意思是“该变量的存储空间分配在其他地方”。它告诉编译器“我在这里引用 myGlobalvar,你以前没有见过它,但这没关系;链接器会知道你在说什么。”在您的具体示例中,它并不是特别有用,因为编译器确实知道myGlobalvar - 它是在同一翻译单元中先前定义的(.c.cc 文件。)当您想要引用当前翻译单元中的内容时,通常使用 extern,例如在您将链接到的库中定义的变量。

(当然,通常该库会在您应该包含的头文件中为您声明 extern 变量。)

The keyword extern means "the storage for this variable is allocated elsewhere". It tells the compiler "I'm referencing myGlobalvar here, and you haven't seen it before, but that's OK; the linker will know what you are talking about." In your specific example it's not particularly useful, because the compiler does know about myGlobalvar -- it's defined earlier in the same translation unit (.c or .cc file.) You normally use extern when you want to refer to something that is not in the current translation unit, such as a variable that's defined in a library you will be linking to.

(Of course, normally that library would declare the extern variables for you, in a header file that you should include.)

吝吻 2024-09-05 03:21:45

来自此处

C/C++ 中的全局变量是可以从任何模块访问的变量你的程序。

int myGlobalVariable;

这会为数据分配存储空间,并告诉编译器您想要使用名称“myGlobalVariable”访问该存储空间。

但是,如果您想从程序中的另一个模块访问该变量,该怎么办?您不能使用上面给出的相同语句,因为这样您将有 2 个名为“myGlobalVariable”的变量,这是不允许的。因此,解决方案是让其他模块声明变量而不定义它:

extern int myGlobalVariable;

这告诉编译器“在另一个名为 myGlobalVariable 的模块中定义了一个整数类型的变量。我希望您接受我访问它的尝试,但不要为其分配存储空间,因为另一个模块已经这样做了”。

From Here:

A global variable in C/C++ is a variable which can be accessed from any module in your program.

int myGlobalVariable;

This allocates storage for the data, and tells the compiler that you want to access that storage with the name 'myGlobalVariable'.

But what do you do if you want to access that variable from another module in the program? You can't use the same statement given above, because then you'll have 2 variables named 'myGlobalVariable', and that's not allowed. So, the solution is to let your other modules DECLARE the variable without DEFINING it:

extern int myGlobalVariable;

This tells the compiler "there's a variable defined in another module called myGlobalVariable, of type integer. I want you to accept my attempts to access it, but don't allocate storage for it because another module has already done that".

魄砕の薆 2024-09-05 03:21:45

简而言之:GLOBAL 变量在一个文件中声明。但只能通过前面的 EXTERN 单词(在另一个文件中)在另一个文件中访问它们。在同一个文件中,不需要EXTERN

例如:

my_file.cpp

int global_var = 3;
int main(){
}

您可以在同一文件中访问全局变量。无需使用EXTERN

my_file.cpp

int global_var = 3;
int main(){
    ++global_var;
    std::cout << global_var; // Displays '4'
}

根据定义,全局变量也可以被所有其他文件访问。
但是,在这种情况下,您需要使用 EXTERN 访问全局变量。

因此,在 my_file.cpp 声明 global_var 的情况下,在 other_file.cpp 中尝试以下操作:

other_file.cpp

int main(){
    ++global_var; // ERROR!!! Compiler is complaining of a 'non-declared' variable
    std::cout << global_var; 
}

相反,请执行以下操作:

int main(){
    extern int global_var;//Note: 'int global_var' without 'extern' would
                          // simply create a separate different variable
    ++global_var;         // and '++global_var' wouldn't work since it'll
                          // complain that the variable was not initiazed.
    std::cout << global_var; // WORKING: it shows '4'
}

In short: GLOBAL variables are declared in one file. But they can be accessed in another file only with the EXTERN word before (in this another file). In the same file, no need of EXTERN.

for example:

my_file.cpp

int global_var = 3;
int main(){
}

You can access the global variable in the same file. No need to use EXTERN:

my_file.cpp

int global_var = 3;
int main(){
    ++global_var;
    std::cout << global_var; // Displays '4'
}

Global variable, by definition, can also be accessed by all the other files.
BUT, in this case, you need to access the global variable using EXTERN.

So, with my_file.cpp declaring the global_var, in other_file.cpp if you try this:

other_file.cpp

int main(){
    ++global_var; // ERROR!!! Compiler is complaining of a 'non-declared' variable
    std::cout << global_var; 
}

Instead, do:

int main(){
    extern int global_var;//Note: 'int global_var' without 'extern' would
                          // simply create a separate different variable
    ++global_var;         // and '++global_var' wouldn't work since it'll
                          // complain that the variable was not initiazed.
    std::cout << global_var; // WORKING: it shows '4'
}
野却迷人 2024-09-05 03:21:45

由于 myGlobalvar 已在函数 myFunc 之前定义。它在函数内部的声明是多余的。

但是,如果定义位于函数之后,则我们必须有声明

int myFunc(int i)
{
    i = 20 + 1000;
    extern int myGlobalvar; // Declaration must now.
    myGlobalvar = 20000;
    printf("Value of passed value : %d",i);
    return i;
}

int myGlobalvar = 10; // Def after the function.

Since myGlobalvar has been defined before the function myFunc. Its declaration inside the function is redundant.

But if the definition was after the function, we must have the declaration.

int myFunc(int i)
{
    i = 20 + 1000;
    extern int myGlobalvar; // Declaration must now.
    myGlobalvar = 20000;
    printf("Value of passed value : %d",i);
    return i;
}

int myGlobalvar = 10; // Def after the function.
时光病人 2024-09-05 03:21:45

myGlobalVar 正如您所定义的,它是一个全局变量,从程序中的所有位置都可见。无需在同一个 .c 文件中将其声明为 extern 。这对于其他 .c 文件很有用,可以让编译器知道将要使用该变量。

myGlobalVar as you've defined it is a global variable, visible from all the places in your program. There's no need declaring it extern in the same .c file . That is useful for other .c files to let the compiler know this variable is going to be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文