Learn C Scope Rules with Examples

In this tutorial, we will learn the scope rules in C and also we will discuss the three types of variables which is to be declared in C language.

What are Scope Rules in C?

In C programming, the scope of any programming is a program where a specific variable has a possible existence and ahead of that variable which cannot be accessed.

Here are the three types of variables:

  • Local Variables
  • Global Variables
  • Formal Parameters

So let us know what are local and global variables and formal parameters.

Local Variables

A local variable which is to be declared inside a function or in a block. They are only usable by statements, which are contained within that function or block of code. Furthermore, the local variables are not known to function within their own.

Here is an example on how to use the local variables: We assume we will use a variables x, y, and z which is a local to the main() function.

#include <stdio.h>
 
int main () {

  /* Here is the local variable declaration */
  int x, y;
  int z;
 
  /* Here is the actual initialization */
  x = 21;
  y = 29;
  z = x + y;
 
  printf ("The value of x = %d, y = %d and z = %d\n", x, y, z);
 
  return 0;
}

Output:

The value of x = 21, y = 29 and z = 50

Global Variables

In C language, a global variables are specified in outside of the function, generally on top of the code. Global variables can be accessed inside in any function that has been written for the program and maintains their values for the continuation of the program.

Here is an example on how to use the global variables.

#include <stdio.h>
 
/* Here is the global variable declaration */
int x;
 
int main () {

  /* Here is the local variable declaration */
  int k, l;
 
  /*Here is the  actual initialization */
  k = 31;
  l = 41;
  x = k + l;
 
  printf ("value of k = %d, l = %d and x = %d\n", k, l, x);
 
  return 0;
}

Output:

value of k = 31, l = 41 and x = 72

Formal Parameters

The formal parameters are designed as local variables within a function and they take a lead over global variables.

For example:

#include <stdio.h>
 
/* Here is the global variable declaration */
int x = 20;
 
int main () {

  /* Here is the local variable declaration in main function */
  int x = 42;
  int y = 68;
  int z = 0;

  printf ("value of x in main() = %d\n",  x);
  z = sum( x, y);
  printf ("value of z in main() = %d\n",  z);

  return 0;
}

/* Here is the function to add two integers */
int sum(int x, int y) {

   printf ("value of x in sum() = %d\n",  x);
   printf ("value of y in sum() = %d\n",  y);

   return x + y;
}

Output:

value of x in main() = 42
value of x in sum() = 42
value of y in sum() = 68
value of z in main() = 110

How to Initialize the Local and Global Variables?

To initialize the local variable we must need to defined it and which is not initialized by the system, we should initialize it on our own. While the global variables are initialized automatically in a system when we define them.

Data TypeInitial Default Value
int0
char‘\0’
float0
double0
pointerNULL

If variables are not initialized correctly, your program may provide unexpected results because uninitialized variables will take whatever junk value is already present at their memory location.

Conclusion

To conclude, we already learned the scope of the rules and give examples with output. We explained the three types of variables which are the local variables, global variables and formal parameters.

We hoped that this article would help you to understand more about Scope Rules in C and how to use it in your own software development projects.

Leave a Comment