Global variable


In computer programming, a global variable is a variable with global scope, meaning that it is visible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state. In compiled languages, global variables are generally static variables, whose extent is the entire runtime of the program, though in interpreted languages, global variables are generally dynamically allocated when declared, since they are not known ahead of time.
In some languages, all variables are global, or global by default, while in most modern languages variables have limited scope, generally lexical scope, though global variables are often available by declaring a variable at the top level of the program. In other languages, however, global variables do not exist; these are generally modular programming languages that enforce a module structure, or class-based object-oriented programming languages that enforce a class structure.

Use

Interaction mechanisms with global variables are called global environment mechanisms. The global environment paradigm is contrasted with the local environment paradigm, where all variables are local with no shared memory.
Global variables are used extensively to pass information between sections of code that do not share a caller/callee relation like concurrent threads and signal handlers. Languages where each file defines an implicit namespace eliminate most of the problems seen with languages with a global namespace though some problems may persist without proper encapsulation. Without proper locking, code using global variables will not be thread-safe except for read only values in protected memory.

Environment variables

are a facility provided by some operating systems. Within the OS's shell they are a kind of variable: for instance, in unix and related systems an ordinary variable becomes an environment variable when the export keyword is used. Program code other than shells has to access them by API calls, such as
getenv and setenv.
They are local to the process in which they were set. That means if we open two terminal windows and change value of environment variable in one window, that change will not be seen by other window.
When a child process is created, it inherits all the environment variables and their values from the parent process. Usually, when a program calls another program, it first creates a child process by forking, then the child adjusts the environment as needed and lastly the child replaces itself with the program to be called. Child processes therefore cannot use environment variables to communicate with their peers, avoiding the action at a distance problem.

Global-only and global-by-default

A number of non-structured languages, such as BASIC, COBOL and Fortran I only provide global variables. Fortran II introduced subroutines with local variables, and the COMMON keyword for accessing global variables. Usage of COMMON in FORTRAN continued in FORTRAN 77, and influenced later languages such as PL/SQL. Named COMMON groups for globals behave somewhat like structured namespaces. Variables are also global by default in FORTH, Lua, Perl, and most shells.

By language

C and C++

The C language does not have a global keyword. However, 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 ; 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. External linkage, however, is not sufficient for such a variable's use in other files: for a compilation unit to correctly access such a global variable, it will need to know its type. This is accomplished by declaring the variable in each file using the extern keyword. Such extern declarations are often placed in a shared header file, since it is common practice for all.c files in a project to include at least one .h file: the standard header file errno.h is an example, making the errno variable accessible to all modules in a project. Where this global access mechanism is judged problematic, it can be disabled using the static keyword which restricts a variable to file scope, and will cause attempts to import it with extern to raise a compilation error.
An example of a "global" variable in C:

  1. include
// This is the file-scope variable, visible only in
// this compilation unit.
static int shared = 3;
// This one has external linkage.
extern int over_shared;
// Also internal linkage.
int over_shared_too = 2;
static void ChangeShared
static void LocalShadow
static void ParamShadow
int main

As the variable is an external one, there is no need to pass it as a parameter to use it in a function besides main. It belongs to every function in the module.
The output will be:
3
5
5
5

Java

Some languages, like Java, don't have global variables. In Java, all variables that are not local variables are fields of a class. Hence all variables are in the scope of either a class or a method. In Java, static fields exist independently of any instances of the class and one copy is shared among all instances; hence public static fields are used for many of the same purposes as global variables in other languages because of their similar "sharing" behavior:

public class Global

PHP

has a global keyword and a number of unusual ways of using global variables.
Variables declared outside functions have file scope. However, they are not accessible inside functions unless imported with the global keyword.
However, some predefined variables, known as superglobals are always accessible.
They are all arrays. A general purpose one is the $GLOBALS superglobal, which contains all the variables
defined out of function scope. Changes to its elements change the original variables, and additions create new variables.
The superglobals $_POST and $_GET are widely used in web programming.

Other languages