Variables and constants are the essential components of every programming language.
1. Constants
A constant is a memory location where a value can be stored, similar to a variable. Constants, unlike variables, do not change in value. When a constant is created, it must be initialized.
In simple words, a Constant is a quantity that cannot be changed during the program execution. Constants in C++ are divided into two categories:
Literal and Symbolic Constant.
1. Literal Constants
The most apparent type of constant is literal. They are used to represent specific values in a program’s source code. We’ve previously used some in our previous tutorials to assign variables specific values or to express messages we wanted our programs to print, such as when we wrote:
int a = 20;
The number 20 in the above code is a literal constant.
String Constants, Numeric Constants, and Character Constants are the different types of Literal Constants.
1) String Constant
A string constant is a collection of alphabetic characters, digits, and special symbols enclosed in double quotation marks ""
. It can be up to 256 characters long. Some examples of valid string constants are shown below.
- “Hello, World!”
- “123 Main Street”
- “OpenAI is amazing!”
- “Password123!”
- “Email: [email protected]“
- “Special characters: !@#$%^&*()”
- “The quick brown fox jumps over the lazy dog.”
2) Numeric Constant
Numeric constant consists of positive and negative sign numbers. The numeric constant is further divided into four types:
Integer Constant, Floating-Point Constants, Hexadecimal Constants, and Octal Constants.
a. Integer Constant
Integer Constant consists of positive and negative signed numbers without the fraction or decimal point. For example 786, -670, 40, etc.
b. Floating Point Constant
The numeric constants having a decimal point are called floating-point constants.
For Example, -39.99, 40.1, 502.67, etc
These constants can also be either positive-signed or negative-signed. These constants can also be represented in their exponential form by using the alphabet 'e'
or 'E'
to denote the exponents of numbers.
c. Hexadecimal Constant
Hexadecimal constants are the constants expressed in the hexadecimal number system. Hexadecimal constants begin with 0x
and are followed by a series of digits ranging from 0 to 9 and A to F. The digits A to F denote values in the 10 to 15 range.
For example,
int a = 0x4fff;
d. Octal Constant
The constants represented in the octal number system are called octal constants. Octal constants start with 0, followed by a sequence of digits in the range 0 to 7. Octal numbers are integer numbers of base 8, and their digits are 0 to 7.
For example,
int i = 0466 ; // Octal Constant
int j = 0599 ; // Error: 9 is not an octal digit
2. Symbolic Constant
The term symbolic constant refers to a value that cannot be changed. After initialization, a constant’s value cannot be changed; it must be initialized again.
A symbolic constant PI, for example, can be used to represent the value of PI, which is 3.141593
. Wherever its value is required, PI can be written in a program.
Variables
In C++, a variable is a symbol representing a memory location that stores data. It acts as a container for holding values that can be accessed and manipulated during the program’s execution.
When a variable is declared, a portion of the computer’s memory, such as Random Access Memory (RAM), is allocated to store its value.
Declaration of Variables
Variable declaration is the process of defining a variable’s name and type. Before they can be used in the program, all variables must be defined.
The variable declaration gives the compiler information about the variable. The compiler uses the declaration to determine how much memory is required for each variable.
Different data types need specific amounts of memory. When a variable is declared, the required number of bytes is allocated.
The General Syntax of declaring a variable in C++;
data_type variable_name;
Consider the following line of code;
int salary = 34,600;
Salary
is an int data type variable that has been given the value 14
as an integer.
The variable’s int data type implies that it can only contain integers. We can use the character, floating, or double data type if we need to store decimals, characters, and exponentials.
To learn more about data types, consider reading our tutorial on data types here. The variables are created in RAM, which is temporary memory. That’s why the data stored in variables is also temporary.
For example,
int salary = 34,600; //The salary is 34,00;
salary = 41,300; //The salary is 41,300;
It is important to note that more than one variable of the same type can be declared in a single statement by separating them with commas.
For Example,
int a, b, c;
Initialization of Variables
The process of assigning a value to a variable at the time of declaration is called variable initialization. The equal sign =
is used to initialize a variable.
Syntax:
Data_type variable_name = value;
For Example:
int a = 4;
Rules For Naming Variables in C++
An identifier or variable name is the symbol used to represent a variable. Variables are named according to a set of rules. Below listed are the rules for naming variables:
- A valid identifier is a combination of one or more letters, numbers, or underscores
_
. - An identifier should not contain spaces, punctuation marks, or symbols. Letters, numbers, and single underscore characters are the only characters that can be used.
- Variable Identifiers must always start with a letter. They can also be preceded by an underscore
_
. - A variable cannot begin with a number in any case.
- The C++ language’s Reserved Words cannot be used as identifiers.
- Variable names should be descriptive (for example,
your_age
is better for a variable thanyg
ory
). - The length of a name should be limited. The name of the variable should be simple and easy to remember.