Q:

Write a C program to demonstrate example of global and local scope

0

C program to demonstrate example of global and local scope

This program will demonstrate the example of global and local scope in c programming language. These scopes are used according to the requirement.

Variable and function which are declared in the global scope can be accessed anywhere in the program, while variable and functions which are declared within the local scope can be accessed in the same block (scope).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use of Global and Local Scope (Blocks) in c

/*C program to demonstrate example global and local scope. */
 
#include <stdio.h>
 
int a=10;       //global variable
 
void fun(void);
 
int main()
{
  int a=20;  /*local to main*/
  int b=30;  /*local to main*/
 
  printf("In main()  a=%d, b=%d\n",a,b);
  fun();
  printf("In main() after calling fun() ~ b=%d\n",b);
  return 0;
}
 
void fun(void)
{
  int b=40;  /*local to fun*/
 
  printf("In fun()  a= %d\n", a);
  printf("In fun()  b= %d\n", b);
}

Output

    In main()  a=20, b=30
    In fun()  a= 10
    In fun()  b= 40
    In main() after calling fun() ~ b=30

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now