What is a variable?
In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instruction s that tell the computer what to do and data that the program uses when it is running. The data consists of constants or fixed values that never change and variable values (which are usually initialized to "0" or some default value because the actual values will be supplied by a program's user). Usually, both constants and variables are defined as certain data type s. Each data type prescribes and limits the form of the data. Examples of data types include: an integer expressed as a decimal number, or a string of text characters, usually limited in length.
In object-oriented programming , each object contains the data variables of the class it is an instance of. The object's method s are designed to handle the actual values that are supplied to the object when the object is being used.
This was last updated in October 2021
Kenneth Leroy Busbee and Dave Braunschweig
Overview
A constant is a value that cannot be altered by the program during normal execution, i.e., the value is constant. When associated with an identifier, a constant is said to be “named,” although the terms “constant” and “named constant” are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable.[1]
Discussion
Understanding Constants
A constant is a data item whose value cannot change during the program’s execution. Thus, as its name implies – the value is constant.
A variable is a data item whose value can change during the program’s execution. Thus, as its name implies – the value can vary.
Constants are used in two ways. They are:
- literal constant
- defined constant
A literal constant is a value you type into your program wherever it is needed. Examples include the constants used for initializing a variable and constants used in lines of code:
In addition to literal constants, most textbooks refer to symbolic constants or named constants as a constant represented by a name. Many programming languages use ALL CAPS to define named constants.
C++ | #define PI 3.14159 or const double PI = 3.14159; |
C# | const double PI = 3.14159; |
Java | const double PI = 3.14159; |
JavaScript | const PI = 3.14159; |
Python | PI = 3.14159 |
Swift | let pi = 3.14159 |
Technically, Python does not support named constants, meaning that it is possible (but never good practice) to change the value of a constant later. There are workarounds for creating constants in Python, but they are beyond the scope of a first-semester textbook.
Defining Constants and Variables
Named constants must be assigned a value when they are defined. Variables do not have to be assigned initial values. Variables once defined may be assigned a value within the instructions of the program.
C++ | double value = 3; |
C# | double value = 3; |
Java | double value = 3; |
JavaScript | var value = 3; let value = 3; |
Python | value = 3 |
Swift | var value:Int = 3 |
Key Terms
constantA data item whose value cannot change during the program’s execution.variableA data item whose value can change during the program’s execution.References
- cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
Various textbooks describe constants using different terminology. Added to the complexity are the explanations from various industry professionals will vary greatly. Let's see if we can clear it up. A constant is a data item whose value cannot
change during the program's execution. Thus, as its name implies – their value is constant. A variable is a data item whose value can change during the program's execution. Thus, as its name implies – their value can vary. Constants are used in two ways within C++. They are: Most text books refer to either symbolic constants or named constants but these two refer to the same concept. A
symbolic constant is represented by a name similar to how we name variables. Let's say it backwards; the identifier name is the symbol that represents the data item. Within C++ identifier names have some rules. One of the rules says those names should be meaningful. Another rule about using ALL CAPS FOR CONSTANTS is an industry rule. There are two ways to create symbolic or named constants: #define PI 3.14159 Called a defined constant, we have discussed
this concept when we talked about preprocessor directives in Lesson 1, because it uses a textual substitution method controlled by the compiler pre-processor command word "define".Understanding Constants vs Variables
const double PI = 3.14159;
The second one is called sometimes called constant variable but that name is contradictory all by itself. How can it be constant and vary at the same time? The better name for the second one is a memory constant because they have a "specific storage location in memory".
As the name suggests the name constants is given to such variables or values in C++ programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants etc. Every constant has some range. The integers that are too big to fit into an int will be taken as long. Now there are various ranges that differ from unsigned to signed bits. Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int varies from 0 to 255.
Defining Constants:
In C/C++ program we can define constants in two ways as shown below:
- Using #define preprocessor directive
- Using a const keyword
Let us now learn about above two ways in details:
- Using #define preprocessor directive: This directive is used to declare an alias name for existing variable or any value. We can use this to declare a constant as shown below: #define identifierName value
- identifierName: It is the name given to constant.
- value: This refers to any value assigned to identifierName.
Example
Output:
Integer Constant: 10 Floating point Constant: 4.5 Character Constant: GRefer Macros and Preprocessors in C for details.
- using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword.
You must set a value on a const variable at the time you create it:
Below program shows how to use const to declare constants of different data types:
#include <iostream> using namespace std; int main() { // int constant const int intVal = 10; // Real constant const float floatVal = 4.14; // char constant const char charVal = 'A'; // string constant const string stringVal = "ABC"; cout << "Integer Constant: " << intVal << "\n"; cout << "Floating point Constant: " << floatVal << "\n"; cout << "Character Constant: "<< charVal << "\n"; cout << "String Constant: "<< stringVal << "\n"; return 0; }Output:
Integer constant: 10 Floating point constant: 4.14 Character constant: A String constant: ABCLimits
Each of the different data types have upper and lower limits depending on the data type, the processor and the compiler. The C++ standard has a defined minimum and maximum for all the different data types.
The limits.h header determines various properties of the various variable types. The macros defined in this header, limits the values of various variable types like char, int and long.
These limits specify that a variable cannot store any value beyond these limits, for example an unsigned character can store up to a maximum value of 255.
Library Macros
The following values are implementation-specific and defined with the #define directive, but these values may not be any lower than what is given here.
CHAR_BIT | 8 | Defines the number of bits in a byte. |
SCHAR_MIN | -128 | Defines the minimum value for a signed char. |
SCHAR_MAX | +127 | Defines the maximum value for a signed char. |
UCHAR_MAX | 255 | Defines the maximum value for an unsigned char. |
CHAR_MIN | -128 | Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero. |
CHAR_MAX | +127 | Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX. |
MB_LEN_MAX | 16 | Defines the maximum number of bytes in a multi-byte character. |
SHRT_MIN | -32768 | Defines the minimum value for a short int. |
SHRT_MAX | +32767 | Defines the maximum value for a short int. |
USHRT_MAX | 65535 | Defines the maximum value for an unsigned short int. |
INT_MIN | -2147483648 | Defines the minimum value for an int. |
INT_MAX | +2147483647 | Defines the maximum value for an int. |
UINT_MAX | 4294967295 | Defines the maximum value for an unsigned int. |
LONG_MIN | -9223372036854775808 | Defines the minimum value for a long int. |
LONG_MAX | +9223372036854775807 | Defines the maximum value for a long int. |
ULONG_MAX | 18446744073709551615 | Defines the maximum value for an unsigned long int. |