Home » C solved programs » C basic programs

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).

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







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.