c
C Programming Tutorial : Introduction

Local, Global and Static Variables in C

Local Variables

The variables that are defined within the body of a function() or a block, are known as local variables.

Syntax-

func ( )
{
Int a,b;
…………………..
…………………..
}
 

Here a and b are local variables that are defined within the body of the function func(). Local var can be used only in those functions or blocks, in which they are declared. The same variable may be used in different functions.

Syntax-

Func1()
{
Int a=2, b=4;
…………………….
…………………….
}
Func2
{
Int a=15, b20;
……………………..
……………………..
}
 

Here values of a=2, b =4 are local to the function func1( ) and a =5, b=20 are local to the function func2().

Global Variables

The variables that are defined outside any function are called global variables: All functions in the program can access and modify global variables. It is useful to declare a variable global if it is to be used by many functions in the program. Global variables are automatically initialized to 0 at the time of declaration.

/*Program to understand the use of global variables*/
·#nclude<stdio.h>
Void funcl (void);
vaid func2 (void);
int a, b=6;
main();
{
    printf("'Inside main() : a= %d, b=%d\n”,a,b);
    func1();
    func2();
}
Void func1(void)
{
    Printd(“Inside Func1() : a=%d, b=%d\n”,a,b);
}
Void func2(void)
{
    Int a=8;
    Printd(“Inside Func2() : a=%d, b=%d\n”,a,b);
}

Output:

Inside maine ): a=0, b=6
Inside funcl(): a=0, b=6
Inside func2(): a=8, b=6
 

here a and b are declared outside all functions hence they are global variables. The variable will be initialized to 0 automatically since it is a global variable. Now we can use these variables in any function. In func2( ), there is a local variable with the same name as a global variable. Whenever there is a conflict between a local and global variable, the local variable gets the precedence. So inside func2( ) the value of local variable gets printed.

Static Variables

Static variables are declared by writing keyword static in front of the declaration.

Syntax

static type var_name;

A static variable is initialized only once, and the value of a static variable is retained between function calls. If a static variable is not initialized, then it is automatically initialized to 0.

Example-

/ *P6. 23 Program to understand the use of static variables* /
#include<stdio.h>
void func(void);
mainJ()
{
    func();
    func();
    func();
}
Void func (void)
{
    Int a=10;
    static int b=10;
    printf ("a =%d b=%d\n”,a,b);
    a++;
    b++;
}
Output:
a=10	b=10
a=10	b=11
a =10	b=12
 

Here ‘b’ is a static variable. The first time when the function is called ‘b’ is initialized to 10. Inside the function, the value of ‘b’ becomes 11. This value is retained and when next time the function is called value of ‘b’ is 11 and the initialization is neglected. Similarly, when a third-time function is called, the value of ‘b’ is 12. Note that the variable ‘a’, which is not static is initialized on each call and its value not retained.