<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Global_variable</id>
	<title>Global variable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Global_variable"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Global_variable&amp;action=history"/>
	<updated>2026-04-21T21:39:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Global_variable&amp;diff=178393&amp;oldid=prev</id>
		<title>24.50.56.74: /* C and C++ */ Indentation of 4 spaces</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Global_variable&amp;diff=178393&amp;oldid=prev"/>
		<updated>2025-09-12T15:32:03Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;C and C++: &lt;/span&gt; Indentation of 4 spaces&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Computer programming, a variable accessible throughout a computer program}}&lt;br /&gt;
{{refimprove|date=July 2009}}&lt;br /&gt;
&lt;br /&gt;
In [[computer programming]], a &amp;#039;&amp;#039;&amp;#039;global variable&amp;#039;&amp;#039;&amp;#039; is a variable with global [[scope (programming)|scope]], meaning that it is visible (hence accessible) throughout the program, unless [[Variable shadowing|shadowed]]. The set of all global variables is known as the &amp;#039;&amp;#039;global environment&amp;#039;&amp;#039; or &amp;#039;&amp;#039;global state.&amp;#039;&amp;#039; In [[Compiled language|compiled languages]], global variables are generally [[static variable]]s, whose [[Variable (programming)#Scope and extent|extent]] (lifetime) is the entire runtime of the program, though in [[Interpreted_language|interpreted languages]] (including [[command-line interpreter]]s), global variables are generally dynamically allocated when declared, since they are not known ahead of time.&lt;br /&gt;
&lt;br /&gt;
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 programming|class-based]] [[object-oriented programming language]]s that enforce a class structure.&lt;br /&gt;
&lt;br /&gt;
==Use==&lt;br /&gt;
Interaction mechanisms with global variables are called &amp;#039;&amp;#039;&amp;#039;global environment&amp;#039;&amp;#039;&amp;#039; (see also &amp;#039;&amp;#039;&amp;#039;global state&amp;#039;&amp;#039;&amp;#039;) mechanisms. The global environment paradigm is contrasted with the [[local environment]] paradigm, where all variables are [[subprogram#Local variables, recursion and reentrancy|local]] with no [[Shared memory (interprocess communication)|shared memory]] (and therefore all interactions can be reconducted to [[message passing]]).&lt;br /&gt;
&lt;br /&gt;
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 (including C) 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 (such as with a [[mutex]]), code using global variables will not be [[thread-safe]] except for read only values in [[protected memory]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Global-only and global-by-default==&lt;br /&gt;
&lt;br /&gt;
A number of non-[[structured programming|structured]] languages, such as (early versions of) [[BASIC]], [[COBOL]] and [[Fortran]] I (1956) only provide global variables. Fortran II (1958) introduced subroutines with local variables, and the COMMON keyword for accessing global variables.  Usage of COMMON in FORTRAN continued in FORTRAN 77,&amp;lt;ref&amp;gt;{{cite web|url=https://web.stanford.edu/class/me200c/tutorial_77/13_common.html|title=Fortran 77 Tutorial}}&amp;lt;/ref&amp;gt; and influenced later languages such as PL/SQL.  Named COMMON groups for globals behave somewhat like structured namespaces.&amp;lt;ref&amp;gt;{{cite web|url=http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/first_steps/stack_and_heap.html|title=First Steps: Stack &amp;amp; Heap Objects}}&amp;lt;/ref&amp;gt; Variables are also global by default in [[Forth (programming language)|Forth]], [[Lua (programming language)|Lua]], [[Perl]], and most shells.&lt;br /&gt;
&lt;br /&gt;
==By language==&lt;br /&gt;
&lt;br /&gt;
===C and C++===&lt;br /&gt;
&lt;br /&gt;
The C language does not have a &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; [[keyword (computer programming)|keyword]]. However, variables declared outside a function have &amp;quot;file scope,&amp;quot; meaning they are visible within the file. Variables declared with file scope are visible between their declaration and the end of the compilation unit (&amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; file) (unless shadowed by a like-named object in a nearer scope, such as a local variable); and they implicitly have external linkage and are thus visible to not only the &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; 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&amp;#039;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 &amp;lt;code&amp;gt;extern&amp;lt;/code&amp;gt; keyword. (It will be &amp;#039;&amp;#039;declared&amp;#039;&amp;#039; in each file but may be &amp;#039;&amp;#039;defined&amp;#039;&amp;#039; in only one.) Such &amp;lt;code&amp;gt;extern&amp;lt;/code&amp;gt; declarations are often placed in a shared header file, since it is common practice for all &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; files in a project to include at least one &amp;lt;code&amp;gt;.h&amp;lt;/code&amp;gt; file: the standard header file &amp;lt;code&amp;gt;errno.h&amp;lt;/code&amp;gt; is an example, making the &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; variable accessible to all modules in a project. Where this global access mechanism is judged problematic, it can be disabled using the [[Static (keyword)|&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword]] which restricts a variable to file scope, and will cause attempts to import it with &amp;lt;code&amp;gt;extern&amp;lt;/code&amp;gt; to raise a compilation (or linking) error.&amp;lt;ref&amp;gt;C in a Nutshell, P.Prinz &amp;amp; T Crawford, 2006, O&amp;#039;Reilly, Ch 11&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example of a &amp;quot;global&amp;quot; variable in [[C (programming language)|C]]:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// This is the file-scope variable (with internal linkage), visible only in&lt;br /&gt;
// this compilation unit.&lt;br /&gt;
static int shared = 3;&lt;br /&gt;
&lt;br /&gt;
// This one has external linkage (not limited to this compilation unit).&lt;br /&gt;
extern int over_shared;&lt;br /&gt;
&lt;br /&gt;
// Also internal linkage.&lt;br /&gt;
int over_shared_too = 2;&lt;br /&gt;
&lt;br /&gt;
static void changeShared() {&lt;br /&gt;
    // Reference to the file-scope variable in a function.&lt;br /&gt;
    shared = 5;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void localShadow() {&lt;br /&gt;
    // Local variable that will hide the global of the same name.&lt;br /&gt;
    int shared;&lt;br /&gt;
&lt;br /&gt;
    // This will affect only the local variable and will have no effect on the&lt;br /&gt;
    // file-scope variable of the same name.&lt;br /&gt;
    shared = 1000;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void paramShadow(int shared) {&lt;br /&gt;
    // This will affect only the parameter and will have no effect on the file-&lt;br /&gt;
    // scope variable of the same name.&lt;br /&gt;
    shared = -shared;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    // Reference to the file-scope variable.&lt;br /&gt;
&lt;br /&gt;
    printf(&amp;quot;%d\n&amp;quot;, shared);&lt;br /&gt;
&lt;br /&gt;
    changeShared();&lt;br /&gt;
    printf(&amp;quot;%d\n&amp;quot;, shared);&lt;br /&gt;
&lt;br /&gt;
    localShadow();&lt;br /&gt;
    printf(&amp;quot;%d\n&amp;quot;, shared);&lt;br /&gt;
&lt;br /&gt;
    paramShadow(1);&lt;br /&gt;
    printf(&amp;quot;%d\n&amp;quot;, shared);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The output will be:&lt;br /&gt;
 3&lt;br /&gt;
 5&lt;br /&gt;
 5&lt;br /&gt;
 5&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Some languages, like Java, don&amp;#039;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 (also known as [[class variable]]s) 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 &amp;quot;sharing&amp;quot; behavior:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;br /&gt;
public class Global {&lt;br /&gt;
    public static int a;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
[[PHP]] has a &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; keyword and a number of unusual ways of using global variables.&lt;br /&gt;
Variables declared outside functions have file scope (which is for most purposes the widest scope). However, they are not accessible inside functions unless imported with the &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; keyword (i.e., the keyword &amp;#039;&amp;#039;accesses&amp;#039;&amp;#039; global variables, it does not &amp;#039;&amp;#039;declare&amp;#039;&amp;#039; them).&lt;br /&gt;
&lt;br /&gt;
However, some predefined variables, known as &amp;#039;&amp;#039;superglobals&amp;#039;&amp;#039; are always accessible. &lt;br /&gt;
They are all arrays. A general purpose one is the &amp;lt;code&amp;gt;$GLOBALS&amp;lt;/code&amp;gt; superglobal, which contains all the variables&lt;br /&gt;
defined out of function scope. Changes to its elements change the original variables, and additions create new variables.&lt;br /&gt;
The superglobals &amp;lt;code&amp;gt;$_POST&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$_GET&amp;lt;/code&amp;gt; are widely used in web programming.&lt;br /&gt;
&lt;br /&gt;
===Other languages===&lt;br /&gt;
&lt;br /&gt;
* In [[Python (programming language)|Python]] and [[MATLAB]] a global variable can be declared anywhere with the &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; keyword.&amp;lt;ref&amp;gt;{{cite web|title=What are the rules for local and global variables in Python?|url=https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python|website=docs.python.org|publisher=Python Software Foundation|access-date=4 June 2020}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|title=Declare variables as global|url=http://in.mathworks.com/help/matlab/ref/global.html|website=in.mathworks.com|publisher=The MathWorks, Inc.|access-date=7 February 2015}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* [[Ruby (programming language)|Ruby]]&amp;#039;s global variables are distinguished by a &amp;#039;&amp;lt;code&amp;gt;$&amp;lt;/code&amp;gt;&amp;#039; [[sigil (computer programming)|sigil]]. A number of predefined globals exist, for instance &amp;lt;code&amp;gt;$$&amp;lt;/code&amp;gt; is the current [[process ID]].&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[[Local variable]]&lt;br /&gt;
*[[Non-local variable]]&lt;br /&gt;
*[[Singleton pattern]]&lt;br /&gt;
*[[Variable (computer science)|Variables]]&lt;br /&gt;
**[[Static variable]]&lt;br /&gt;
**[[External variable]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Variable (computer science)]]&lt;br /&gt;
	&lt;br /&gt;
[[de:Variable (Programmierung)#Variablen in einer Blockstruktur]]&lt;/div&gt;</summary>
		<author><name>24.50.56.74</name></author>
	</entry>
</feed>