0

In this program, when p() is called for the first time its prints 0 (I realize it could just print garbage on some systems).

But the second time p() is called it prints 2 even though y gets declared again. It seems to keep the value that y was when p() was first called.

I'm very confused as to why. Can someone help explain? And also how I can modify the main function to make it not do this?

void p ()
{
 int y;
 printf ("%d ", y);
 y = 2;
}
void main ()
{
 p();
 p();
} 
  • 3
    You are printing an unintialised variable. That is Undefined Behaviour and you cannot expect any particular outcome. Some reading for you: [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) and [Does “Undefined Behavior” really permit *anything* to happen?](https://stackoverflow.com/questions/32132574/does-undefined-behavior-really-permit-anything-to-happen) – kaylum Nov 19 '15 at 02:25
  • It's not clear what you actually want it to do. Just set `y` to be whatever value you want before printing it. And the last statement in `p` is useless as `y` is a local variable and thus setting it has no meaning unless it is to be used later in that same function. – kaylum Nov 19 '15 at 02:30
  • This is something you should ignore, not worry about. If you call the same function back-to-back, then of course there's a good chance it could pick up the old value, since it will be looking for it in the same place. But you can't rely on that. – Tom Karzes Nov 19 '15 at 02:42
  • Related: http://stackoverflow.com/questions/24113478/two-integer-variables-residing-at-one-memory-address – R Sahu Nov 19 '15 at 02:47
  • @RSahu Thank you this explained exactly what I was trying to figure out. – Dominik Figueroa Nov 19 '15 at 03:45

2 Answers2

0

http://en.cppreference.com/w/c/language/storage_duration#Storage_duration

The easiest way is to change storage duration or your variable. But alternative is to use function parameter and return value to transfer the state information. The static storage duration can be problematic in multithreaded programs which makes alternative solution often more attractive.

EDIT: Right. I read and understood the question completely opposite to what was asked. The comment about uninitialized values are correct answer. Meaning that without initialization the variable has a random value that was left from some random previous operation. In this case that random value happens to be same variable from previous call to same function.

Community
  • 1
  • 1
Pauli Nieminen
  • 1,100
  • 8
  • 7
  • Ohhhh this post cleared it up. I didn't think about the random value being in the same place both times. And since that memory address gets changed at the end of the first function call then it changes what the random value is going to be in the second function call. – Dominik Figueroa Nov 19 '15 at 03:47
0

Most of C compiler store local variable in stack. you code behavior is like following.

-> first time call p()

1. **int y;** push y in top of stack. (here value y is 0 or garbage according to compiler because it take value of is memory location).
2. **printf ("%d ", y);** print value of y location.
3. **y = 2;** change value of y location with 2.
4. end of function scope of y finish ( i assume you know a variable scope in c) and pop from stack but not reset value of that location.

-> second time call p()

1. **int y;** push y in top of stack (memory location allocated of variable y is same as first call p() memory allocation of y so that here value y is 2 because in first call we set value 2 in this location)
2. **printf ("%d ", y);** print value of y location. 

that`s why here 2 print in second call p().

for your reference see the following code, i print value and memory address of variable in this code.

void p ()

{

int y ;

printf ("value of y = %d \n", y);

printf ("address of y = %p \n", &y);

y = 2;

}

void q () {

int x ;

printf ("value of x = %d \n", x);

printf ("address of x = %p \n", &x);

x = 8;

}

void main ()

{

p();

q();

p();

}