Topic :Constants in C++ Programming languages: Using Preprocessor directive | Using Const Keyword

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.



It is illegal to assign a constant outside of its declaration statement, all constants must initialized where they are declared.


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;
}


#define is Preprocessor directive, not a C++ command. Do not put a semicolon at the end of its expression. if you did so, it will generate an error.


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;
}


Const is the keyword, so, it's a C++ statement. Here we need to put a semicolon at the end of its expression. if we forget to add semicolon at the end of its expression, it will generate an error.






I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement