What Is a Variable?

Man's face half revealed from behind a computer screen in office of computers
Gunnar Svanberg/Iconica/Getty Images

A variable is a name for a place in the computer's memory where you store some data.

Imagine a very large warehouse with lots of storage bays, tables, shelves, special rooms etc. These are all places where you can store something. Let's imagine we have a crate of beer in the warehouse. Where exactly is it located?

We wouldn't say that it is stored 31' 2" from the west wall and 27' 8" from the north wall. In programming terms we also wouldn't say that my total salary paid this year is stored in four bytes starting at location 123,476,542,732 in RAM.

Data in a PC

The computer will place variables in different locations each time our program is run. However, our program knows exactly where the data is located. We do this by creating a variable to refer to it and then let the compiler handle all the messy details about where it is actually located. It is far more important to us to know what type of data we will be storing in the location.

In our warehouse, our crate might be in section 5 of shelf 3 in the drinks area. In the PC, the program will know exactly where its variables are located.

Variables Are Temporary

They exist just as long as they are needed and are then disposed of. Another analogy is that variables are like numbers in a calculator. As soon as you hit the clear or power off buttons, the display numbers are lost.

How Big Is a Variable

As big as is needed and no more. The smallest a variable can be is one bit and the largest is millions of bytes. Current processors handle data in chunks of 4 or 8 bytes at a time (32 and 64 bit CPUs), so the bigger the variable, the longer it will take to read or write it. The size of the variable depends on its type.

What Is a Variable Type?

In modern programming languages, variables are declared to be of a type.

Apart from numbers, the CPU does not make any kind of distinction between the data in its memory. It treats it as a collection of bytes. Modern CPUs (apart from those in mobile phones) can usually handle both integer and floating point arithmetic in hardware. The compiler has to generate different machine code instructions for each type, so knowing what the type of variable helps it generate optimal code.

What Types of Data Can a Variable Hold?

The fundamental types are these four.

  • Integers (both signed and unsigned) 1,2,4 or 8 bytes in size. Usually referred to as ints.
  • Floating Point Numbers up to 8 bytes in size.
  • Bytes. These are organized in 4s or 8s (32 or 64 bits) and read in and out of the CPU's registers.
  • Text strings, up to billions of bytes in size. CPUs have special instructions for searching through large blocks of bytes in memory. This is very handy for text operations.

There is also a general variable type, often used in scripting languages.

  • Variant - This can hold any type but is slower to use.

Example of Data Types

  • Arrays of types- single dimension like drawers in a cabinet, two-dimensional like post office sorting boxes or three dimensional like a pile of beer crates. There can be any number of dimensions, up to the limits of the compiler.
  • Enums which are a restricted subset of integers. Read about what is an enum is.
  • Structs are a composite variable where several variables are lumped together in one big variable.
  • Streams provide a way to manage files. They're a form of a string.
  • Objects, are like structs but with much more sophisticated data handling.

Where are Variables Stored?

In memory but in different ways, depending on how they are used.

  • Globally. All parts of the program can access and change the value. This is how older languages like Basic and Fortran used to handle data and it is not considered a good thing. Modern languages tend to discourage global storage though it is still possible.
  • On the Heap. This is the name for the main area used. In C and C++, access to this is via pointer variables.
  • On the Stack. The stack is a block of memory that is used to store parameters passed into functions, and variables that exist local to functions.

Conclusion

Variables are essential to procedural programming, but it is important not to get too hung up on the underlying implementation unless you are doing systems programming or writing applications that have to run in a small amount of RAM.

Our rules regarding variables:

  1. Unless you are tight on ram or have large arrays, stick with ints rather than a byte (8 bits) or short int (16 bits). Especially on 32 Bit CPUs, there is an extra delay penalty in accessing less than 32 bits.
  2. Use floats instead of doubles unless you need the precision.
  3. Avoid variants unless really necessary. They are slower.

 

Format
mla apa chicago
Your Citation
Bolton, David. "What Is a Variable?" ThoughtCo, Apr. 5, 2023, thoughtco.com/what-is-a-variable-958334. Bolton, David. (2023, April 5). What Is a Variable? Retrieved from https://www.thoughtco.com/what-is-a-variable-958334 Bolton, David. "What Is a Variable?" ThoughtCo. https://www.thoughtco.com/what-is-a-variable-958334 (accessed March 28, 2024).