Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

9.3. static

static variables are variables that don't get deleted when they fall out of scope, they are permanent and retain their value between calls to the function. Here's an example:

Example 9-1. list_squares.c

#include <stdio.h>

int get_next_square(void);

int
main()
{
  int i;

  for(i = 0; i < 10; i++)
    printf("%6d\n", get_next_square());

  printf("and %6d\n", get_next_square());

  return 0;
}

int
get_next_square()
{
  static int count = 1;

  count += 1;

  return count * count;
}
     
This will list the squares of the numbers from zero to ten. Zero to nine are printed by the loop and the square of ten is printed afterwards just to show it still has it's value.

 
 
  Published under the terms of the GNU General Public License Design by Interspire