Easy Tutorial
❮ C Examples For Even Odd C Exercise Example76 ❯

Scope Rules in C

In any programming language, scope refers to the region of the program where a defined variable can exist and beyond which it cannot be accessed. In C, there are three places where variables can be declared:

Let's understand what local variables, global variables, and formal parameters are.

Local Variables

Variables declared within a function or block are called local variables. They can only be used by statements inside that function or code block. Local variables are not known outside their function. Here is an example using local variables. Here, all variables a, b, and c are local to the main() function.

Example

#include <stdio.h>

int main ()
{
  /* Local variable declaration */
  int a, b;
  int c;

  /* Initialization */
  a = 10;
  b = 20;
  c = a + b;

  printf ("value of a = %d, b = %d and c = %d\n", a, b, c);

  return 0;
}

Global Variables

Global variables are defined outside of all functions, typically at the top of the program. They are available throughout the lifetime of the program and can be accessed by any function.

Global variables can be accessed by any function. That is, they are available throughout the entire program after declaration. Here is an example using global and local variables:

Example

#include <stdio.h>

/* Global variable declaration */
int g;

int main ()
{
  /* Local variable declaration */
  int a, b;

  /* Initialization */
  a = 10;
  b = 20;
  g = a + b;

  printf ("value of a = %d, b = %d and g = %d\n", a, b, g);

  return 0;
}

In a program, local and global variables can have the same name, but within a function, if there is a local variable with the same name, the global variable will not be used. Here is an example:

Example

#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;
}

When the above code is compiled and executed, it produces the following result:

value of g = 10

Formal Parameters

Parameters declared in a function, known as formal parameters, act as local variables within that function and take precedence over global variables if they share the same name. Here is an example:

Example

#include <stdio.h>

/* Global variable declaration */
int a = 20;

int main ()
{
  /* Local variable declaration in the main function */
  int a = 10;
  int b = 20;
  int c = 0;
  int sum(int, int);

  printf ("value of a in main() = %d\n", a);
  c = sum(a, b);
  printf ("value of c in main() = %d\n", c);

  return 0;
}

/* Function to add two integers */
int sum(int a, int b)
{
    printf ("value of a in sum() = %d\n", a);
    printf ("value of b in sum() = %d\n", b);

    return a + b;
}

When the above code is compiled and executed, it produces the following result:

value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30

>

Difference in Memory Allocation between Global and Local Variables:

For more details, refer to: Global and Local Variables in C/C++ with static keyword

Initializing Local and Global Variables

When a local variable is defined, the system does not initialize it, and you must initialize it yourself. Global variables are automatically initialized by the system as follows:

Data Type Initial Default Value
int 0
char '\0'
float 0
double 0
pointer NULL

It is a good programming practice to initialize variables correctly, as uninitialized variables can lead to unexpected results due to garbage values in memory locations.

❮ C Examples For Even Odd C Exercise Example76 ❯