Constant भी variable के समान ही value store करने वाला container है लेकिन variable के विपरीत constant मे store value को program execution के दौरान बदला नही जा सकता।
दूसरे शब्दों में कहे तो ये fixed value रखते हैं। अतः Constant का उपयोग उस समय किया जाता है जब हम value को बदलना नहीं जाते।
यदि जब हम इसके Value को बदलने करने का प्रयास करते है तब हमे Error आता है।
Example:
C मे Constant Define करने का तरीका
const keyword का प्रयोग करके
C मे Constant Define करने के लिए const keyword का प्रयोग निम्न प्रकार से किया जाता है।
const float PI = 3.14;
ऊपर हम PI की Value को Change नही कर सकते क्योंकि वह एक Constant बन चुका है।
#include<stdio.h>int main() { // Defining constant const float PI = 3.14; printf("The value of Pi is: %f", PI); return 0; }
#define Preprocessor का प्रयोग कर
#include<stdio.h>#define PI 3.14 int main() { printf("The value of Pi is: %f", PI); return 0; }
C Language मे Constant के प्रकार
Numeric Constant
Integer Constant
Example:
Integer Constant के प्रकार
Examples:
Example:
Example:
#include<stdio.h>
int main()
{
//Decimal constant defined
const int decimal_constant = 62;
//Octal constant defined
const int octal_constant = 072;
//Hexadecimal constant defined
const int hexa_constant = 0x64B;
printf("Value of decimal constant is : %d\n", decimal_constant);
printf("Value of octal constant is : %d\n", octal_constant);
printf("Value of hexadecimal constant is : %d\n", hexa_constant);
return 0;
}
Floating Point Constant
Example:
#include<stdio.h>int main() { //Defining Floating Point Constant const float PI = 3.14; const float x = 5.6; printf("The value of PI is:%f\n", PI); printf("The value of x is:%f", x); return 0; }
Character Constant
Example:
#include<studio.h>
int main()
{
//Defining Character constant
const char my_letter = 'A';
const char show_letter = 'd';
const char digit_char = '9';
printf("Value of my-letter
is:%c\n", my_letter);
printf("Value of show-letter is:%c\n", show_letter);
printf("Value of digit-letter is:%c\n", digit_char);
return 0;
}
String Constant
Example:
#include<stdio.h>int main() { //Defining string constant char my_string1[ ] ="I'm using string constant"; char my_string2[ ] ="Hey, I'm learning c"; printf("%s\n",my_string1); printf("%s\n",my_string2); return 0; }
Backslash Character Constant
C Language में कुछ Character को '\' Backslash के साथ प्रयोग किया जाता है, जिसका प्रयोग हम Program के Instruction लिखने के समय करते है।
Backslash के साथ English के Lower Case Letters (a to z) का Use किया जाता है।
Backslash Meaning
अन्य प्रकार के Constants
Array Constant
Example:
Pointer Constant
Enum
Example:
Difference between Variable and Constant in Hindi in C Language
| Variable | Constant |
|---|---|
| कंप्यूटर की storage location होती है, जिसकी value program चलने के दौरान बदल सकती है। | एक fixed value होती है जिसे बदल नहीं सकते। |
| Declare करने के लिए Data Type का उपयोग किया जाता है। | Declare करने के लिए const या #define keyword का उपयोग होता है। |
| Declare करते समय या बाद में value assign करके initialize किया जाता है। | Declare करते समय ही value assign करनी होती है। |
| Letters या symbols का उपयोग करके बनाया जाता है। | Numbers या fixed value का उपयोग किया जाता है। |
Examples:int a = 19;float marks = 67.45;
|
Examples:#define PI 3.14const float PI = 3.14;
|
Important of Constant in C in Hindi - कांस्टेंट का महत्व
- Fixed value(स्थिर मान): एक constant, की value प्रोग्राम एग्जीक्यूशन के दौरान कभी नहीं बदलते है।
- Safe code(सुरक्षित कोड): यह values में होने वाले अचानक बदलाव को रोकता है।
- Better understanding(बेहतर समझ): प्रोग्राम को पढ़ना और समझना आसान हो जाता है।
- Easy maintenance(आसान रखरखाव): यदि वैल्यू बदलता है तो आपको केवल एक ही बार अपडेट करना होता है।
- Error reduction(त्रुटियों में कमी): यह logical errors होने की संभावना को कम करता है।
Use of Constant in C in Hindi - कांस्टेंट के उपयोग
- fixed values को परिभाषित करने के लिए जैसे `PI = 3.14` या `MAX = 100`
- कोड को पढ़ने योग्य बनाने के लिए उदाहरण के लिए `60 * 60`, लिखने के बजाए आप `SECONDS_IN_HOUR` लिख सकते है।
- सुरक्षा में सुधार आता है ताकि गलती से भी महत्वपूर्ण वैल्यू को बदला न जा सके।
- macros और `#define` statements में इसका उपयोग किया जाता है।
0 टिप्पणियाँ