Q:

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

0

Write a C program to demonstrate example of global and local scope. Here’s simple program to demonstrate example of global and local scope in C Programming Language.

Scope : :

“Scope” is just a technical term for the parts of your code that have access to a variable.

 
 
  • Variables defined outside a function are […] called global variables.

  • Variables defined within a function are local variables.

A local variable is a variable which is either a variable declared within the function or is an argument passed to a function.

A global variable (DEF) is a variable which is accessible in multiple scopes.  It is important to note that global variables are only accessible after they have been declared.

All Answers

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

Below is the source code for C program to demonstrate example of global and local scope which is successfully compiled and run on Windows System to produce desired output as shown below :

SOURCE CODE : :

/*  C program to demonstrate example of 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 : :

/*  C program to demonstrate example of global and local scope  */

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

Above is the source code for C program to demonstrate example of global and local scope which is successfully compiled and run on Windows System.The Output of the program is shown above .

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

total answers (1)

C Basic Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C program to convert feet to inches... >>
<< Write a C program to multiply two numbers using pl...