#include <stdio.h>
int next_int() {
static int counter = 0;
counter++;
return counter;
}
int main() {
int i;
for (i = 0; i < 8; i++)
printf("%i\n", next_int());
}
% cc -o static -Wall static.c
./static
1
2
3
4
5
6
7
8
| # static.py: static counter in a function
import sys
def next_int(): # for temp variables
next_int.count += 1
return str(next_int.count)
next_int.count = -1 # define attribute
def my_incr(): # for temp variables
if not hasattr(my_incr, "counter"): # !exist init
my_incr.counter = 35*8 # define attribute
my_incr.counter += 8
return str(my_incr.counter)
for i in range(0,8):
sys.stdout.write(next_int() + " ")
sys.stdout.write(my_incr() + "\n")
% python static.py
0 288
1 296
2 304
3 312
4 320
5 328
6 336
7 344
|