What is Constant in C++ in Hindi - C++ में Constant क्या है

Constant, Variable, के समान एक Memory location है जहां Store value को बदला नहीं जा सकता
Variables, से भिन्न Constant की Value को Change या Modify नही किया जा सकता है।
 
दूसरे शब्दों में कहे तो ये Fixed Value रखने वाले एक Container हैं अतः Program में इस Container अर्थात Constants का Use उस स्थिति मे किया जाना चाहिए जब हम Value को Fixed रखना चाहते है।

जब कभी हम Program में Constants की Value को बदलने का प्रयास करते है हमे Compiler Error देता है

Define Constants in C++ in Hindi

C++ में Constant को Define करने के कई तरीके है जो निम्न है
1) const keyword का Use करके
2) #define Preprocessor का Use करके
3) 'constexpr' keyword का Use करके

const keyword का प्रयोग करके

C++ मे`const` Keyword का Use करके Constant को Define करने का यह तरीका मुख्य और ज्यादा Use की जाती हैं 'const' keyword का Use constant define करने के लिए निम्न प्रकार से किया जाता है
Program to define Constant by using const keyword 
#include<iostream> 
using namespace std;
int main() 
 {
    const int myNum = 45;
    const float PI = 3.14;

 cout << "Value of myNum:"<< myNum<<endl;
cout << "Value of PI is:"<< PI;
 return 0;
 } 

Output

