The static
keyword in C is employed to declare static variables, and this modifier is applicable to various data types, including int, float, double, arrays, pointers, structures, functions, and others.
Important
points about static keyword:
1.It is not the default storage class for global variables. For example, analyze the following three
programs and its output.
(a)
#include<stdio.h>
int a;
int main(){
printf("%d",a);
return 0;
}
Output:
0
(b)
#include<stdio.h>
static int a;
int main(){
printf("%d",a);
return 0;
}
Output:
0
(c)
#include<stdio.h>
extern int a;
int main(){
printf("%d",a);
return 0;
}
Output:
Compilation error
Upon initial inspection of the output from the three aforementioned codes, it might seem that the default storage class for global variables is static. However, this assumption is inaccurate. The reason lies in understanding the extern
storage class.
2. The default initial value of static integral type variables in C is zero. If the integral type variable is a pointer, the default initial value is typically NULL
. For example:
#include <stdio.h>
static char c;
static int i;
static float f;
static char *str;
int main(){
printf("%d %d %f
%s",c,i,f,str);
return 0;
}
Output:
0 0 0.000000 (null)
3.A static variable can be declared multiple times, but initialization is permissible only once for the same variable. For example:
(a)
#include <stdio.h>
static int i; //Declaring the variable i.
static int i=25; //Initializing the variable.
static int i; //Again declaring the variable i.
int main(){
static int i; //Again declaring the variable i.
printf("%d",i);
return 0;
}
Output:
25
(b)
#include <stdio.h>
static int i; //Declaring the variable
static int i=25; //Initializing the variable
int main(){
printf("%d",i);
return 0;
}
static int i=20; //Again initializing the variable
Output: Compilation
error: Multiple initialization variable i.
4. In C, assignment statements, like other executable code, cannot be placed at the global scope. Assignment statements need to be inside a function or a code block. Global scope is generally reserved for variable declarations and function prototypes. For example:
#include <stdio.h>
static int i=10; //Initialization statement
i=25; //Assignment statement
int main(){
printf("%d",i);
return 0;
}
Output:
Compilation error
Note: Assigning a value to a variable at the time of its declaration is termed "initialization." On the other hand, assigning a value to a variable at a later point, separate from its declaration, is referred to simply as an "assignment."
(b)
#include <stdio.h>
static int i=10;
int main(){
i=25; //Assignment statement
printf("%d",i);
return 0;
}
Output:
25
(5) A static variable is initialized only once throughout the entire program's execution. This initialization occurs during the startup phase, and the variable retains its value across subsequent calls to the function or block where it is declared. For example:
#include <stdio.h>
static int i=10;
int main(){
i=5;
for(i=0;i<5;i++){
static int a=10; //This statement will execute
//only time.
printf("%d",a++);//This statement will
execute
//five times.
}
return 0;
}
Output:
10 11 12 13 14
(6)When a static variable is declared locally within a block, its visibility is confined to the specific block where it is declared.
For example:
#include<stdio.h>
int main(){
{
static int a=5;
printf("%d",a);
}
//printf("%d",a); variable a is not
visible here.
return 0;
}
Output:
5
7. When a static variable or function is declared globally, its visibility is restricted to the file in which it has been declared and is not accessible from other files. For example:
(a)
#include<stdio.h>
static float a=144.0f; //global
to all function
int main(){
{
printf("%d",a); //variable a is
visible here.
//printf("%d",b);
variable b is not visible here.
}
printf("%d",a); //variable
a is visible here.
//printf("%d",b); variable b is not
visible here.
return 0;
}
static int b=5; //Global to only calculation function
void calculation(){
printf("%d",a); //variable
a is visible here.
printf("%d",b); //variable
b is visible here.
}
(b)
Consider a c program which has written in two files named as one.c and two.c:
//one.c
#include<conio.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);
}
Compile
and execute above two file one.c and two.c at the same time:
In Turbo c compiler
Step
1: Write above two codes in the file named as one.c and two.c (You can give any
name as you like) and save it.
Step
2: In Turbo c++ IDE click on Project
-> Open project menu as shown in following screen dump:
Step
3: After Clicking on open project you will get following screen:
In
Open project File text field write any project name with .prj extension. In
this example I am writing project name as CProject.PRJ. Now press OK button.
Step
4: After pressing OK button you will get following screen:
Now
click on Project -> Add item menu.
Step
5: After clicking Add item you will get following screen:
Enter the names of each C source code file sequentially into the name text field. Begin by typing "one.c" and proceed by clicking the "Add" button. Subsequently, input "two.c" and repeat the process for each file in succession.
Step
6: After inputting all the C source code file names and clicking on the "Done" button, the system will conclude the input process.
you will get following screen:
Towards the bottom of the window, you will find information such as the project name and a list of the files you have added.
Step7: Compile the two files by pressing Alt+F9, and execute the program by pressing Ctrl+F9.
Note: To close the project, navigate to Project -> Close Project.
Output: Compilation error: Undeclared symbols 'i' and 'j'.
The variables 'i' and 'j,' initialized in two.c, are not visible in file one.c. This example underscores that the visibility of globally declared static variables is limited to the file in which they are declared.
Note: In the above example, the function 'sum,' which was declared and defined in two.c, also has the storage class 'extern.' This allows us to call it from another file (one.c). If it were static, calling the function 'sum' would not be possible, as the static storage class confines visibility to the file where it is declared.
(8)If
we static variable has declared locally or globally its scope will always whole
the program. For example:
(a)
//locally declaration of static variable
#include<stdio.h>
void visit();
int main(){
int i=0;
{ //Opening inner block
static int a=5; //locally declaration
XYZ:; //Label of goto statement
printf("%d ",a);
a++;
i++;
} //closing inner block.
visit();
/* printf("%d",a); Variable a is not visible here but
it
is alive. */
if(i<5)
goto XYZ;
return 0;
}
void visit(){
}
Output:
5 6 7 8 9
Explanation: When the program control exits the inner block where variable 'a' is declared, the variable 'a' is not visible outside of that block, but its scope persists throughout the program. If, using a goto statement, control returns to the inner block, it prints the previously incremented values. This behavior is achievable with static variables, unlike auto or register variables, which lose their values when exiting their scope.
(b)
//Locally
declarations of variable
There
are two c source code files:
//one.c
#include<stdio.h>
#include<conio.h>
void main(){
int i;
for(i=0;i<3;i++){
{
static int a=5;
printf("%d\n",a);
a++;
}
visit();
}
getch();
}
//two.c
#include<stdio.h>
void visit(){
printf("Don’t disturb, I
am learning storage class");
/* printf("%d",a); Variable a is not visible here but
It
is alive. */
}
Now
compile and execute both files together:
Output:
5
disturb,
I am learning storage class
6
disturb,
I am learning storage class
7
disturb,
I am learning storage class
Explanation:
When control goes to another file and comes even that variable didn’t dead and
it prints previous incremented value.
Note: In
both examples if you will declare static variable globally you will get same
output.
9.
A static variables or functions have internal linkage. An internal linkage
variables or functions are visible to the file where it has declared.
8 comments:
good for practice,thanks for posting
its goog
Really good one
Good collection..
In question 11, example 3(a) ans should be 0.
Good question thank you
Very good.
very good describe about negative integers
Post a Comment