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); }
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. That's 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 unaffiliated variable.
Copyright © 1997-2024 by QPBC.
All rights reserved