Friday, April 13, 2012
Static storage class in C
Static storage class will be used when the value of the variable should be persistent between the function calls. default value for the static variable is zero. And scope of the variable or function is limited to the file only.
- Keyword: static
- Default value: zero if not initialised .
- Memory Location: Main memory
- Scope: Local to the function or block
- Lifetime: till the program or application completes
int test() { int a=10; // local to test() function printf("a value is %d\n",a); a++; } int static_test() { static int st=10; // stativ variable, value will be restored printf("st value is %d\n",st); st++; } main() { printf("************************** non-static value **********************************\n"); test(); test(); test(); printf("************************** static value **********************************\n"); static_test(); static_test(); static_test(); }Output:
**************** non-static value **********************************
a value is 10
a value is 10
a value is 10
************************** static value **********************************
st value is 10
st value is 11
st value is 12
From the above sample code, it is clear that static variable in static_test() function is restoring the value by incrementing one, where as in test() function value is always same.
Static Initialization: Static variable initialises only one time in whole program. And it restores the new value when changes. See the sample code below.
main() { for(int j=0;j<5;j++) { static int i=10; // this executes only once, it wont execute for each iteration printf("i is %d\n",i++); } }Output:
i is 10
i is 11
i is 12
i is 13
i is 14
In the above sample code, static integer variable initialised in for loop. Because of static property, it wont initialise for each iteration. for auto variables it will initialise for each iteration.
Scope Limitation: Scope of the variable is limited to the file if you declare it as static. see the sample code below.
// file name is file1.c and code is below static int i=10; // i is static, so its scope is limited to this file only main() { print(); } // file name is file2.c and code is below #includeextern int i; // i is extern, so i value is somewhere in ohter file extern void print(); void print() { printf("i is %d\n",i); // will get error here, because in file1.c i is static }
Output with static:
/tmp/ccwXbAno.o: In function `print':
file2.c:(.text+0x6): undefined reference to `i'
Output without static:
i is 10
In the above code, there are two files file1.c and file2.c. static variable i is declared and initialised in file1.c, and trying to access in file2,c in print function. at this time we will get the error, because of declaring i in file1.c as static. if the variable is not static we will get the output shown above.
P.S: Static keyword behaviour varies in C++. Click here for static behaviour in C++.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment