Variables in C | W4 learn in Hindi

Variables क्या होते है

Variables in C


दोस्तों आज हम  " learnw4 के  C series" के tutorial में सीखेंगे कि Variables क्या होता है  
तो चलिये शुरू करते है   W4 learn in hindi के साथ,

Variables in C


यदि आप किसी data के साथ operations perform करना चाहते है तो इसके लिए सबसे पहले आपको उसे computer की memory में store करना पड़ता है। Computer memory में data को store करने के लिए variables का प्रयोग किया जाता है।

एक variable memory में किसी location का नाम होता है। यह नाम (या variable) उस memory location को computer की संपूर्ण memory में uniquely identify करने के लिए प्रयोग किया जाता है और इसी नाम के द्वारा आप उस memory location में data store करते है और उस data को पुनः प्राप्त करते है। Variables की values changeable होती है। आप एक value को हटाकर दूसरी value डाल सकते है। ऐसा आप compile time पर भी कर सकते है और dynamically (program execution के दौरान) भी कर सकते है।और इसे  multiple time use कर सकते है

Rules for naming C variable:


1) एक variable name में Capital letters A-Z, lowercase letters a-z, digits 0-9
और underscore character हो सकते है

2) पहला character letter या  underscore होना चाहिए

3)Blank spaces  को variable names में उपयोग नहीं किया जा सकता है।

5)  #, $ जैसे Special characters allow नहीं है।

6) C keywords को variable names के रूप में उपयोग नहीं किया जा सकता है।

7) Variable names case sensitive होने चाहिए

8) variable की values numeric or alphabetic हो सकती है

9) Variable type char, int, float, double or void हो सकते हैं


Creating C Variables
 

एक variable create करने के लिए सबसे पहले आप data type define करते है। इसके बाद आप एक unique नाम define करते है।

syntax:


type variable_list;

उदाहरण के लिए नीचे दिए गए statement को देखिये। 

/* Variable declaration with value assignment */
int age = 10;   

इस statement के द्वारा एक integer variable create किया गया है, जिसका नाम age है और इस variable को 10 value assign की गई है। आइये अब समझते है की compiler इस statement को किस प्रकार execute करता है।

जब compiler सबसे पहले int को execute करता है तो वह computer की memory में से 2 bytes की memory allot करता है


 इसके बाद जब compiler age को execute करता है तो वह उस 2 bytes की memory को age नाम दे देता है। इसके बाद जब  compiler = 10 को execute करता है तो वह 10 को इस memory location पर store कर देता है।




Types of variable in C:

1) local variable
2) global variable
3) static variable



1) Local Variable

1) local variables का scope केवल function के अंदर होता है।

2)  ये  variables function के अंदर  declared  किए जाते हैं और फ़ंक्शन के बाहर access नहीं कर सकते हैं।

3) नीचे दिए गए उदाहरण में, p और q variable का scope केवल  main function के  अंदर हैं। ये variables sample function को  visible नहीं होंगे।

4) इसी प्रकार a और  b variables का scope केवल  sample function के  अंदर हैं।  ये variables main function को  visible नहीं होंगे।

 

Example:


 #include<stdio.h>
void sample();

int main()
{
   int p = 10, q = 20;

 
        // p, q main function के local variable हैं।
        /*p और q variable का scope केवल  main function के  अंदर हैं।
            ये variables sample function को  visible नहीं होंगे।*/
            /* यदि आप इस फंक्शन में a और b access करने की कोशिश करते हैं,
            तो आपको 'a' undeclared और 'b' undeclared error मिलेगी */

 
   printf("\nvalues : p = %d and q = %d", p, q);
   test();
}

void sample()
{
   int a = 60, b = 70;

 
        // a, b sample function के local variable हैं।
        /*a और  b variables का scope केवल  sample function के  अंदर हैं।  
            ये variables main function को  visible नहीं होंगे।*/
            /*  यदि आप इस फंक्शन में p और  q access करने की कोशिश करते हैं,
            तो आपको 'p' undeclared और 'q' undeclared error मिलेगी  */

 
   printf("\nvalues : a = %d and b = %d", a, b);
}


Output:

values : p = 10 and q = 20
values : a = 60 and b = 70


2) Global Variable

1) global variables का scope पूरे  program में होगा। इन variables को program में कहीं से भी access किया जा सकता है।

2)  यह variables main function के बाहर defined किया गया है। ताकि, यह variables main function और अन्य सभी  sub functions के लिए visible हो। 


 Example:


#include<stdio.h>
void test();int p = 10 and q = 20;
int a = 60 and b = 70;

int main()
{
   printf("All variables are accessed from main function");
   printf("\n values: m=%d:n=%d:a=%d:b=%d", p,q,a,b);
   sample();
}

void sample()
{
   printf("\n All variables are accessed from" \"sample function");
   printf("\n values: m=%d:n=%d:a=%d:b=%d", p,q,a,b);
}


Output:

All variables are accessed from main function
values : p = 10 : q = 20 : a = 60 : b = 70


All variables are accessed from sample function
values : p = 10 : q = 20 : a = 60 : b = 70


3) Static Variable

1) Static variables केवल एक बार  initialized होते हैं

2) compiler variable के साथ program के अंत तक बना रहता है।

3) Static variables को फ़ंक्शन के अंदर या बाहर defined किया जा सकता है


4) static variables का default value शून्य है।

5) static variables program के execution तक जीवित रहता है
 
Syntax:

static data_type var_name = var_value;

Example:


#include<stdio.h>
int growth()
{
  static int count = 0;
  count++;
  return count;
}
 
int main()
{
  printf("%d ", growth());
  printf("%d ", growth());
  return 0;
}


Output:

1 2


Comments