static keyword in C

Keyword static is used for declaring static variables in c. A static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program.

When the program (executable or library) is loaded into memory, static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized).

Scope :
In terms of scope and extent, static variables have extent the entire run of the program. If declared a static variable or function globally then its visibility will only the file in which it has declared not in the other files.

1. Static global variables declared at the top level of the C source file have the scope that they can not be visible external to the source file. The scope is limited to that file.

2. Static local variables declared within a function or a block, also known as local static variables, have the scope that, they are visible only within the block or function like local variables. The values assigned by the functions into static local variables during the first call of the function will persist / present / available until the function is invoked again.

The static variables are available to the program, not only for the function / block. It has the scope within the current compile. When static variable is declared in a function, the value of the variable is preserved , so that successive calls to that function can use the latest updated value. The static variables are initialized at compile time and kept in the executable file itself. The life time extends across the complete run of the program.

Static local variables have local scope. The difference is, storage duration. The values put into the local static variables, will still be present, when the function is invoked next time.

Example :Consider a c program which has written in two files named as one.c and two.c:

//one.c
#include<stdio.h>
static int i=25;
static int j=5;
int main(){
    sum();
    return 0;
}

//two.c
#include<stdio.h>
extern int i; //Declaration of variable i.
extern int j; //Declaration of variable j.
/**
Above two lines will search the initialization statement of variable i and j either in two.c (if initialized variable is static or extern) or one.c (if initialized variable is extern)
*/

extern void sum(){
    int s;
    s=i+j;
    printf("%d",s);
}
Compilation Part :
[root@cisco project]# gcc one.c -o one.o -c
[root@cisco project]# gcc two.c -o two.o -c
[root@cisco project]# gcc -o myprog one.o two.o
two.o(.text+0x7): In function `sum':
: undefined reference to `j'
two.o(.text+0xd): In function `sum':
: undefined reference to `i'
collect2: ld returned 1 exit status

Hence we can say variable i and j which has initialized into two.c is not visible in file one.c. This example proves visibility of globally declared static variable is file.

Note: In the above example function sum which was declared and defined in two.c has also storage class extern. So we can call from other file (one.c).If it will static then we cannot call function sum since static storage class is only visible to the file where it has declared.

No comments:

Post a Comment