Name
Address
Type
Lifetime
Value
Scope
The lifetime of a variable is the time during which it is bound to a particular memory cell.
Categories of Variables by Lifetimes (1) Static variables: bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. int f(int x) { static int a = 0; a += 2; printf("%d\n",a); } f(2) --> 2 f(4) ---> 4
#include
int sum;
int fun1(int a, float b)
{
static int cnt;
int i, j;
…
}
- Disadvantage: lack of flexibility (no recursion).
- lifetime = entire program execution.
e.g., C and C++ static variables
int f(int x) {
static int a = 0
a += 2;
printf("%d\n",a); }
f(2) --> 2 f(4) --->4
Share with your friends: |