简体   繁体   English

可以在另一个文件中使用extern访问静态声明的全局变量吗?

[英]can static declared global variable can be accessed with extern in another file?

I have one doubt if i declared global variable with static. 如果我用静态声明全局变量,我有一个疑问。

file1.c 在file1.c

static int a=5;

main()
{
   func();
}

can it be access in another file2.c using extern ? 可以使用extern在另一个file2.c中访问吗?

file2.c file2.c中

func()
{
   extern int a;
   printf(a);
}

or only global variable declared without static can be access using extern? 或者只使用extern来声明没有静态声明的全局变量?

No! 没有!
static limits the scope of the variable to same translation unit . static将变量的范围限制为相同的转换单元
static gives the variable an Internal Linkage and this variable cannot be accessed beyond the translation unit in which was created. static为变量提供内部链接,并且在创建的转换单元之外无法访问此变量。

If you need to access a variable accross different files just drop the static keyword. 如果需要访问不同文件中的变量,只需删除static关键字即可。

No. A a in file1.c names a variable with internal linkage . 编号A a在file1.c中的名称具有内部链接的变量。 The same name used from a different translation unit will refer to a different variable. 从不同的翻译单元使用的相同名称将引用不同的变量。 That might also have internal linkage or it might (as in this case) have external linkage . 这可能也有内部联系,或者可能(如本案例中)有外部联系

Within the same file you can refer to a file scoped variable with internal linkage with extern , though. 但是,在同一个文件中,您可以引用带有与extern 内部链接的文件范围变量。

static int a;

int main(void) {
    extern int a; // still has internal linkage
    printf("%d\n", a);
}

That seems to be a misunderstanding of the two meanings of static: 这似乎是对静态的两个含义的误解:

  • for global declarations static means restriction to translation unit, so static is exactly meant to prevent what you are trying to do 对于全局声明,静态意味着对翻译单元的限制,因此静态意味着阻止您尝试执行的操作
  • for local variables static is a storage class, meaning that the variable keeps its value between function calls. 对于局部变量,static是一个存储类,这意味着变量在函数调用之间保持其值。 For global variables (on module level, ie outside of functions) this is always the case, so static is not needed. 对于全局变量(在模块级别,即在函数之外),总是如此,因此不需要静态。

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

相关问题 可以在C中将静态变量声明为extern吗? - Can a static variable be declared extern in C? 变量可以同时声明为static和extern吗? - Can a variable be declared both static and extern? 如何在另一个C文件中访问指向在一个C文件中声明的字符串文字的外部指针? - How can an extern pointer to string literal declared in one C file be accessed in another C file? 你能在另一个文件中使用#define变量吗? - Can you extern a #define variable in another file? extern 可以使 function 变量成为全局变量吗? - Can extern make a function variable a global variable? 如何存根声明为“ extern void _x”的变量/函数 - How can I stub an variable/function that is declared “extern void _x” 如何从另一个模块访问静态变量(模块范围内的静态变量)? 在C中 - How can a static variable (static in module scope) be accessed from another module? In C 头文件中声明的extern变量的链接器错误 - Error with linker for a extern variable declared in a header file 可以在 C/C++ 中将外部变量重新声明为 static 吗? - Can an extern variable be redeclared as static in C/C++? 在文件中使用带有 extern 关键字的变量,该关键字定义为 static 以及基本文件中的全局变量? - Using a variable in a file with extern keyword which is defined as static as well as global in the base file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM