Solution for Exercise 16-6
ex1606
#include <stdio.h> void proc(void); int main() { puts("First call"); proc(); puts("Second call"); proc(); return(0); } void proc(void) { static int a; printf("The value of variable a is %d\n",a); printf("Enter a new value: "); scanf("%d",&a); }
Output
First call
The value of variable a is 0
Enter a new value: 6
Second call
The value of variable a is 6
Enter a new value: 5
Notes
* A static variable's contents are retained after its function quits. It might seem like all variables should be declared that way, but memory was tight on computers when the C language was developed.
* Variables declared as static
are initialized to zero. This is a compiler feature. It explains why the value you see in the output is zero, and not garbage, as well as why the compiler doesn't generate a warning about using an uninitialized variable.
Copyright © 1997-2024 by QPBC.
All rights reserved