2

Why the value of static variable a is not incrementing even after return statement?

#include<stdio.h>
int p(int x);
int main()
{   
  int r=p(4);
  printf("%d",r);
  return 0;
}

int p(int x)
{
  static a=1;
  printf(" x = %d,a= %d\n",x,a);

  if(x<=1)
  {
    return 1;
  }
  else
  {
    printf(" x = %d,a= %d\n",x,a);    
    return p(x-1)+x+a++;
    printf(" x = %d,a= %d\n",x,a);    
  }
}

Here, when I print static variable a then all time its value is 1. But according to the theory I read, it should be incremented after each recursive call.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
codie
  • 19
  • 1

3 Answers3

2

The order of evaluation of expression p(x-1) + x + a++ is unspecified. We know that all three operands are evaluated before the sum is evaluated, but we do not know in what order they are evaluated (it is up to the compiler). In your case, the compiler first evaluates p(x-1), so the value of a in the recursive function calls is always 1 and changes only after your program returns from all recursive calls.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/137845/discussion-on-answer-by-dyz-static-variable-doesnt-increment-in-this-code-using). – Bhargav Rao Mar 11 '17 at 18:32
1

The incrementing function returns before post-increment of variable a. Logically, the second printf statement should not print.

hmatar
  • 2,437
  • 2
  • 17
  • 27
  • NMDV, but I suppose it was DV'd for not being clear that the `printf()` preceding the `return` would not be expected ever to print the updated value of `a` anyway. (As in, the downvoter probably did not recognize that this actually answers the question.) – John Bollinger Mar 11 '17 at 06:36
  • @JohnBollinger This answer is not correct. The function call and the evaluation of a++, and indeterminately sequenced. The order is unspecified and behavior defined. See a very similar example here: https://stackoverflow.com/questions/41775973/is-this-undefined-behaviour-in-c-if-not-predict-the-output-logically/41777679#41777679 and here: https://stackoverflow.com/questions/40532404/is-calling-a-function-with-local-side-effects-twice-in-the-same-expression-undef/40532729#40532729 – 2501 Mar 11 '17 at 06:48
  • @2501, *this* answer has nothing to do with the sequencing of a++ relative to the function call. It points out that the `printf()` that is actually executed occurs before either one, in every recursive call, so only in the sense that the program's overall behavior is undefined could it ever see a value for `a` different from 1. – John Bollinger Mar 11 '17 at 06:54
1

First notice that code after a return is never executed. So this code block:

printf(" x = %d,a= %d\n",x,a);    
return p(x-1)+x+a++;
printf(" x = %d,a= %d\n",x,a);  <--- will never be executed!!

is really the same as

printf(" x = %d,a= %d\n",x,a);    
return p(x-1)+x+a++;

When you find the behavior of a piece of code strange/difficult to understand, it is often a good idea to rewrite complex expressions into a number of simple expressions.

This statement:

return p(x-1)+x+a++;

can be rewritten as:

int sum = 0;
sum = sum + p(x-1);  // Here you call the function again
sum = sum + x;
sum = sum + a;
a = a + 1;           // Here you increment a
return sum;

When you look at the rewritten code, it becomes clear that the recursive call of p happens before the increment of a. Consequently all prints of a is before the increment and will therefore print 1.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63