Constant in C++
0 4161
Constant can be referred to as an entity that doesn't change throughout the program.
There are different types of constant available in C++ i.e. integer constant (whole number), floating-point constant (number with decimal values), a character constant.
Example: Integer Constant = 37, 45 Floating Point Constant = 46.7, 57.8 Character Constant = This type of constant is written in single quotes i.e. 'a', 'b' String Constant = This type of constant is written in double-quotes i.e. "Anita", "Anmol"
In C++, There are two simpler techniques can be used to define constants.
Example: Integer Constant = 37, 45 Floating Point Constant = 46.7, 57.8 Character Constant = This type of constant is written in single quotes i.e. 'a', 'b' String Constant = This type of constant is written in double-quotes i.e. "Anita", "Anmol"
In C++, There are two simpler techniques can be used to define constants.
- Using #definepreprocessor
- Using the const keyword
- #definepreprocessor
#include <iostream>Result: 32
using namespace std;
#define LENGTH 4
#define WIDTH 8
#define NEWLINE '\n'
int main() {
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
2) Using the const keyword
The const prefix can be used to declare constants. Syntax:const type variable = value; const int size = 4;
Share:




Comments
Waiting for your comments