Difference between File Scope and Global Scope
Asked Answered
S

5

17

I am a student and I am confused about global and file scope variables in C and C++. Is there any difference in both perspectives? If yes, please explain in detail.

Shammy answered 20/2, 2014 at 5:16 Comment(3)
drdobbs.com/cpp/scope-regions-in-c/240002006Puseyism
A similar question was answered here, you may want to take a look #14027817Ruthannruthanne
I want to ask not for difference between them b/c in your question mention i checked that examples that works same in C++ for file and global scope thats why i asked this questionShammy
M
26

A variable with file scope can be accessed by any function or block within a single file. To declare a file scoped variable, simply declare a variable outside of a block (same as a global variable) but use the static keyword.

static int nValue; // file scoped variable
float fValue; // global variable

int main()
{
    double dValue; // local variable
}

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

Mollescent answered 20/2, 2014 at 5:24 Comment(2)
It's not file scope. It's tranlation-unit scope. You can include two or more .cpp files into one and they can access statics of each other.Haywire
This answer confuses terminology, at least for C. File scope is simply outside any block or list of parameters (C99, 6.2.1/P4). fValue and nValue are both declared in file scope. Global is not C standard nomenclature. It is vernacular term for entities declared/defined in file scope. Both variables are global and both are declared in the file scope. For a variable to have either external or internal linkage does not make it either-or file scope / global. Please don't try to use those terms to indicate linkage.Lagerkvist
N
6

It is perhaps clearer to illustrate file (or more precisely, translation unit)-scope vs global scope when there are actually multiple translation units...

Take 2 files (each being it's own translation unit, since they don't include each other)

other.cpp

float global_var = 1.0f;
static float static_var = 2.0f;

main.cpp

#include <cstdio>

extern float global_var;
//extern float static_var; // compilation error - undefined reference to 'static_var'

int main(int argc, char** argv)
{
    printf("%f\n", global_var);
}

Hence the difference is clear.

Nanettenani answered 29/8, 2018 at 7:33 Comment(0)
M
2

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.

Example:

static int nValue; // file scoped variable
float fValue; // global variable

int main()
{
    double dValue; // local variable
}

Read more here.

Moolah answered 20/2, 2014 at 5:21 Comment(0)
O
1

File scope: Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit after its declaration. Names with file scope that do not declare static objects are often called global names.

In C++, file scope is also known as namespace scope.

Overtake answered 20/2, 2014 at 5:46 Comment(0)
D
-4

Read this carefully now.

You use those #include<'...'.h> statements at the top of your program/code. What you actually are doing there is telling the computer to refer to the functions prewritten in those *h*eader files.That is, those functions have file scope.You donot write the code of printf scanf and functions like these cauz they are somewhere in header files.

Variables declared outside a function have "file scope," meaning they are visible within the file. Variables declared with file scope are visible between their declaration and the end of the compilation unit (.c file) and they implicitly have external linkage and are thus visible to not only the .c file or compilation unit containing their declarations but also to every other compilation unit that is linked to form the complete program.

Global variables can, as the name suggests, be considered to be accessible globally(everywhere)

Dietz answered 20/2, 2014 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.