Solution for Exercise 16-5

ex1605

#include <stdio.h>

void proc(void);

int main()
{
    puts("First call");
    proc();
    puts("Second call");
    proc();
    return(0);
}

void proc(void)
{
    int a;

    printf("The value of variable a is %d\n",a);
    printf("Enter a new value: ");
    scanf("%d",&a);
}

Notes

* It's entirely possible that the results you see reflect that the value of variable a is retained for both calls. That result is merely a happy coincidence; never rely on such a thing happening for your code. Ever!

* Yes, this code commits the sin of using a variable before it's initialized (in the proc() function). It's fortunate that the value of variable a isn't being used to calculate the amount of fuel required to fly over the ocean. The point of the exercise is to show how the function discards the value once it's been set (at Line 20).

* The compiler warning error you may see may remind you that variable a is uninitialized before it's used.

* By the way — I swear by Apollo's golden crown — the uninitialized value I see when I run this program is really zero. Your computer may show a more expected "garbage" value, such as 1964477653. That's okay.