Introduction
C Language में
constants एक variable ही होते है बस constants variable और normal variable में difference सिर्फ इतना रहता है की
constants variable की value program में
change नहीं कर सकते है। अगर आप constants variable की value को change या overwrite करते है तो error आएगी। तो चलिए जानते है
Constants and Literals In Hindi
C Language में
constants दो types के होते है
- Constants Literals
- Constants Variables
Constants Literals
Constants Literals में आप value को directly use करते है example के लिए नीचे दिए गए program को देखिये
#include<stdio.h>
main()
{
int a = 2;
int b = a+3;
}
ऊपर दिए गए program में
int b = a+3 में 3 को आप directly use कर रहे है जो की एक Constants Literals है आप इसकी value को program में change नहीं कर सकते और काफी programmers
constants literals को न use करने की सलाह देते है उसका reason ये है की मान लीजिये आप एक बड़ा program बना रहे है और उस program में अपने
literals constants को काफी जगह use किया है तो अगर आपको उसकी value change करनी पड़े तो आप उस
constants literals को search करके manually change करना पड़ेगा। इसलिए हो सके तो literals constants को program में ज्यादा use न करे।
Constants Variables
Constants Variables ऐसे variable होते है जिनको अगर आपने एक बार declare कर दिया तो फिर उसके बाद आप इनकी value को change नहीं कर सकते। इन variable को use करने का फायदा ये है की अगर आपको इनकी value को change करना है तो आपको सिर्फ एक बार ही इन variable की value को change करना पड़ेगा बार बार हर जगह change करने की जरुरत नहीं पड़ती और अगर आप इन variable की value को overwrite करते है तो आपको error मिलेगी।
Constants Variable को दो तरीको से बनाया जा सकता है
- #define directive
- const keyword
Declare Constant Variable Using #define
#define एक processor directive है इसका use करके आप
constant variable को declare कर सकते है। declare करने के लिए आपको
main function के पहले
#define के साथ variable का नाम लिख के declare किया जाता है। अगर आपको ऐसी value पूरे program में use करनी है तो आप #define के साथ variable को एक बार declare कर दीजिये और उसके बाद पूरे program में कहीं भी use करिये।
# include <stdio.h>
#define define 5
main()
{
printf("%d", define);
}
Declare Constant Variable Using const Keyword
constant variable को
const keyword का use करके declare किया जा सकता है इसके लिए आपको variable के name के आगे
const keyword लिखना लिखना होता है। अगर आपको किसी particular block या function में constants variable को use करना है तो आप const keyword के साथ use कर सकते है।
# include <stdio.h>
main()
{
const int a = 5;
const int b = 6;
const int c = a+b;
printf("%d",c);
}