C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Difference between Local and Global variables in C

As we know that variables are the name of memory blocks which are used to store values, in this tutorial we will learn how to declare local and global variables what are their scopes in C language?

Local variables

Before learning about the local variable, we should learn about the function block and function parts. There are two parts of the function block (block means region of the function between curly braces in C)

  1. Declaration part - Region where we declare all variables which are going to be used within the function (this part starts from starting curly brace "{").
  2. Executable part - Other statements except the declarations are the executable statements.

Local variables are the variables which are declared or defined within the declaration part of the function block.

These variables have local scope to that function only, in which they are declared. They cannot be accessed outside of the function.






Global variables

Global variables are the variables which are declared or defined below the header files inclusion section or before the main () function.

These variables have global scope to the program in which they are declared. They can be accessed or modified in any function of the program.

Global variable can also be accessed in another files too (for this, we have to declare these variables as extern in associate header file and header file needs to be included within particular file).

Let's consider the following program

#include <stdio.h>

/*global variables*/
int a,b;

/*function to set values to the global variables*/
void setValues(void)
{
	a=100;
	b=200;
}
int main()
{
	/*local variables*/
	int x,y;
	
	x=10;
	y=20;
	setValues();
	
	printf("a=%d, b=%d\n",a,b);
	printf("x=%d, y=%d\n",x,y);
	
	return 0;	
}

Output

    a=100, b=200
    x=10, y=20

In this program,
Global variables are: a and b
Local variables are: x and y

Here, a and b are using within the function setValues() and main() function because they are global and can be accessed anywhere. While x and y are using within the main() function only because they are local and declared in main() function, so they are accessible only for the main() function i.e. their scope is local for main() function only.

Can we declare local and global variables with the same name?

Yes, we can declare global and local variables with the same name but priority to access the variables are local that means if any function has local variables with the same name of the global variables, local variables will be accessed within that function, but if there are no local variables with the same name as global variables, global variables can be accessed within that function.

Let's consider the following program

#include <stdio.h>

/*global variables*/
int a,b;

/*function to set values to the global variables*/
void setValues(void)
{
	a=100;
	b=200;
}
/*function to print global variables*/
void getValues(void)
{
	printf("getValues ()...\n");
	printf("a=%d, b=%d\n",a,b);
}
int main()
{
	/*local variables*/
	int a,b;
	
	a=10;
	b=20;
	
	setValues();
	
	printf("main ()...\n");
	printf("a=%d, b=%d\n",a,b);
	
	getValues();
	
	return 0;	
}

Output

    main ()...
    a=10, b=20
    getValues ()... 
    a=100, b=200

In this program,
Global variables are: a and b
Local variables of main() are: a and b

When we print the value of a and b in main() function, values of local variables 10 and 20 are printing but when we print the value of a and b in getValues() function, values of global variables 100 and 200 are printing.


COMMENTS