Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, March 2, 2020

Scope Rule in C

welcome2protec | Scope Rule in C

Scope rule in C:

The scope of a variable is the region of a program in which the variable is visible (i.e. in which it is accessible by its name and can be used in the program.

There are basically three scope rules:

  • Local Scope 
  • Global Scope
  • Function Prototype Scope

Local Scope:

Local scope is also known as block scope. A Block is a group of statements wrapped with a curly braces i.e. ‘{‘ statements ‘}’ respectively. Blocks may be nested in C (a block may contain other blocks inside it). A variable declared inside a block is accessible in the block and all inner blocks of that block, but not accessible outside the block. Basically these are local to the blocks in which the variables are defined and are not accessible outside.

For Example:

#include <stdio.h>  
int main() 
{ 
 { 
      int x = 9, y = 90; 
   { 
     /* The outer block contains declaration of x and Y, 
      * so following statement is valid and prints 9 and 90 */
      printf("Value of x = %d, and y = %d\n", x, y);
   { 
     /* y is declared again, so outer block y is not accessible
      * in this block */ 
      int y = 99;
     /* Let's change the outer block variable x to 10 */
      x++; 

     /* Now let's change this block's variable y to 100 */
      y++; 
      printf("Value of x = %d, y = %d\n", x, y); 
    } 

     /* This statement will accesses only outer block's variables */
      printf(" Value of x = %d, y = %d\n", x, y); 
   } 
 } 
 return 0; 
 } 

Output:

Value of x = 9, and y = 90 
Value of x = 10, and y = 100
Value of x = 9, and y = 90

Global Scope:

The variables which are declared outside of all of the function or a blocks, at the top of the program and can be accessed from any portion of the program. this is called global scope of variable as the can be globally accessed. Have a look at the below example:

Example 1 | Global Variable

  /* C program to illustrate the global scope of variables */
  
  #include <stdio.h> 
   
  /* Global variable */ 
  int x = 9; 
   
  /* Accessing global variable x within a display() function */
  void display() 
  { 
    printf("%d\n", x); 
  } 
   
  /* main function */
  int main() 
  { 
    /* Let's print the value of globla variable x before 
     * changing it form main() */
    printf("Before changing the value of x = %d", display());
   
    /* Now let's change the value of global variable x
     * from main(), then call display() function. */
     x = 99;
     printf("After changing the value of x = %d", display());  
  } 

Output:

Before changing the Value of x = 9 
After changing the value of x = 99

Example 2: | Global Variable

  /* filename: file1.c */

  int a; 

  int main(void) 
  { 
   a = 2; 
  } 

  /* filename: file2.c, When this file is linked with file1.c - 
   * function of this file can access the global variable 'a' */ 

  extern int a; 

  int myfun() 
  { 
   a = 2; 
  } 

Next to be updated soon...








Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us