Daniel Cazares
Daniel Cazares

Reputation: 183

What is the difference between Global and File Scope?

I found another thread containing some information, but I still am a bit confused. I think this is correct, however. I figured that global scope and file scope were the same thing, though?

My options are local/function scope, global scope, file scope. This is referring to C, if that makes a difference.

QUESTION                      MY ANSWER

1) external static variables  file scope

2) internal static variables  local scope

3) global functions           global scope

4) global variables           global scope

5) local variables            local scope

6) formal parameters          local scope

7) static functions           file scope

Upvotes: 0

Views: 1239

Answers (4)

haccks
haccks

Reputation: 106132

File/global scope:

A name has file scope if the identifier's declaration appears outside of any block. A name with file scope and internal linkage is visible from the point where it is declared to the end of the translation unit.

File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared.

static int foo1;    // file scoped variable
float foo2;         // global variable

int main(){
     ...
}

Upvotes: 2

LearningC
LearningC

Reputation: 3162

static functions in c get the file scope i.e. it can be used only in the file in which it is defined. where as global scope means the function can be called even from other C files.
So file scope means you can use that function in only that C file. and global scope means the function can be called from other C files. it comes into picture when you merge 2 C files when you compile to create a executable.

Upvotes: 1

Raging Bull
Raging Bull

Reputation: 18767

A name has file scope if the identifier's declaration appears outside of any block. A name with file scope and internal linkage is visible from the point where it is declared to the end of the translation unit.

Global scope or global namespace scope is the outermost namespace scope of a program, in which objects, functions, types and templates can be defined. A name has global namespace scope if the identifier's declaration appears outside of all blocks, namespaces, and classes.

Upvotes: 1

Eric J.
Eric J.

Reputation: 150238

I figured that global scope and file scope were the same thing, though?

File scope means the identifier is only "known" within the specific file it appears in, e.g. main.c.

Global scope means it is visible to the entire program, no matter which c file it is defined in.

Upvotes: 2

Related Questions