Variable (computer science)

In computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of data or object referred to as a value; or in simpler terms, a variable is a named container for a particular set of bits or type of data (like integer, float, string, etc...).[1] A variable can eventually be associated with or identified by a memory address. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context. This separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.[2][3][4][5]

Variables in programming may not directly correspond to the concept of variables in mathematics. The latter is abstract, having no reference to a physical object such as storage location. The value of a computing variable is not necessarily part of an equation or formula as in mathematics. Variables in computer programming are frequently given long names to make them relatively descriptive of their use, whereas variables in mathematics often have terse, one- or two-character names for brevity in transcription and manipulation.

A variable's storage location may be referenced by several different identifiers, a situation known as aliasing. Assigning a value to the variable using one of the identifiers will change the value that can be accessed through the other identifiers.

Compilers have to replace variables' symbolic names with the actual locations of the data. While a variable's name, type, and location often remain fixed, the data stored in the location may be changed during program execution.

  1. ^ Brookshear 2019, p. 249, "Variables and Data Types", "high-level programming languages allow locations in main memory to be referenced by descriptive names rather than by numeric addresses."
  2. ^ Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1986), Compilers: Principles, Techniques, and Tools, pp. 26–28, Bibcode:1986cptt.book.....A
  3. ^ Knuth, Donald (1997). The Art of Computer Programming. Vol. 1 (3rd ed.). Reading, Massachusetts: Addison-Wesley. pp. 3–4. ISBN 0-201-89683-4.
  4. ^ "Programming with variables". Khan Academy. Retrieved 23 March 2020.
  5. ^ "Scratch for Budding Coders". Harvard. Retrieved 23 March 2020.