ऊपर के Example में `myNum` और `PI` को `const' keyword का Use करके Constants declare किया गया है
हम जानते है कि जब Constants को Value Assign कर Initialize किया जाता है 

तब आप उसके value को modify नही कर सकते और यदि ऐसा करने का प्रयास करते है जैसे कि हमने PI की Value को Modify करने की कोशिश कि तो हमे Compilation error का सामना करना पड़ा

#define Preprocessor का प्रयोग कर

C++, में constants  को define करने के लिए आप #define Preprocessor का प्रयोग कर सकते है मान लीजिए आप अपने Program में myNum की Value 10 Set करना चाहते है तथा PI की 3.14159 तब इसे #define का Use करके निम्न प्रकार से कर सकते हैं
 
Program to define Constant by using #define keyword 
#include<iostream>
using namespace std;
#define myNum 10
#define PI 3.14
int main()
 {
cout<<"The value of myNum is:"<<myNum<<endl;
cout<<"The value of PI is:"<<PI<<endl;
return 0;
}

constexpr keyword का Use करके

constexpr keyword, const keyword के समान है और C++ में इसका Use Constant
को Declare करने में होता है परंतु दोनों में बड़ा अंतर है यह है कि 

constexpr constants को Compiler time में Initialize किया जाता है क्योंकि इसकी value को Compiler time में ही पता होना चाहिए जबकि const keyword का Use करके constants को compile time और runtime में Initialize किया जा सकता है

Program to define Constant by using constexpr keyword
#include<iostream>
using namespace std;
int main()
 {
int constexpr Num = 26;
cout<<"The value of Num is:"<<Num;
return 0;
 }
Output;
The value of Num is:26

C++ Language में Constant के प्रकार


Constant कई प्रकार के होते है जो निम्न है
1) Integer constants
2) Floating point constants as
3) Character constants
4) String constants

Integer Constant

Integer Constant एक Whole number है अर्थात इसमें Decimal Point नही होते  
इसमें Positive और Negative दोनो Numbers को शामिल किया जाता है

Types of Integer Constants

Integer Constant के निम्न प्रकार होते है
I) Decimal Integer Constant
II) Octal Integer Constant
III) Hexadecimal Constant

Decimal Integer Constant(Base 10)

एक Decimal Integer Constant को '0' से '9' तक कोई भी Digits के Combination से बनाया जाता है

Examples
const deConstant = 4567;
const int x = 674;

Octal Integer Constant(Base 8)


एक Octal Integer Constant का निर्माण (0 से 7) तक Digits के Combination का Use करके बनाते है लेकिन इसका पहला शुरुवाती Digit , '0' होना चाहिए ताकि उसे Octal Integer Constant के रूप मे पहचाना जा सके

Example
int octConstant = 0642; 
int x = 047"

Hexadecimal Integer Constant

एक Octal Integer Constant का निर्माण (0 से 9) तथा (A से F या a से f) तक के Letters(Uppercase या Lowercase) के Combination का Use करके बनाते है इसकी शुरुवात हमेशा 0x या 0X से होनी चाहिए

Example
int hexNum = 0x2A;
const int a = 0x2A6;


Program for all types of Integer(Decimal, Octal, Hexadecimal) Constant  
#include<iostream>
using namespace std;
int main() 
{
 //Decimal constant defined
const int decConstant = 802;

//Octal constant defined
const int octalConstant = 0732; 

//Hexadecimal constant defined
const int hexaConstant = 0x72C; 

//Printing all the Integer Constants

cout<<"Value of decimal constant is: "<<decConstant<<endl;

cout<<"Value of octal constant is: "<<octalConstant,<<endl;

cout<<"Value of Hexadecimal constant is: "<<hexaConstant;
    return 0;
} 
 
Output:
Value of decimal constant is: 802 Value of octal constant is: 474
Value of Hexadecimal constant is:1836

Floating Point Constant

इसमे Real Numbers को Store किया जाता है जिसमें Decimal Point या Fractions शामिल होते हैं साथ ही इसमें Positive और Negative Value होते है

Example
float a = 22.45;
float pi = 3.14;        


Program for Floating Point Constant 
#include<iostream>
using namespace std;
int main() 
{ 
//Defining Floating Point Constant
const float PI = 3.14;
const float x = 4.6;

cout<<"The value of PI is:"<< PI<<endl;
cout<<"The value of x is:"<< x;
return 0;
 }
Output: 
The value of PI is:3.14 
The value of x is:4.6

Character Constant

Character Constant को Single Character(letter, digit,symbol) से प्रदर्शित किया जाता है
इसे लिखने के लिए Single Quotation Mark का Use करते है

Example
char my_letter = 'A';
char x = 'C';
char y = '7';
char mySymbol = '#';

 
 
Program for Character Constant 
#include<iostream>
using namespace std;
int main() 
{ 
//Defining Character constant
const char myLetter = 'D';
const char digitChar = '7';

cout<<"Value of my-letter is:"<<myLetter<<endl;
cout<<"Value of digit-letter is:"<< digitChar;
return 0;
 }   
Output:
Value of myLetter is: D
Value of digitChar is : 9

String Constant

एक String Constant , Characters का एक Sequence होता है जिसे Double quotation mark के अंदर लिखा जाता है
Example
const myStr = "xyz";
const myNum = "1234";
const mySymbol = "$$$$";


Program for String Constant  
#include<iostream>
using namespace std;
int main() 
{ 
/Defining string constant
char myStr1[ ] ="Welcome to Computerehub";

char myStr2[ ] ="Here, we are learning string constant in C++";

cout<<myStr1<<endl;
cout<<myStr2;
return 0;
 }

Output:

Welcome to Computerehub
Here, we are learning string constant in C++

Difference between Variable and Constant in Hindi in C++ Language



 Related Posts

> C++ क्या है? उसके इतिहास, गुण, उपयोग, फायदे और नुकसान 

> Basic structure of C++ Program 

> C++ में Tokens क्या है? और उसके प्रकार

> C++ Variables क्या है?, उसके प्रकार, उसे कैसे Declare, Define करते हैं

> C++ में Basic Input और Output (cin,cout,cerr) की जानकारी

> Data type in C++ की संपूर्ण जानकारी 

> C+ में Operators और उसके प्रकार जानें Practical सहित 

> C++ में Conditional और उसके प्रकारों को जानें Practical सहित 

> C++ में Looping statements और उसके प्रकार Practical सहित 

> C++ में Jump Statements और उसके प्रकारों की संपूर्ण जानकारी Practical सहित

> C++ में Array क्या है? और उसके प्रकारों की जानकारी Practical सहित

> C++ में Function क्या है उसके प्रकार, उपयोग प्रोग्राम सहित 

> C++ में Structure क्या है Practical सहित

> OOPs Concepts in C++ in Hindi- C++ में OOPs के बारे में

> Oops के फायदे और नुकसान की जानकारी 

> OOP और POP के बीच अंतर 

> C++ में Class और Object की सम्पूर्ण जनकारी

> C++ में Array of Objects क्या है? 

> C++ में Pointers, Pointer to an objects, Pointer to an Array की संपूर्ण जानकारी हिंदी में।

C++ में Passing objects क्या है

> C++ में Reference और Type Casting की संपूर्ण जानकारी

> C++ में Access specifier की संपूर्ण जानकारी 

> C++ में Static Data Members और Member Functions के बारे में Practical सहित 

> C++ में Memory allocation और Memory management operators (new और delete) Practical सहित 

> Friend Function in C++ in Hindi 

> Friend Class in C++ in Hindi Practical सहित 

> Inline function in C++ in Hindi 

> Function Overloading in C++ in Hindi Practical सहित 

> Operator Overloading in C++ in Hindi Practical सहित 

> C++ में Constructor क्या है और उसके प्रकारों की संपूर्ण जानकारी

> C++ में Destructor क्या है ?उसकी संपूर्ण जानकारी

> C++ मे Inheritance क्या है उसके प्रकारों को जानें प्रोग्राम सहित

> C++ में Polymorphism क्या है? और उसके प्रकारों को जानें

> C++ में Virtual function की संपूर्ण जानकारी

> C++ में File handling की संपूर्ण जानकारी 

> C++ में Exception handling  की संपूर्ण जानकारी