किसी भी programming language में variable data को store करने के लिए use किया जाता है। variable computer memory में data को store करने के लिए space बनाता है जिसमे आप किसी भी value को store कर सकते है और बाद में अपने जरुरत के हिसाब से उसे value को variable के name से access कर सकते है। तो चलिए जान लेते है की कैसे create करते है variable in c
int a;
float b;
char c;
ये example है variable को create करने के लिए। इसमें a, b, c variable के नाम है और int , float ,char data type है। data type के बारे में हम लोग next post में जानेंगे।
अब अगर आपको इन variable में value store करनी है तो इसके दो तरीके है पहला ये की आप variable declaration के time आप उसमे value store कर दीजिये।
int a = 12 ;
float b = 4 .5 ;
char c = 'A ';
और दूसरा ये की बाद में value को variable में store करिये।
#include <stdio.h>
int main() {
int a;
float b;
char c;
a=15;
b=4.12;
c= 'A';
}
Rules For Creating Variable In C Language
C Language में variable को create करने के लिए कुछ rule है और आपको इन rule को follow करके variable को create करना है जिससे की आपको कोई error न आये।
एक variable में alphabets , digit यानी की number और underscore हो सकता है.
C Language में variable alphabets ,underscore से start होता है।
variable में white space नहीं होना चाहिए।
variable create करते time ये याद रखना है की variable का नाम कोई keywords न हो जैसे की int ,float ,char etc.
Types Of Variable In C Language
C language में variable के बहुत types होते है जिनका अपना एक अलग मतलब होता है तो आइये जानते है की variable कितने types के होते है।
Local Variable
Global variable
Static Variable
Automatic Variable
External Variable
Local Variable
जो variable किसी किसी block या function के अंदर बनाये जाते है उसे हम local variable बोलते है मतलब उनका scope या ये बोले उनका इस्तेमाल हम उसी function और block के अंदर कर सकते है जिस function और block के अंदर वो define किये गए है उसके बाहर हम उन variable का इस्तेमाल नहीं कर सकते। जैसे की control statements,function आदि में अगर आप कोई variable create करते है तो आप इस variable का इस्तेमाल सिर्फ उसी के अंदर कर सकते है बहार उसका इस्तेमाल नहीं कर सकते है।
#include<stdio.h>
void fun();
main()
{
int a=20; //local variable
printf("%d\n",a);
fun();
}
void fun()
{
int a=10;
printf("%d",a);
}
ऊपर वाले program में आप देख सकते है की मैंने a नाम के दो variable create किया है एक fun नाम के function में और दूसरा main function में तो जब से run करेंगे तो आपके सामने ये result आएगा जैसा की आप देख सकते है नीचे
Learn C Language In Hindi – Variable In C
तो आप देख सकते है एक ही नाम के दो variable है लेकिन उनकी value अलग अलग है तो इसे ही local variable कहते है।
Global Variable
जो variable किसी function या block के बहार define किये जाते है उन्हें हम Global Variable बोलते है। इन variable का उसे हम program के किसी भी function में कर सकते है।
#include<stdio.h>
int a=2; //global variable
void fun();
int main()
{
fun();
}
void fun()
{
printf("%d",a);
}
ऊपर के program में आप देख सकते है की a variable को हम किसी भी function के अंदर इस्तेमाल कर सकते है। जब इस program को आप अपने compiler पे run कराएँगे तो result 2 आएगा तो इस तरह से हम global variable को declare करते है। अब global variable का इस्तेमाल वहां पे कर सकते है जहाँ हमे एक ऐसी value चाहिए जिसे हम पूरे program में इस्तेमाल करना है तो ऐसी setuation में हम global variable का इस्तेमाल कर सकते है।
Static Variable
ऐसे variable जो static keyword के साथ declare किये जाते है उन्हें हम static variable बोलते है। ऐसे variable अपनी value को हमेशा रोक के रखते है इसे example से समझते है।
#include<stdio.h>
void myFun();
main()
{
myFun();
myFun();
}
void myFun()
{
int a = 10;
static int b = 10; //static variable
a = a+1;
b = b+1;
printf("%d %d\n",a,b);
}
ऊपर लिखा हुए program को run करिये तो जैसे ही आप इस program को run करेंगे तो आपके सामने ये result show होगा जैसा की नीचे आपको दिख रहा है।
Learn C Language In Hindi – Variable In C
तो इस program में मैंने main function में myFun नाम function को दो बार call किया है पहली बार call करने पे a और b की value 11 11 print होती है उसके बाद दूसरी बार फिर वही function call होता है तो a की value 11 ही रहती है लेकिन b variable की value 12 हो जाती है तो इससे ये पता चलता है की static variable अपनी value को hold करके रखता है मतलब अगर एक बार static variable में value increment या decrement कर दिया तो वही value वो store कर लेता है।
Automatic Variable
automatic को हम local variable भी बोलते है difference बस इतना है की इस variable को auto keywords के साथ किसी block या function में लिखा जाता है।
#include<stdio.h>
main()
{
auto int a; //automatic variable
int b;
}
External Variable
external variable को global variable भी बोलते है क्यूंकि इस variable को हम किसी दूसरी फाइल में भी access कर सकते है बस difference इतना की की global variable को हम सबसे ऊपर लिखते है बिना किसी keyword के और external variable को extern keywords के साथ लिखा जाता है।
#include<stdio.h>
main()
{
extern int a = 5; //external variable
}
अब बात आती है की इस external variable को हम दूसरी किसी file में कैसे access कर सकते है तो इसके लिए आपको दूसरी फाइल में जाके सबसे ऊपर जहाँ पर आप header file include करते हो वही पर आपको इस फाइल को भी include करना पड़ेगा उसके बाद आप इस फाइल में external variable को access कर सकते है जिसे आप global variable भी बोल सकते हो। लेकिन ये ध्यान में रखियेगा की आपको file .h extension से save करनी है जिस भी file में आप external variable बना रहे हो।
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.