Here we are going to see what are C Local and Global variables and their differences.

C Local variables:

A local variable is declared inside a function. A local variable is visible only inside their function, only statements inside a function can access that local variable.

Local variables are declared when the function execution started and local variables get destroyed when the control exits from the function.

Let us consider an example:

#include <stdio.h>
void test();
void main() {
  int p = 22, q = 44;
  test();
  printf("Values in main() function p = %d and q = %d\n", p, q);
}
void test() {
  int p = 50, q = 80;
  printf("Values in test() function p = %d and q = %d\n", p, q);
}

Output:

Values in test() function p = 50 and q = 80
Values in main() function p = 22 and q = 44

In the above code we have 2 functions main() and test(), in these functions local variables are declared with the same variable names p and q but they are different.

Operating System calls the main() function at the time of execution. the local variables within the main() are created when the main() starts execution. When a call is made to test() function, first the control is transferred from main() to test(), next to the local variables within the test() is created and they are available only within the test() function.

After completion of execution of test() function, the local variables are destroyed and the control is transferred back to the main() function.

C Global variables:

Global variables are declared outside of any function. A global variable is visible to every function and can be used by any piece of code. Unlike local variablesglobal variables retain their values between function calls and throughout the program execution.

Let us consider an example:

#include <stdio.h>
int a = 20; // Global declaration
void test();
void main() {
  printf("In main() function a = %d\n", a); // Prints 20
  test();
  a = a + 15; // Uses global variable
  printf("In main() function a = %d\n", a); // Prints 55
}
void test() {
  a = a + 20; // Uses global variable
  printf("In test() function a = %d\n", a); // Prints 40
}

Output:

In main() function a = 20
In test() function a = 40
In main() function a = 55

In the above code, the global variable a is declared outside of all the functions. So, the variable a can be accessed in every function. Operating System calls the main() function at the time of execution. the variable a has no local declaration, so it accesses the global variable a.

In test() function also there is no local declaration of variable a, the variable a gets access from the global. The global variables are destroyed only after the completion of the execution of the entire program.

Difference between local & global variables:

Local variables are declared and used inside a function (or) in a block of statements. Local variables are created at the time of function call and destroyed when the function execution is completed.

Local variables are accessible only within the particular function where those variables are declared. Global variables are declared outside of all the function blocks and these variables can be used in all functions. Global variables are created at the time of program beginning and reside until the end of the entire program.

Global variables are accessible in the entire program. If a local and global variable has the same name, then the local variable has the highest precedence to access within the function.

Let’s consider an example:

#include <stdio.h>
void change();
int a = 17; // Global Variable x
void main() {
  int a = 35; // Local Variable x
  change();
  printf("%d\n", a); // The value 35 is printed
}
void change() {
  printf("%d\n", a); // The value 17 is printed
}

Output:

17
35

In the above code, the global and local variables have the same variable name a, but they are different.

In the main() function the local variable a is only accessed, so it prints the value 35. In the change() function the variable a is not declared locally so it accesses global variable a, so it prints 17.

References:

Happy Learning 🙂

Hemanth Krishna Vineel Ramayanam
I’m an Undergraduate Student at the Indian Institute of Information Technology, Design, and Manufacturing, Kurnool, Andhra Pradesh. I belong to the CSE domain.

Leave A Comment