简体   繁体   中英

global scope vs file scope

What is right to say about global variable:

If it is declared without static keyword:

  1. It has file scope and external linkage OR
  2. It has program scope and is visible to all files in the program

If it is declared with static keyword:

  1. It has global scope and internal linkage OR
  2. It has file scope and internal linkage

And how is global namespace scope related to this?

This notions are really confusing altough i think I understand how it works, but at different places they call it in different ways so i don't know which is right.

The answers for the first question are both incorrect.

The variable will have extrenal linkage indeed but will be visible in other program units only if it is declared in it.

As for the second question then indeed the variable will have file scope and internal linkage.

Any variable declared outside some explicitly specified namespace is considered as declared in the global namespace. Variavles with external or internal linkage can be declared in any namespace including the global namespace.

We are speaking about variables that are declared outside any function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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