Technology Programming

How to Declare a Static Variable in C

Instructions

Declare a Static Variable

1

Know that static variables are always declared inside a C function, but unlike other variables, their values continue to exist and are retained, even after the function exits.
2

Declare a static variable by using the same syntax as you would to declare a normal local variable, but precede the declaration with the word static, like this:

static int sum = 0;
3

Expect initialization to happen only the first time you call the function. Subsequent times, the previous value will still be there. If you omit the initialization, it will automatically be initialized to 0.
4

Use the variable in the function like you would any other.
5

Remember that, like any other local variable, a static variable cannot be referred to outside the function. However, if you pass out a pointer to it, the pointer can be dereferenced successfully, since the variable still exists.

Know When to Use Static Variables

1

Use a static variable to allow your function to have its own memory that carries over from one call to another. For example, a function that gets and parses the next line of a file might need to internally keep track of where it is in the file.
2

Use a static variable as a way to provide a piece of memory for storing a result. For example, a function to concatenate strings might use a static variable in which to store the result of the concatenation and return a pointer to it. The static variable's memory is constantly available, but will automatically be freed when the program ends, just like any other local variable.
3

Use static variables for a running total or similar accumulation. Consider this example:

int running_total(int num) {
static int sum = 0;
sum += num;
return sum;
}Each time you call this function, it keeps and returns a running total of all numbers passed into it.
SHARE
RELATED POSTS on "Technology"
Blitzerwarner: Could You Escape the Speed Cameras?
Blitzerwarner: Could You Escape the Speed Cameras?
What Makes a Perfect Web Design Company?
What Makes a Perfect Web Design Company?
Inside No-Fuss Products In Gaming Computers
Inside No-Fuss Products In Gaming Computers
Gaming Institute, Game Design Schools at PAI-ILS, PAI International Learning Solutions Pune
Gaming Institute, Game Design Schools at PAI-ILS, PAI International Learning Solutions Pune
Free Music Players For Websites
Free Music Players For Websites
'See How NCP Tracks Down Fake Ration Card Holders'
'See How NCP Tracks Down Fake Ration Card Holders'
You Can Stop Panic Attacks in Just two Minutes
You Can Stop Panic Attacks in Just two Minutes
WordPress Blog to WP Site! A Journey to Online Success
WordPress Blog to WP Site! A Journey to Online Success
Videos will give your business portal an extra leverage
Videos will give your business portal an extra leverage
Excellent Ways To Make Weight Loss Work For You
Excellent Ways To Make Weight Loss Work For You
Five Notable Differences between National and International Logo Designs
Five Notable Differences between National and International Logo Designs
Who Should Your Phoenix Web Design Client Be?
Who Should Your Phoenix Web Design Client Be?
White Papers & Other Documents - including ALFLB
White Papers & Other Documents - including ALFLB
Google Analytics Tips
Google Analytics Tips
Want To Make Your Website Faster? Follow These Steps
Want To Make Your Website Faster? Follow These Steps
The Starting of Couture Apparel
The Starting of Couture Apparel
Choose Custom Website Design Services
Choose Custom Website Design Services
Choosing the Right Joomla CMS Developer
Choosing the Right Joomla CMS Developer
A Look at SQL Server Denali AlwaysOn
A Look at SQL Server Denali AlwaysOn
4 Tips to Choose the Perfect Flash Animation Course
4 Tips to Choose the Perfect Flash Animation Course

Leave Your Reply

*