Read in Hindi
Read in English

Going forward with this C/C++ programming language tutorial today we will know about scope of variable using function examples. If you don't know about C function and C variables then read them 1st.

What is scope
In C scope of variable means after declaring/defining a variable where can we read value stored in that variable. If we have defined a variable inside any function then that variable can be read after defining it anywhere in that function, but not outside. If variable is defined inside loop then it can not be read outside loop. It is easy to understand. Variable defined inside any {...} can be read anywhere inside that after defining, but not outside.
In the example given below we will make a register where any name can be added and all names in register can be printed. When you run this program, a menu will be displayed which will give you option to add any name in register or print all names.
#include <stdio.h>
char list[10][20]; 
int length = 0;
void add_name() {
  if(length >= 10) {
    printf("list is full\n");
    return;
  }
  printf("Enter the name: ");
  scanf("%s", list[length]);
  length = length + 1;
  printf("name added\n");
}
void print_list() {
  int i = 0;
  for(i=0; i < length; i++) {
    int serial_no = i + 1;
    printf("%d : %s\n", serial_no, list[i]);
  }
}
int main() {
  int input = 0;
  while(input != 3) {
    if(input == 0) {
      printf("0 - Print this menu\n");
      printf("1 - add name to list\n");
      printf("2 - print list\n");
      printf("3 - quit program\n");
    }
    else if(input == 1) {
      add_name();
    }
    else if(input == 2) {
      print_list();
    }
    else {
      printf("Wrong Input, try again\n");
    }
    scanf("%d", &input);
  }
}

Running program given above will print a menu. Enter 1 to add a name, Then it will ask what name you want to enter. Type the desired name and enter. To enter another name Again enter 1. To print all names you have entered press 2. Try to rum this program 1st then we will understand how it works.

Now lets see how it works. In beginning we have defined 2 variables: list and length. Since these 2 variables are not inside any function or { } hence can be accessed from anywhere in program.
Now lets see char list[10][20]; just consider char list[10]; 1st which means array of characters with capacity 10. char list[10][20]; means it can contain 10 arrays with each array have capacity of 20 char. Here instead of 10 chars it will contains 10 arrays.
If it was int a[2][3] then we can see in the way show below.
a = {  {1,2,3} ,   {4,5,6}   };

Here a can hold 2 arrays one at a[0] and another at a[1]. Each of these two arr can hold 3 integers.
We will use char list[10][20]; to store names. We have seen that name is a string which can be stored in char array. Here list can contain 10 arrays hence we can store 10 names. Each array can hold up to 20 char Hence we can store names having less than 20 char. 2nd variable length how many names have been added to the list which is 0 in the beginning since no name on the list.
After that there is add_name function, calling which adds name into the list. 1st we check if list is full(10 names added) then stop executing function using return statement, otherwise read name in the list using scanf. 1st argument of scanf is %s hence it will read string, hence 2nd argument should be char array. list[0], list[1], list[2] ... list[19] are char array.
After adding name to the list length is increased by 1. list[length] always gives next empty space in list. In the beginning list is empty hence list[length] (length=0) is empty. after adding a name at list[0] length becomes 1 now list[1] (length = 1) is empty.
After that print_list function is defined, which reads length variable to know how many named are on the list. It prints all those names using for loop.Inside loop a variable serial_no is defined which can not be accessed outside for loop.

In the end there is main function from where execution starts. Using if-else according to input variable either we add name to the list, or print list or print menu inside a while loop. While loop will continue till user enters 3.
If you like this, share this with you friends in facebook, twitter,orkut.
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम function के कुछ examples के द्वारा variable के scope के बारे में जानेंगे. अगर आपको C function और C variables का ज्ञान नहीं है तो पहले इन्हें पढ़ लें.

What is scope
C में किसी variable के scope का मतलब यह है कि उस variable को declare या define करने के बाद कहाँ कहाँ पर उसकी value पढ़ सकते हैं. अगर हमने किसी function के अंदर कोई variable x define किया है तो उस function के अंदर कहीं भी उस variable को read/write कर सकते हैं परन्तु किसी अन्य function के अंदर नहीं. अगर किसी loop के अंदर कोई variable define किया है तो उस loop के अंदर कहीं भी उस variable को read/write कर सकते हैं परन्तु उसके बाहर नहीं.
इसे समझना बहुत आसान है. किसी भी {...} के अंदर define किया गया variable उसके अंदर कहीं भी read/write किया जा सकता है परन्तु उसके बाहर नहीं. इसे हम नीचे दिए गए example से समझेंगे.
नीचे दिए गए program में हम एक register बनायेंगे जिसमेकोई भी नाम add कर सकते हैं और सभी नामों को print कर सकते हैं. program को run करने पर एक Menu आयेगा जिसके द्वारा हम उस register में name add कर सकते हैं या register के सभी नामो को print कर सकते हैं.
#include <stdio.h>
char list[10][20]; 
int length = 0;
void add_name() {
  if(length >= 10) {
    printf("list is full\n");
    return;
  }
  printf("Enter the name: ");
  scanf("%s", list[length]);
  length = length + 1;
  printf("name added\n");
}
void print_list() {
  int i = 0;
  for(i=0; i < length; i++) {
    int serial_no = i + 1;
    printf("%d : %s\n", serial_no, list[i]);
  }
}
int main() {
  int input = 0;
  while(input != 3) {
    if(input == 0) {
      printf("0 - Print this menu\n");
      printf("1 - add name to list\n");
      printf("2 - print list\n");
      printf("3 - quit program\n");
    }
    else if(input == 1) {
      add_name();
    }
    else if(input == 2) {
      print_list();
    }
    else {
      printf("Wrong Input, try again\n");
    }
    scanf("%d", &input);
  }
}

