What is the Scope of Variables in C Language?

Scope is the program area. Variable scope is the area of ​​your program where variables are declared and used. So you can have three types of scopes, depending on where they are declared and used …

  • Local variables are defined within a function or block.
  • Global variables are outside of all functions.
  • Formal parameters are defined by function parameters.

1. Local Variables:

Variables declared within a function or block are called local variables and are said to have local scope. These local variables can only be used within the function or block in which they are declared. A local variable can only be used (or accessed) in the block or function in which it is declared. Otherwise, it is invalid.

Local variables are created when control reaches the block or function that contains the local variable and then destroyed.

For example:
Here Code:
#include <stdio.h>
int main () {
  /* local variable declaration */
  int a, b;
  int c;
  /* actual initialization */
  a = 10;
  b = 20;
  c = a + b;
  printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
  return 0;
}

2. Global Variables:

Variables that are defined outside of all functions and accessible throughout the program are global variables and are said to have global scope. Once declared, any function of the program can access and modify them. You can give the local and global variables the same name, but the local variable takes precedence within the function.

For example:
Here Code:
#include <stdio.h>
/* global variable declaration */
int g;
int main () {
  /* local variable declaration */
  int a, b;
  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;
  printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
  return 0;
}

The program can give the local and global variables the same name, but the value of the local variable in the function takes precedence. Here is an example –

Here Code:
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main () {
  /* local variable declaration */
  int g = 10;
  printf ("value of g = %d\n",  g);
  return 0;
}

3. Formal Parameters:

Formal parameters are parameters that are used within the body of a function. Formal parameters are treated as local variables in the function and take precedence over global variables.

For example:
Here code:
#include <stdio.h>
/*Global variable*/
int x = 10;
void fun1(int x)
{
    /*x is a formal parameter*/
    printf("%d\n",x);
}
int main()
{
    fun1(5);
}

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 56 times, 1 visits today)

0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
You cannot copy content of this page
Scroll Up
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.