Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Global variable
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==By language== ===C and C++=== The C language does not have a <code>global</code> [[keyword (computer programming)|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 (<code>.c</code> 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 <code>.c</code> 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 <code>extern</code> keyword. (It will be ''declared'' in each file but may be ''defined'' in only one.) Such <code>extern</code> declarations are often placed in a shared header file, since it is common practice for all <code>.c</code> files in a project to include at least one <code>.h</code> file: the standard header file <code>errno.h</code> is an example, making the <code>errno</code> variable accessible to all modules in a project. Where this global access mechanism is judged problematic, it can be disabled using the [[Static (keyword)|<code>static</code> keyword]] which restricts a variable to file scope, and will cause attempts to import it with <code>extern</code> to raise a compilation (or linking) error.<ref>C in a Nutshell, P.Prinz & T Crawford, 2006, O'Reilly, Ch 11</ref> An example of a "global" variable in [[C (programming language)|C]]: <syntaxhighlight lang="C"> #include <stdio.h> // This is the file-scope variable (with internal linkage), visible only in // this compilation unit. static int shared = 3; // This one has external linkage (not limited to this compilation unit). extern int over_shared; // Also internal linkage. int over_shared_too = 2; static void ChangeShared() { // Reference to the file-scope variable in a function. shared = 5; } static void LocalShadow() { // Local variable that will hide the global of the same name. int shared; // This will affect only the local variable and will have no effect on the // file-scope variable of the same name. shared = 1000; } static void ParamShadow(int shared) { // This will affect only the parameter and will have no effect on the file- // scope variable of the same name. shared = -shared; } int main() { // Reference to the file-scope variable. printf("%d\n", shared); ChangeShared(); printf("%d\n", shared); LocalShadow(); printf("%d\n", shared); ParamShadow(1); printf("%d\n", shared); return 0; } </syntaxhighlight> 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 (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 "sharing" behavior: <syntaxhighlight lang="Java"> public class Global { public static int a; } </syntaxhighlight> === PHP === [[PHP]] has a <code>global</code> keyword and a number of unusual ways of using global variables. 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 <code>global</code> keyword (i.e., the keyword ''accesses'' global variables, it does not ''declare'' them). However, some predefined variables, known as ''superglobals'' are always accessible. They are all arrays. A general purpose one is the <code>$GLOBALS</code> 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 <code>$_POST</code> and <code>$_GET</code> are widely used in web programming. ===Other languages=== * In [[Python (programming language)|Python]] and [[MATLAB]] a global variable can be declared anywhere with the <code>global</code> keyword.<ref>{{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}}</ref><ref>{{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}}</ref> * [[Ruby (programming language)|Ruby]]'s global variables are distinguished by a '<code>$</code>' [[sigil (computer programming)|sigil]]. A number of predefined globals exist, for instance <code>$$</code> is the current [[process ID]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)