Local And Global Variable in C Language – CsTutorialpoint

Hello friends, in today’s article we are going to talking about Local Variable and Global Variable, but to know better about Local Variable and Global Variable, you need to know about some basic things in advance, so let’s talk about those basic things.

As we know, a Variable is the name of the place in the memory in which we store the data or information. And in C language we cannot use any variable without declaring it.

If we want to use any variable in our program, then need to declare it first.

On the basis of the declaration of the variable, we can divide the variable into two parts – Local Variable and Global Variable.

Where is the declaration of the variable, on the basis of this we can tell which variable is local variable and which variable is global variable?

To understand better about local variables and global variables, you need to know about the scope of variables, so without wasting time let’s go to know what is the scope of variables in C language.

What is Variable Scope?

The scope of a variable means the lifetime of that variable, anyone can access it only in the block or function in which it is declared.

Let’s understand this with an example –

#include<stdio.h>
void func()
{
int z = 5;
}

See the above example, In this example, we have declared a variable named x inside the fan() function, whose scope will remain only till the fan() function, we cannot use it in any other function and if we try to do so, then the compiler error gives |

Example -:

#include<stdio.h>
void func()
{
int z = 5;
}
void main()
{
 printf(" %d", x);
}

Output -:

error: 'x' undeclared (first use in this function)

In the above program, you saw when we accessed the variable (Which is declared in fun() function) by the main() function and wanted to print its value, the program did not run and the compiler gave an error.

It is clear that the scope of any variable is limited to the function or block in which it is declared.

The scope of the variable depends on its declaration and in C language we can declare the variable in three places.

  • inside a function or block
  • outside function or block
  • Inside a function parameter at the time of function definition

Now Let’s understand in detail that what is called when a variable is declared in which place.

Local and Global Variable In C
Local and Global Variable In C

Local Variable In C

When we declare a variable inside a function or block, then it is called a Local Variable.

We can use this variable in that block or function in which that variable is declared.

The lifetime of a local variable is only equal to the lifetime of the function or block in which it is declared, as soon as that function or block is destroyed, the local variable is also destroyed and the space it has got in the memory is also released.

#include <stdio.h>
void main()
 {
  int a, b; //local variable declaration
  int c;
 
  a = 10; // actual initialization
  b = 20;
  c = a + b;
 
  printf("sum of %d, and %d is : %d\n", a, b, c);
 }

Output

sum of 10 and 20 is: 30

In this example, we have created three variables named a, b, and c inside the main() function, whose scope is limited to this main() function, we cannot use it in any other function.

 If we try to use declare variable inside main() function in another function made in the program then compiler gives error.

Advantages of Local Variable In C

When we declare a variable inside a function, then its scope is limited to that variable so that if we want, we can create a variable with the same name in two different functions, this will not cause any problem in the program because the scope of both will be limited to that function.

The memory that the local variable gets is available only till the function in which that local variable is declared is run in the computer. As soon as the function ends, the variable’s memory is released and that memory can be used by any other variable of the program.

Priority of local variable is greater than a global variable.

Disadvantages of Local Variables In C

  • The scope of a variable is limited so that a variable declared in a function cannot be used by another function.
  • The debugging process becomes a bit tricky.
  • Data sharing is not possible in local variable.

Global Variable In C

Global variables are declared outside the function or block. Generally the Global Variable is declared at the top of the program.

We can use global variable in any block or function of the program. Any function can use and change the value of this variable.

Since a global variable is not declared in any block or function, its scope is global. So that we can use these variables in any block or function.

The lifetime of a global variable is equal to the lifetime of the entire program, and as soon as the program ends, the global variable is also destroyed and its available space in the memory is also released.

This variable needs to be declared before it can be used.

#include <stdio.h>
 int g; // global variable declaration
void main()
{
  int a, b; // local variable declaration

  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;
 
  printf("value of a = %d, b = %d and g = %d\n", a, b, g);
 }

Output -:

value of a = 10, b = 20 and c = 30

In this example, we have declared a variable named g at the very beginning of the program. Since it is not declared inside any function or block, the variable g is a global variable.

As we know, we can use global variable inside any block or function, so we have used global variable g in main() function.

Note -: If the name of the global variable is same as the local variable, then in this case we give priority to the local variable.

Example -:

#include <stdio.h>
int g = 20; // global variable declaration
void main()
{
  int g = 10; // local variable declaration

  printf("value of g = %d\n", g);
}

Look at this example, a variable that is declared at the top of the program and another variable that is declared inside the main() function, both have the same name.

In this case, when we print the value of g variable, then the value of local variable g created inside main() function is printed because When it comes to priority among local and global variables, then the local variable is given the highest priority.

Output -:

value of g = 20

Advantages of Global Variable

  • Global variables can be accessed by any function in the program.
  • The global variable needs to be declared only once in the program.
  • This is very useful when all the functions of the program are accessing the same data.

Disadvantages of Global Variable

  • The value of a global variable can be changed at any time by any function.
  • If we use a lot of global variables in our program, then the possibility of error in the program increases.
  • The life of a global variable is similar to the life of the program, as long as the program remains in the memory, the global variable will remain there, so even when we do not have to use the variable, there is still such memory which is a waste of memory.

Formal Parameters

While defining a function, when we declare a variable inside the function parenthesis, then it is called Formal Parameters and the scope of such a variable is local.

The variable we declare inside the function parenthesis is also like a local variable whose lifetime is limited to the running of that function as soon as the function is destroyed the variable also gets destroyed.

#include <stdio.h>
 int a = 20; // global variable declaration
void main()
 {

  // local variable declaration
  int a = 10;
  int b = 20;
  int c = 0;

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

   return a + b;
}

Difference Between Local Variable and Global Variable

What is more useful?

Both local and global type variables have their own importance in a program. However, by creating too many global variables in a program, the program needs a lot of memory space because even if global variables are not being used in the program, they still get memory till the end of the program.

In such a situation, we should try not to make useless global variables in our program. Because this sometimes makes it difficult to identify which variable is global and which is local.

Conclusion

Friends, I hope that after reading this article you know very well, what is function in C? And how many types of functions are in C language?

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

If you liked this post, then do not forget to share this post with your friends so that they can also get information about Function in C Language

If you still have any questions or doubts related to Function in C Language, then you must tell me, I will answer all your questions and you can contact us for more information.

Subscribe to this website newsletter to get information related to this new technology, Programming Language, Coding, C Language, C++Python Course, Java Tutorial.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment