Saturday, April 28, 2012

7.14

The following program prints two integer values; the first value typically is garbage ( or possibly 0, if you are executing inside a debugger or other controlled environment), but the second value might be 2. Explain why.


#include  <stdio.h>


void p(void)
{
int y;
printf("d\n", y);
y = 2;
}

int main()
{
p(); p();

return 0;
}

The first value will be garbage or 0 because y has been instantiated but not initialized.  The second value printed may be 2 because the value of y has by then been initialized to 2, and so may keep its value for the second printf.

No comments:

Post a Comment