The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Local Scope In C

  • The variables declared inside a function or a block is known as local variables.
  • Local variables can be accessed only within function or block.
  • Local variables can not be accessed outside a function or a block.
Local Variables in C

C program - Local Scope

localscope.c
#include <stdio.h>
int main()
{
int m = 5;
void display(){
int m = 10; //Local variable declaration
printf("Value of M inside a function is %d",m);
}
display();
printf("\nValue of M outside a function is %d",m);
return 0;
}
  • Value of M inside a function is 10
  • Value of M outside a function is 5

Note:

m is a variable used locally and globally.

Did You Know?

If there is no declaration of variable in a subfunction. Then the value of a variable will be fetched from global declaration. Let us check the same program that we used early.

localscope.c
#include <stdio.h>
int main()
{
int m = 5;
void display(){
//No Local variable declaration
printf("Value of M inside a function is %d",m);
}
display();
printf("\nValue of M outside a function is %d",m);
return 0;
}
  • Value of M inside a function is 5
  • Value of M outside a function is 5

Note:

m is a variable used locally, but fetches a values globally.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report