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

Constant भी variable के समान ही value store करने वाला container है लेकिन variable के विपरीत constant मे store value को program execution के दौरान बदला नही जा सकता।

दूसरे शब्दों में कहे तो ये fixed value रखते हैं। अतः Constant का उपयोग उस समय किया जाता है जब हम value को बदलना नहीं जाते।

यदि जब हम इसके Value को बदलने करने का प्रयास करते है तब हमे Error आता है

Example:

const int a = 10;

C मे Constant Define करने का तरीका

C में दो तरीके से Constant Define किए जाते है।
1) const keyword
2) #define Preprocessor का प्रयोग कर

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

C मे Constant Define करने के लिए const keyword का प्रयोग निम्न प्रकार से किया जाता है।
const float PI = 3.14;

ऊपर हम PI की Value को Change नही कर सकते क्योंकि वह एक Constant बन चुका है।

Program to define Constant by using const keyword 
#include<stdio.h>
int main()
 {
// Defining constant 
 const float PI = 3.14;
 printf("The value of Pi is: %f", PI);
return 0;
 }
 
Output:
The value of Pi is: 3.14

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

आप #define Preprocessor का प्रयोग करके भी निम्न प्रकार से Constant Define कर सकते है।
#define PI 3.14

Program to define constant by using #define Preprocessor 
#include<stdio.h>
#define PI 3.14
int main()
 {
 printf("The value of Pi is: %f", PI);
return 0;
 } 
Output:
The value of Pi is: 3.14

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

Constant कई प्रकार के होते है जो निम्न है।
1) Numeric Constant
2) Character Constant
3) String Constant
4) Backslash Character Constant

Numeric Constant

Numeric Constant में Numbers शामिल होते है
इसे आगे निम्न भागों में बांटा गया है
a) Integer Constant
b) Floating Point Constant

Integer Constant

Integer Constant में बिना Decimal Point या Fractions के Whole Number होते है
इसमे Positive और Negative Numbers शामिल है।

Example:

const int x = 24;
const int y = -45;
const int z = 849;

Integer Constant के प्रकार

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

Decimal Integer Constant(Base 10)

एक Decimal Integer Constant का निर्माण (0 से 9) Digits को मिलाकर किया जाता है।

Examples:

const decNum x = 4567;
const int y = 123;

Octal Integer Constant(Base 8)

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

Example:

int octalNum = 052; 
int a = 047"

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

Example:

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

Program for all types of Integer(Decimal, Octal, Hexadecimal) Constant
#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;
} 
Output:
Value of decimal constant is : 62
Value of octal constant is : 58
Value of hexadecimal constant is :
 1611

Floating Point Constant

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

Example:

float a = 27.55;
float pi = 3.14;        

Program for Floating Point Constant 
#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;
 }
 
Output:
The value of PI is:3.140000
The value of x is:5.600000

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<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;
 }
Output:
Value of my-letter is: A
Value of show-letter is: d
Value of digit-letter is : 9

String Constant

एक String Constant , Characters का एक Sequence होता है जिसे Double quotation mark के अंदर लिखा जाता है।

Example:

const my_string = "abcde";
const my_num = "4567";
const my_symbol = "@@@@";

Program for String Constant
#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;
 }
 
Output:
I'm using string constant
Hey, I'm learning c

Backslash Character Constant

C Language में कुछ Character को '\' Backslash के साथ प्रयोग किया जाता  है, जिसका प्रयोग हम Program के Instruction लिखने के समय करते है।

Backslash के साथ English के Lower Case Letters (a to z) का Use किया जाता है।

Backslash Meaning 


 '\n' Newline
'\t' Horizontal Tab
'\r' Carriage return
'\b' Backspace
'\a' Audible Alert (bell)
'\f' Form feed
'\\' Backslash 
 '\' Single quote
 '\" ' Double quote
'\v' Vertical Tab
'\?' Question Mark

अन्य प्रकार के Constants 

उपरोक्त Constants के अतिरिक्त कुछ अन्य Constants निम्न है

Array Constant

Array एक ही प्रकार के Data Types वाले Elements का संकलन है
वास्तव में Array स्वयं एक Constant नही है लेकिन हम Arrays में const Keyword का Use करके Array के लिए constant Pointer का निर्माण कर सकते हैं ।

Example:

const int marks[5] = {56, 98, 67, 40, 66};    

Pointer Constant

C में Pointers Variables होते है जो memory addresses को Store करते है हम const keyword का Use करके निम्न प्रकार से constant pointers का निर्माण कर सकते है

 Example:
   
     int a = 64;
  const int *ptr = &a;
  
     int b = 87;
     int *const ptr = &b;

Enum

C language में एक enumeration (enum) एक User-defined data type है जो Integer Value के एक Set को Represent करते है इसे बनाने के लिए enum keyword का प्रयोग किया जाता है

Example:

enum Fruits{Apple, Grapes, Pear, Banana, Orange};
 
ऊपर Example मे Fruits एक enum प्रकार है तथा Apple, Grapes, Pear, Banana, Orange enum constants है जिसके लिए

Default Integer Value जो 0 से शुरू होती है Assign किया जाता है 
जैसे Apple के लिए Assigned Value 0 है, Grapes के लिए 1, Pear के लिए 2 , Banana के लिए 3 और Orange के लिए 4 है।

हम enum Constant के लिए आवश्यकतानुसार कोई अन्य विशेष Value Assign कर सकते है।

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.14
const float PI = 3.14;
  

Important of Constant in C in Hindi - कांस्टेंट का महत्व 

  1. Fixed value(स्थिर मान):  एक constant, की value प्रोग्राम एग्जीक्यूशन के दौरान कभी नहीं बदलते है। 
  2. Safe code(सुरक्षित कोड): यह values में होने वाले अचानक बदलाव को रोकता है। 
  3. Better understanding(बेहतर समझ): प्रोग्राम को पढ़ना और समझना आसान हो जाता है। 
  4. Easy maintenance(आसान रखरखाव)यदि वैल्यू बदलता है तो आपको केवल एक ही बार अपडेट करना होता है। 
  5. 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 में इसका उपयोग किया जाता है।