Scope Rules in C Language

In any programming language, the scope is the area in which a variable defined in the program exists; outside of this area, the variable cannot be accessed. In C language, there are three places where variables can be declared:Local variables inside functions or blocksGlobal variables outside of all functionsFormal parameters in function definitionsIn the last class, we discussed formal parameters, and today we will mainly learn about local variables and global variables.Local VariablesA variable declared inside a function or block is called a local variable. They can only be used by statements within that function or code block. Local variables are not known outside the function. Below is an example of using local variables. Here, all variables a, b, and c are local variables of the main() function.

#include <stdio.h>
int main (){
  // Declare variables
  int a, b;
  int c;
  // Initialize
  a = 10;
  b = 20;
  c = a + b;
  printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
  return 0;
}

The variables a, b, and c can only be accessed within the main() function; if accessed outside of the main() function, an error will occur.Global VariablesGlobal variables are defined outside of functions, usually at the top of the program. Global variables are valid throughout the program’s lifecycle and can be accessed from any function.Global variables can be accessed by any function. This means that once declared, global variables are available throughout the entire program. Below is an example of using global and local variables:

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

Global variables can be accessed anywhere in the program, and in the program, local and global variables can have the same name, but within a function, if two names are the same, the local variable’s value will be used, and the global variable will not be used; this is a point to keep in mind.Next, we will discuss formal parameters:Formal parameters are the parameters of a function, treated as local variables within that function. If they share the same name as a global variable, they will take precedence, which is essentially the same principle as the previous point. Below is an 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;
}

If you are interested, you can try compiling and running it yourself to see the results! Scope Rules in C LanguageDifferences in Memory Between Global and Local Variables

  • Global variables are stored in the global storage area of memory, occupying static storage units;
  • Local variables are stored in the stack, and storage units for variables are dynamically allocated only when the function they are in is called;

Initialization of Local and Global VariablesDuring our initial programming learning process, we often encounter situations where a variable is not defined or the variable value is not what we expect. Most of the time, this is because we forgot to define the variable. In C language, there is a rule:When a local variable is defined, the system does not initialize it; you must initialize it yourself; otherwise, the variable’s value will be a garbage value from the system. When defining a global variable, the system automatically initializes it:

Data Type Default Initialization Value
int 0
char ‘\0’
float 0
double 0
pointer NULL

However, even though the program automatically gives us default values for undefined variables, it is still advisable to develop a habit of initializing variables yourself, as it greatly helps in maintaining coding standards.So that concludes today’s discussion on scope rules. If anyone has any additions, feel free to share in the comments Scope Rules in C Language. Programming skills are not developed overnight, and I hope everyone will follow my lead, work hard together, and improve together. In the next class, we will discuss arrays in C language, opening the door to the new world of arrays (づ。◕‿‿◕。)づ

Leave a Comment