A constant is a quantity that does not change its value over a period of time. A constant is a universal quantity, where it remains the same for every aspect of computation. This quantity can be stored at specified locations in the memory of the computer. Once a constant is defined in the program, it cannot be changed. Let us dive deeper into the article and understand the concept of Constants in programming languages in a better way.
Definition of Constants
Constant is an entity that refers to a fixed value of data and which cannot be modified. During execution/computation of programming, the value of a constant cannot be altered, it remains constant.
The most common example which can be considered to understand the concept of a constant is “PI”
PI = 3.1415927 whose value is universal and does not change.
Methods of Defining a Constant in Program
- The keyword used to define a constant in a program is – const
For example –
- const int i = 10; // i is a constant with value 10 which cannot be modified throughout the execution life cycle of a program.
- const float PI = 3.1415927; //PI value defined as a constant
- Another method of defining a constant is by using the #define preprocessor directive.
Syntax – #define identifiername value //where identifiername = name given to constant
For example –
- #define val 10
- #define char ‘G’
Types of Constants
Integer Constants
Integer constants are sequences of integers with a fixed value. They must not contain any decimal points or fractional numbers. Integer constants can either be positive or negative numbers. They include decimal system integers, octal system integers, hexadecimal system integers.
- Decimal integers – Set of digits ranging from 0 to 9. For Example – const int y = 123;
- Octal integers – Set of digits ranging from 0 to 7 which start with the number ‘0’ in the beginning/at the prefix. For example – const int x = 032;
- Hexadecimal integers – Set of digits ranging from 0 to 9 and alphabets from A to F where A indicates number 10 and F indicates number 15. 0x is used at the prefix of the value. For example – const int z = 0x14;
Browse more Topics Under Data Types, Variables and Constants
- Concept of Data types
- Built-in Data Types
- Access Modifier
- Variables of Built-in-Datatypes
- Declaration/Initialization of Variables
- Assignment Statement
- Type Modifier
Real Constants or Floating-point Constants
They contain fractional numbers that can be written in 2 forms, Fractional form, and Exponential form.
Eg. 140.9, 4578.218, 4.1 e 45 (i.e. 4.1 * 10^45), 5.0 e -2 (i.e. 5.0 * 10^-2)
Character Constants
Any single character from the defined character set is called a character constant. They include a single alphabet, single-digit, single symbol enclosed within single inverted commas.
Eg. ‘A’, ‘p’, ‘4’, ‘$’, ‘=’
String Constants
It contains a set/collection of characters that should be enclosed within double inverted commas.
Eg. “Hello”, “Length”, “ND123”
Advantages of Constants
- Immutability – It is useful if you know that the value of the variable will not change throughout your program lifecycle.
- Code Maintenance – If you need to use the Constant value multiple times in your program, assigning it to a variable and then using that variable will save time.
- Memory Allocation – It is generally stored in program memory (ROM) and not in the main memory(RAM) of the computer thus providing faster execution speed.
FAQs on Constants in Programming Language
Q1. Which is the wrong way of declaring a constant?
- const int x = 34;
- int const x = 34;
- int constant x =34;
- None
Answer – Option C
Q2. Which of the following is a primary constant?
- Integer constant
- Numeric constant
- String constant
- Both A and C
Answer – Option B
Q3. const char z = “Hello World!”;
Is this a valid Character constant example? Explain why?
- True
- False
Answer – Option B, because Character constants only include single letter, single alphabet, and single symbols.
Leave a Reply