Vous êtes sur la page 1sur 5

Static Variables in C

Static variables have a property of preserving their value even after they are out
of their scope!Hence, static variables preserve their previous value in their
previous scope and are not initialized again in the new scope.
Syntax:

static data_type var_name = var_value;

Following are some interesting facts about static variables in C.

1) A static int variable remains in memory while the program is running. A normal
or auto variable is destroyed when a function call where the variable was declared
is over.

For example, we can use static int to count number of times a function is called,
but an auto variable can�t be sued for this purpose.

For example below program prints �1 2�

filter_none

edit

play_arrow

brightness_4

#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}

Output:

1 2

But below program prints 1 1


filter_none

edit

play_arrow

brightness_4
#include<stdio.h>
int fun()
{
int count = 0;
count++;
return count;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}

Output:

1 1

2) Static variables are allocated memory in data segment, not stack segment. See
memory layout of C programs for details.

3) Static variables (like global variables) are initialized as 0 if not initialized


explicitly. For example in the below program, value of x is printed as 0, while
value of y is something garbage. See this for more details.
filter_none

edit

play_arrow

brightness_4
#include <stdio.h>
int main()
{
static int x;
int y;
printf("%d \n %d", x, y);
}

Output:

0
[some_garbage_value]

4) In C, static variables can only be initialized using constant literals. For


example, following program fails in compilation. See this for more details.
filter_none

edit

play_arrow
brightness_4
#include<stdio.h>
int initializer(void)
{
return 50;
}

int main()
{
static int i = initializer();
printf(" value of i = %d", i);
getchar();
return 0;
}

Output

In function 'main':
9:5: error: initializer element is not constant
static int i = initializer();
^

Please note that this condition doesn�t hold in C++. So if you save the program as
a C++ program, it would compile \and run fine.

5) Static global variables and functions are also possible in C/C++. The purpose of
these is to limit scope of a variable or function to a file. Please refer Static
functions in C for more details.

Related Articles:

Static Keyword in C++


Quiz on Static Keyword
Static data members in C++
When are static objects destroyed?
Interesting facts about static member functions
Can static functions be virtual?
Comparison of static keyword in C++ and Java
Static functions in C

Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.

Recommended Posts:

Initialization of static variables in C


How are variables scoped in C - Static or Dynamic?
What are the default values of static variables in C?
Initialization of global and static variables in C
Static functions in C
C++ | Static Keyword | Question 6
C++ | Static Keyword | Question 5
C++ | Static Keyword | Question 2
C++ | Static Keyword | Question 4
C++ | Static Keyword | Question 3
When are static objects destroyed?
Can static functions be virtual in C++?
C++ | Static Keyword | Question 1
Static data members in C++
�static const� vs �#define� vs �enum�

Article Tags : C
C Basics
C-Storage Classes and Type Qualifiers
Practice Tags : C

thumb_up
3
2

Based on 37 vote(s)
Please write to us at contribute@geeksforgeeks.org to report any issue with the
above content.
Post navigation
Previous
first_page Data Types in C
Next
last_page
C | Operators | Question 27

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share
the link here.

Most popular in C

Commonly Asked C Programming Interview Questions | Set 3


How will you print numbers from 1 to 100 without using loop? | Set-2
time() function in C
Loader in C/C++
Dividing a Large file into Separate Modules in C/C++, Java and Python

More related articles in C

Different ways to Initialize all members of an array to the same value in C


Pre-increment and Post-increment in C/C++
getopt() function in C to parse command line arguments
Difference between Call by Value and Call by Reference
How to Read and Print an Integer value in C

Most visited in C

How to find Segmentation Error in C & C++ ? (Using GDB)


Interesting facts about C Language
strrchr() function in C/C++
Program to copy the contents of one array into another in the reverse order
How to avoid Structure Padding in C?
Communication between two process using signals in C
Find the Nth term of the series 14, 28, 20, 40,.....
Program to check if two strings are same or not
Why strcpy and strncpy are not safe to use?
Measure execution time with high precision in C/C++
C program to store Student records as Structures and Sort them by Name
strlen() function in c
iswupper() function in C/C++
Code Optimization Technique (logical AND and logical OR)
Difference between C and C++

Advertise Here

Vous aimerez peut-être aussi