Definition of Variable

Variable Types Categorize the Data Stored in a Program

Equifax Exploit
Smith Collection/Gado / Getty Images

A variable is a way of referring to a storage area in a computer program. This memory location holds values—numbers, text or more complicated types of data like payroll records.

Operating systems load programs into different parts of the computer's memory so there is no way of knowing exactly which memory location holds a particular variable before the program is run. When a variable is assigned a symbolic name like "employee_payroll_id," the compiler or interpreter can work out where to store the variable in memory.

Variable Types

When you declare a variable in a program, you specify its type, which can be chosen from integral, floating point, decimal, boolean or nullable types. The type tells the compiler how to handle the variable and check for type errors. The type also determines the position and size of the variable's memory, the range of values that it can store and the operations that can be applied to the variable. A few basic variable types include:

int - Int is short for "integer." It is used to define numeric variables holding whole numbers. Only negative and positive whole numbers can be stored in int variables. 

null - A nullable int has the same range of values as int, but it can store null in addition to whole numbers.

char - A char type consists of Unicode characters—the letters that represent most of the written languages. 

bool - A bool is a fundamental variable type that can take only two values: 1 and 0, which correspond to true and false. 

float, double and decimal - these three types of variables handle whole numbers, numbers with decimals and fractions. The difference between the three lies in the range of values. For example, double is twice the size of float, and it accommodates more digits.

Declaring Variables

Before you can use a variable, you have to declare it, which means you have to assign it a name and a type. After you declare a variable, you can use it to store the type of data you declared it to hold. If you try to use a variable that hasn't been declared, your code won't compile. Declaring a variable in C# takes the form:

<data_type> <variable_list>;

The variable list consists of one or more identifier names separated by commas. For example:

 int i, j, k;

 char c, ch;

Initializing Variables

Variables are assigned a value using an equal sign followed by a constant. The form is:

<data_type> <variable_name> = value;

You can assign a value to a variable at the same time you declare it or at a later time. For example:

 int i = 100;


 short a;
int b;
double c;

 /*actual initialization */
a = 10;
b = 20;
c = a + b;

About C# 

C# is an object-oriented language that does not use any global variables. Although it could be compiled, it is almost always used in combination with the .NET framework, therefore applications written in C# are run on computers with .NET installed.

mla apa chicago
Your Citation
Bolton, David. "Definition of Variable." ThoughtCo, Apr. 5, 2023, Bolton, David. (2023, April 5). Definition of Variable. Retrieved from Bolton, David. "Definition of Variable." ThoughtCo. (accessed June 4, 2023).