What is Constants?
A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change its value. we must initialize a constant when it is created. Just like The speed of the light, Avogadro's number is scientific constants, that is, to the degree of precision to which they have been measured and/or calculated, they do not vary. C++ supports named constants. we can use constants with the addition of Const keyword as shown in the given example.
const double PI = 3.14159;
Once declared and initialized, a constant can be used like a variable in all but one way. a constant may not be reassigned. it is illegal for a constant to appear on the left side of the assignment operator (=) outside its declaration statement as shown in the given example.
PI = 3.41; /*Not allowed in C++.*/
this statement would cause the compiler to issue an error message, and fail to compile the program.
Using #define Preprocessor.
The #define preprocessor directive is used in C++ Programming, although not nearly as frequently as it is in C. Due to the const Keyword (in C++) that enables us to define variables as constants. #define preprocessor in not used as much in C++. Nevertheless, #define is useful for compatibility to C Programs we are converting to C++. We can use #define preprocessor using the following syntax:
/* #define Variable-name value*/
#define RollNO 12;
#define NAME "InfoBrother";
#define ADDRESS "Abbottabad Pakistan";
#define Rate 25.60;
In Above Example, Variable-name is an single variable Name containing no spaces. We can choose any variable name using the same naming rules that we discuss in our variable tutorial. It is an traditional to use uppercase letters. at least one space separates VARIABLE from value. The value can be any character, word, or Number, it can also contain space or anything else we can type on the keyboard. #define is Preprocessor directive and not a C++ command. Do not put a semicolon at the end of its expression.
The #define Preprocessor directive replaces the occurrence of VARIABLES everywhere in our Program with the contents of Value. as shown in the given example:
#include <iostream>
using namespace std;
#define GREETING "Welcome to InfoBrother"
#define NAME "Omar"
#define AGE 22
#define NEWLINE '\n' // \n is for newline.
int main()
{
cout<<GREETING<<NEWLINE;
cout<<"This is "<<NAME;
cout<<" And i am "<<AGE<<" Year Old:";
return 0;
}
Using #define Preprocessor.
Welcome to InfoBrother
This is Omar And i am 22 Year Old:
Using Const Keyword.
By using const keyword we can do same thing as with using #define. but here we need data type as well as semicolon at the end of statement, as shown in the given example:
#include <iostream>
using namespace std;
const string GREETING = "Welcome to InfoBrother";
const string NAME = "Omar";
const int AGE = 22;
const char NEWLINE ='\n'; // \n is for newline.
int main()
{
cout<<GREETING<<NEWLINE;
cout<<"This is "<<NAME;
cout<<" And i am "<<AGE<<" Year Old:";
return 0;
}
Using Const Keyword.
Welcome to InfoBrother
This is Omar And i am 22 Year Old: