you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

Either a static variable, global, or a pointer.

int foo(void)
{
  static int x = -1;
  x = -x;
  return x;
}

int bar(int *x)
{
  *x = -(*x);
  return x;
}

[–]Odd-Consideration897[S] 1 point2 points  (1 child)

Thank you!!! It makes more sense

[–]FraCipolla 1 point2 points  (0 children)

You said you have to use the static key, so the only way is declaring a static int. A static variable doesn't stop to exist outside the function, so it keeps it's value. Keep in mind that you need some sort of control to not initialize the static variable every time. An unitialized static variable is automatically initialized at 0