Constant Variables In C In Hindi

📔 : C 🔗

पिछले topic में आपने Variables के बारे में पढ़ा समझा , इस topic में आप constant variables के बारे में पढ़ेंगे।


C में Constants, Identifier होते हैं जिन्हे हम variables की तरह ही define करते हैं , हालाँकि normal variables की तरह इनकी value Immutable होती , means हम program execution के समय इनकी value को change नहीं कर सकते हैं। जैसे normal variable को Define करके हम Runtime पर भी increment / decrement या new value assign करके change करते थे।

CopyFullscreenClose FullscreenRun
#include <stdio.h>      
int main() {
  // we can change it as per the need.
  int age = 45;
  printf("Before change : %d", age);
  // now change the value.
  age = 25;
  printf("\nAfter change : %d", age);
  return 0;
}
Output
Before change : 45
After change : 25

Example में आप easily देख सकते हैं , कि normal variables की values को need के according program में कही भी change कर सकते हैं।

Understanding Identifier - identifier का use program में किसी entity को Identify करने के लिए किया जाता है, ये unique होते है, define करने के बाद हम इन्हे program में कही भी use कर सकते हैं।

C const

const एक predefined keyword है जिसका use करके , हम constant variables को define करते हैं।

const int age = 78;

well , आप बाकी normal variables की तरह ही हर data type के constant variable define कर सकते हैं।

C constant variable example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  const int age = 90;
  const double double_var = 15.99;
  const char letter_var = 'R';  
  printf("constant int variable : %d", age);
  printf("\nconstant double variable : %f", double_var);
  printf("\nconstant char variable : %d", letter_var);
  return 0;
}
Output
constant int variable : 90
constant double variable : 15.990000
constant char variable : 82

Example में \n का use new line के लिए किया गया है और %d या %f का use as variable output identifier के रूप में किया गया है , जिससे पता चल सके कि print होने वाले variable का type क्या है। हालाँकि इसके बारे में आप आगे पढ़ेंगे।


हालाँकि जैसा कि आप ऊपर पढ़ चुके हैं कि , इनकी value define हो जाने के बाद change नहीं हो सकती है, इसलिए जब आप new value assign करने का try करेंगे तो कुछ इस तरह से error आएगी।

const int age = 90;
  age = 80;

O/P : error: assignment of read-only variable 'age'

I Hope, अब आपको C++ में constant variables के बारे में अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers