 # Constants in Programming Language 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

1. 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
1. 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.

1. Decimal integers – Set of digits ranging from 0 to 9. For Example – const int y = 123;
2. 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;
3. 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;

### 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”

1. Immutability – It is useful if you know that the value of the variable will not change throughout your program lifecycle.
2. 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.
3. 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?

1. const int x = 34;
2. int const x = 34;
3. int constant x =34;
4. None

Q2. Which of the following is a primary constant?

1. Integer constant
2. Numeric constant
3. String constant
4. Both A and C

Q3. const char z = “Hello World!”;

Is this a valid Character constant example? Explain why?

1. True
2. False

Answer – Option B, because Character constants only include single letter, single alphabet, and single symbols.

Share with friends  Browse
##### Data Types, Variables and Constants
Customize your course in 30 seconds

Which class are you in?

5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.  Ashhar Firdausi
IIT Roorkee
Biology  Dr. Nazma Shaik
VTU
Chemistry  Gaurav Tiwari
APJAKTU
Physics
Get Started
Browse
##### Data Types, Variables and Constants
Customize your course in 30 seconds

Which class are you in?

No thanks.