C Constants

Introduction to C Constants

Constants वो variables होते है जिनकी value program execution के दौरान किसी प्रकार भी change नहीं होती है। जब भी आप कोई constant declare करते है तो program के execution के दौरान उसकी value fixed रहती है। यदि इसकी value change करने की कोशिश की जाती है तो program में error आ जाती है।

C language में constants दो types के होते है।

  • Constant Literals
  • Constant Variables

इन दोनों types के constants के बारे में निचे detail से बताया जा रहा है।

Constant Literals

Constant literals ऐसी values होती है जिन्हें आप program में directly use करते है। उदाहरण के लिए निचे दिए गए code को देखिये।

y = x+2;

ऊपर दिए गए statement में 2 एक constant literal है। इसे program में directly use किया गया है। इसे program के execution के दौरान change नहीं किया जा सकता है।

Constant literals को आप ऐसी direct values समझिये जिन्हें change नहीं किया जा सकता है। आम तौर पर literal constants को use करने की सलाह नहीं दी जाती है।

मान लीजिये आपने एक literal constant का प्रयोग program में कई जगह पर किया है, अब यदि आपको इस constant को change करने की आवश्यकता होती है तो आपको उसे manually ढूंढ कर program में हर जगह change करना होगा।

इसलिए literal constants का प्रयोग आपको कम से कम करना चाहिए।

Constant Variables

Constant variables को आप खुद variables की तरह declare करते है। Constant variables को प्रयोग करने का फायदा ये है की यदि आपको बाद में constant को change करना पड़े तो आपको इसे program में कई जगह पर change करने की आवश्यकता नहीं होती है आप सिर्फ constant variable की value change करते है और वह program में हर जगह अपने आप change हो जाती है।

Constant variables को आप दो प्रकार से declare कर सकते है। इनके बारे में निचे बताया जा रहा है।

Using #define Directive

#define एक pre processor directive है इसे use करके आप constant variables declare करते है। इस directive द्वारा constant variables program की शुरआत में main function से पूर्व ही declare किये जाते है। इस directive द्वारा define किये गए constant variables को आप program में कँही भी use कर सकते है।

Constant variables के उपयोग को निचे उदाहरण द्वारा समझाया जा रहा है।

# include <stdio.h>
/* Defining constant */

#define result 10
int main()
{
    int a=5, b=6;

    /* Wrong, (error) Value of constant result variable can not be changed. */
    result = a + b;  
    printf(“%d”, result);  
    return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

error : lvalue required as left operand of assignment            result = a + b;                     ^

Using const Keyword

C language में const keyword के द्वारा भी आप constant variables declare कर सकते है। यदि आप सिर्फ किसी function में ही constant variable का प्रयोग करना चाहते है तो इस keyword के द्वारा constant variable declare कर सकते है।

C language में const keyword के use को निचे उदाहरण द्वारा समझाया जा रहा है।

#include<stdio.h>
int main()
{
const int a=5;
const int b=6;
int c; /* Adding two constants */
c = a+b;
printf(“Result is : %d”,a);
return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Result is : 11

Types of C Constants

C language में निचे दिए जा रहे 5 type के constants होते है।

  1. Integer Constants
  2. Floating-Point Constants
  3. Character Constants
  4. String Constants
  5. Enumeration Constants

इनके अलावा C language में escape sequence characters को भी constants ही माना जाता है।