ऊपर दिए गए program को run करने पर शुरू में एक menu print होगा जो यह बताएगा कि क्या enter करने पर क्या होगा. List में नाम add करने के लिए 1 press करके enter करें, इसके बाद या पूछेगा कि कौन सा नाम add करना है. नाम type करके enter करें. फिर से दूसरा नाम add करना हो तो फिर से 1 enter करें. List print करने के लिए 2 Enter करें. पहले इस program को run करके देखें फिर इसे समझते हैं कि यह कैसे काम करता है.

अब देखते हैं कि यह कैसे काम करता है. शुरू में हमने 2 variable list और length declare किया है. चूंकि ये दोनों variable किसी function के अंदर नहीं बल्कि सबसे बाहर हैं इसलिए इन्हें किसी भी function या loop के अंदर से read/write कर सकते हैं. नियम यह है कि variable जिस {} के अंदर declare किये गए हैं उससे बाहर कहीं भी access नहीं किये जा सकते, उसके अंदर कहीं भी access किये जा सकते हैं.
अब आते हैं char list[10][20]; पर. अगर सिर्फ char list[10]; होता तो इसका मतलब यह होता कि list एक array है जिसमे 10 char आ सकते हैं. पर यहाँ char list[10][20]; है इसका मतलब है कि list एक array है जिसमे 20 char वाली 10 array आ सकती हैं. यहाँ 10 char की जगह 10 array आएँगी और हर एक array 20 char वाली होगी.
अगर int a[2][3] होता तो इस तरीके से देख सकते हैं.
a = {  {1,2,3} ,   {4,5,6}   };

यहाँ पर a के अंदर 2 array आ सकती हैं एक array a[0] में और एक a[1] में. उन दोनों array में 3 3 int आ सकते हैं.
char list[10][20]; का use नाम store करने के लिए करेंगे. हम देख चुके हैं कि नाम string है जिसे char array में store करते हैं. यहाँ list में 10 char array आ सकती हैं इसलिए इसमें हम 10 नाम store कर सकते हैं. हर एक array में 20 char आ सकते हैं इसलिए हम वही नाम store कर सकते हैं जिसमे 20 या उससे कम letter हो. दूसरा variable length या बताता है कि currently list में कितने नाम हैं, जोकि शुरू में 0 है.
इसके बाद add_name function है जिसे call करने पर यह list में एक नाम add कर देगा. पहले हम check करते हैं अगर list में 10 नाम आ चुके हैं तो return का use करके function को वहीँ समाप्त कर देते हैं, अन्यथा scanf का use करके list variable में सीधे नाम read कर लेते हैं. ध्यान दें कि scanf का पहला argument "%s" है जो कि string read करता है इसलिए दूसरा argument char array होना चाहिए. list[0], list[1], list[2] ... list[19] सभी 20 char वाली array हैं.
list में नाम add होने के बाद हम length 1 बढ़ा देते हैं. list[length] हमें list में हमेशा अगली खाली जगह दे देगा क्योंकि शुरू में list खाली है इसलिए list[length] (length=0) वाली char array खाली है. एक नाम list[0] में add होने के बाद length = 1 हो जायेगा, अब list[length] (length = 1) वाली array खाली है.
इसके बाद print_list function define किया है, जिसे length से यह पता चल जाता है कि list में कितने नाम हैं उतना ही for loop से print कर लेते हैं. यहाँ for loop के अंदर एक variable serial_no define किया है जो उस loop के बाहर से access नहीं होगा.

अंत में main function है जहाँ program run होना start होता है. जिसमे if-else statement का use करके input variable की value के according list में नाम add करते हैं, list print करते हैं, menu print करते हैं. इस पुरे if-else को while loop के अंदर लिखा गया है, जो कि तब तक run होगा जब तक input की value 3 नहीं हो जाती. while loop खत्म होने से पहले input variable में scanf का use करके input read करते हैं, अगर input 3 है तो while loop खत्म होके program finish हो जायेगा otherwise while loop के अंदर फिर से आकर input value के according if-else का use करके list में नाम add करते हैं, list print करते हैं, menu print करते हैं और फिर से input variable में scanf का use करके input read करते हैं.
अगले लेख में हम function के बारे में जानेंगे जो कि बहुत important है.