C++ Constant

Constants
A value which is hard coded into a program, which remains unchanged through out the program. Constants are of five (5) types:
  1. charconstant:                                        Character constant
  2. int constant:                                          Integer constant
  3. float constant:                                       Single precision floating point constant
  4. double constant                                     Double precision floating point constant
  5. String constant
Note: C++ does not support constant of the type void.
A Literal Constant is a value typed directly  into your program wherever it is needed.
Examples of C++ Constants are given below:
Data   Type
Constants
char
‘A’,’B’,’C’,…,’X’,’Y’,’Z’,’a’,’b’,’c’,…,’x’,’y’,’z’
‘0’,’1?,…,’8?,’9?,’!’,’@’,’#’,’%’,’^’,’&’,’*’,’+’,…,’?’
int
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, …,   2147483647
0, -1, -2, -3, -4, -5, -6, -7, -8, -9, …,   -2147483648
float
double
0.0, 15.75, 96.625, 1.25, 28.575, 2.3333,   0.69, 10.0
-25.8, -0.72, -154.85, -5.0, -759.625,   -89.025, -7.2
string
“Amit”, “Pizza”,   “India”, “Apple”, “G”, “9”, “$”,   “e”
“GH-14/12”, “23981535”,   “20/10/2005”, “***”, “6.0”
Back Slash Character Constant (Escape sequence)
In C++ a character constant is any single character from ASCII character set enclosed within a pair of single quote (‘). But there are few special type character constant which needs two characters within a pair of single quote (‘). All these special character constant starts with back slash hence they called Back Slash Constant. Back slash constant are know as Escape Sequence. List of back slash constant are given below:
‘\a’        Alert character or Bell character. Bell character makes a beep sound. ASCII code of Bell character is 7.
‘\b’        Back space character. Back space character moves the cursor one column to the left. ASCII code of Back space character is 8.
‘\n’        New line character. New line character shifts the cursor to the beginning of next line (an alternative for endl). ASCII code of Back space is character is 10.
 ‘\t’        Tab character. Tab character makes the cursor jump 8 columns to the right. ASCII code of Back space is character is 9.
These four back slash constants are also white space characters also, meaning these four back slash constant are not visible on the screen. All these four back slash constant can be embedded in a string and can be used with string also. Examples of back slash constants are given below:
void main()
{
cout<<‘\a'<<‘\n’;
cout<<“Simplifying”<<‘\b'<<“ICT\n”;
cout<<“Welcome\t”<<“to\t”<<“INDIA”;
}
Running of the program produces following output
SimplifyinICT
Welcome   to     INDIA
Explanation of output
  • First cout Bell character (‘\a’) makes a beep sound and New line character (‘\n’) shifts the cursor to the beginning of the next line and hence first row of output is a blank line.
  • Second cout displays Simplifying on the screen and then Back space character (‘\b’) takes the cursor back one column to left. Therefore I of  ICT replaces g of Simplifying line character takes the cursor to the beginning of the next line.
  • Third cout display Welcome, Tab character (‘\t’) takes cursor to the 9th column and displays TO. Next Tab character takes cursor to the 17th columns and displays INDIA.
