Getting Started
...
Glossary
N-Z Terms

Variable

4min

A variable in computer programming is a named location in memory where data can be stored. Variables are used to store information that is needed by a program. For example, a variable can be used to store the player's score in a game or the current temperature in a weather forecast.

Variables are created using a variable declaration statement. The general syntax for a variable declaration statement is:

variable_type variable_name;

Where variable_type is the type of data that the variable will store, and variable_name is the name of the variable. For example, the following statement declares a variable called score that will store an integer:

int score;

Once a variable has been declared, it can be used to store data using an assignment statement. The general syntax for an assignment statement is:

variable_name = expression;

Where expression is an expression that evaluates to a value of the same type as variable_name. For example, the following statement assigns the value 10 to the variable score:

score = 10;

Variables can be used to store data of any type, including integers, floating-point numbers, strings, and Boolean values. The type of data that a variable can store is determined by the variable's type. For example, the variable score can only store an integer value, while the variable name can store a string value.

Variables can be used to store data that is needed by a program. For example, a variable can be used to store the player's score in a game or the current temperature in a weather forecast. Variables can also be used to store temporary data that is used by a program. For example, a variable can be used to store the result of a calculation that is needed by the rest of the program.

Feedback

Please be sure to submit issues or feature requests through the embedded feedback form. In the event it is a major issue please contact us directly through Discord.

Updated 03 Apr 2024
Doc contributor
Did this page help you?