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. Show
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 OverviewA 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] DiscussionUnderstanding ConstantsA 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:
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: 21 12.34 'A' "Hello world!" false null 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.
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 VariablesNamed 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.
Key TermsconstantA data item whose value cannot change during the program’s execution.variableA data item whose value can change during the program’s execution.References
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". 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:
Let us now learn about above two ways in details:
#include <iostream> using namespace std; #define val 10 #define floatVal 4.5 #define charVal 'G' int main() { cout << "Integer Constant: " << val << "\n"; cout << "Floating point Constant: " << floatVal << "\n"; cout << "Character Constant: "<< charVal << "\n"; return 0; } Output: Integer Constant: 10
Floating point Constant: 4.5
Character Constant: G
Refer Macros and Preprocessors in C for details.
You must set a value on a const variable at the time you create it: RIGHT: You MUST set the value at the time of creation const int myAmount = 10; WRONG: you can not attempt to set a const variable after it is defined const int myAmount; myAmount = 10; 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: ABC
LimitsEach 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 MacrosThe following values are implementation-specific and defined with the #define directive, but these values may not be any lower than what is given here.
Which term refers to a data item whose value can change during the program's execution?A variable is a data item whose value can change during the program's execution. Thus, as its name implies – the value can vary.
What is called the values that do not change during the execution of program?Data values that stay the same every time a program is executed are known as constants. Constants are not expected to change.
Is a named unit of data that is assigned a value if the value is modified the name does not change?A variable is a named unit of data that is assigned a value. If the value is modified, the name does not change.
What does a data item data type describe?A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.
|