User Defined Constant (Named Constant or ReadOnly Identifier)
A variable created in C++ program are also know as ReadWrite identifier because value can be stored in a variable (Write) and value stored in variable can be retrieved (Read) for display or for further calculation. An example is given below:
Variables x1 and x2 are created with     initial values 5 and 7.Value stored in variables x1     and x2 are displayed. New values are inputted in     variables x1 and x2.     New values stored in variables x1 and x2 are displayed. New variable prod     is created storing product of x1 and x2. Value stored in prod is displayed.
int x1=5, x2=7;
cout<<x1<<x2<<endl;
cin>>x1>>x2;
cout<<x1<<x2;
int prod=x1*x1;
cout<<prod;
Sometimes in a C++ program we need to create an identifier whose value can only be read and its value cannot be updated. To do that we prefix keyword const before the data type, it then creates a ReadOnly identifier, an identifier whose value cannot be altered in a program. ReadOnly identifiers are also known as User Defined Constant or Named Constant.
Rule:     const DataType ConstantName=ConstantValue;
Examples of User Defined Constants are given below:
const char BELL=’\a’;
const int MAX=20;
const double PI=3.14159;
// Character     constant BELL     stores Bell     character.
// Integer constant MAX represents     value 20.
// Floating point constant PI has a value 3.14159.
Normally User Defined Constant names are written in uppercase but that’s just the convention. If we try to update value stored in User Defined Constant, compiler flags an error. Examples are given below:
const int MAX=20;
const double PI=3.14159;
PI=22.0/7;
cin>>MAX;
Following statements will  flag syntax errors:
PI=22.0/7;
cin>>MAX;
Because the codes updates     value stored in constant identifier.
If data type omitted while creating a User Defined Constant, then by default User Defined Constant becomes an int type. Examples are given below to illustrate the concept:
const MAX=20;
const PI=3.14159;
cout<<MAX<<endl;
cout<<PI<<endl;
Program     segment displays 20 and 3. Since integer part of 3.14159     is stored in PI, that is, 3 is     stored in PI.
Generally a User Defined Constants are created as a Global identifier, so that the User Defined Constant can be used through out the program. But it is syntactically correct to create a User Defined Constant which is Local to block and in that case the constant identifier can be used inside the block and the block nested below. An example is given below:
void area(double rad)
{
double ca=PI*rad*rad;
double sa=4*PI*rad*rad;
cout<<“Circle Area =”<<ca<<endl;
cout<<“Surface Area=”<<sa<<endl;
}
void main()
{
const double PI=3.14159;
double radius;
cout<<“Input Radius? “; cin>>radius;
double cir=2*PI*radius;
cout<<“Circumference=”<<cir<<endl;
area(radius);
}
Compiler     flags syntax errors in
PI*rad*rad
4*PI*rad*rad
Because     constant PI is a local constant created in the main() function.

Macro

Macro is an identifier created by using Compiler Directive #define. Macro identifier represents replacement text. A replacement text either represents replacement text (symbolic constant or single line / multi-line code. A programmer can use a Macro identifier instead of User Defined Constant identifier or a programmer can use a Macro in place of user defined function.
Rule for creating Macro as Symbolic Constant:
#define MacroName ReplacementText
MacroName:                           C++ identifier name
ReplacementText:   Integer / Character / Floating point / String constant
Usage
#define PI 3.14159
#define SIZE 20
#define is a compiler directive. It tells the compiler that it should replace Macro Identifier Name with Replacement Text (Symbolic Constant Name). A Complete program is given below showing the usage of Macro as a Replacement Text.
#include<iostream.h>
#define PI 3.14159
void main()
{
double rad;
cout<<“Input Radius? “; cin>>rad;
double area=PI*rad*rad;
double volume=4.0/3*PI*rad*rad*rad;
double sarea=4*PI*rad*rad;
cout<<“Area of Circle  =”<<area<<endl;
cout<<“Volume of Sphere=”<<volume<<endl;
cout<<“Surface Area    =”<<sarea<<endl;
}
When C++ compiler compiles the program, every occurrence of PI is replaced by 3.14159 (Replacement Text / Symbolic Constant). Running of the program produces following output:
Input Radius? 7.0
Area of Circle  =153.938
Volume of Sphere=1436.75
Surface Area    = 615.752
Differences between Macro Identifier and Constant Identifier are given below:
Macro Identifier Constant Identifier
Macro Identifier has no data type
No memory is allocated to a Macro Identifier
Compiled code does not contains macro Identifier name
Constant Identifier has a data type
Memory is allocated to a Constant Identifier
Compiled code contains Constant Identifier name

 

Next: C++ Operators