1

I asked some days ago

     #include <stdio.h>
     
     int MyAdd(int);
     main ()
     {
          int i;
          int c = 0;
          c = MyAdd(5); 
          printf("%d\n", c);
     }
     
     int MyAdd(int a)
     {
          if(a > 0)
               return a + MyAdd(--a);
          else
               return 0;
     }

why the result of this is 10 and not 15 and i get an answer

When used in expressions, side effect operators do funny, unexpected things, because you're basically at the mercy of the compiler.

In this case, your compiler is evaluating the second operator of a + MyAdd(--a) before the first one. So, you're decrementing the variable before using it in the addition.

Understood it! I was just playing with my code and I replace the --i with i--, my compiler didn’t get any error and when I run it I got Segmentation fault (core dumped) and i am trying to understand why this happened.

Community
  • 1
  • 1
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
  • 1
    One sound advice would be to never use the `++` and `--` operators as part of an expression where there are other operators. Another such advice is that recursion is a dangerous, superfluous feature that shouldn't be used in 99% of all the cases you can come up with. – Lundin Nov 18 '13 at 11:51

4 Answers4

2

1)

"I got Segmentation fault (core dumped)"

In this code you see that each time when MyAdd function is being called, you pass to it a smaller argument (because of --a):

#include <stdio.h>

     int MyAdd(int);
     main ()
     {
          int i;
          int c = 0;
          c = MyAdd(5); 
          printf("result: %d\n", c);
     }

     int MyAdd(int a)
     {
          int  res = 0;

          if(a > 0) {
            res += a + MyAdd(--a);
            printf("%d\n", res);
            return res;
          } 
          else
               return 0;
     }

The output:

0     // you pass 1 to your function and decrement the argument
1     // you pass 2
3     // you pass 3
6     // you pass 4
10    // you pass 5. Because you decrement the argument each time
      // you pass it to your function, this will return normal result.
result: 10

But if you change --a to a--, you will always pass 5 to MyAdd function. This will couse infinite loop because: (5 + (5 + (5 + (5 + .... As you can see, 5 is always more than 0, so if will always call MyAdd function recursively...

So the return value will increase and increase until overflow happens:

The ISO C99 standard says that an integer overflow causes "undefined behaviour", meaning that compilers conforming to the standard may do anything they like from completely ignoring the overflow to aborting the program. Most compilers seem to ignore the overflow, resulting in an unexpected or erroneous result being stored.

2)

"why the result of this is 10 and not 15"

When you call MyAdd first time, you should better take into consideration the argument you pass to your function. Then you'll get 15 instead of 10:

#include <stdio.h>

     int MyAdd(int);
     main ()
     {
          int i;
          int c = 0;
          c = MyAdd(5); 
          printf("result: %d\n", c);      
     }

     int MyAdd(int a)
     {
          int  res = a;    // here it is

          if(a > 0) {
            res +=  MyAdd(--a);
            printf("%d\n", res);
            return res;
          } 
          else
               return 0;
     }
yulian
  • 1,601
  • 3
  • 21
  • 49
1

You have to do the decrement part before doing the function call. If you don't, all you get is (5 + (5 + (5 + (5 + ..., and ultimately, a stack overflow and subsequent segfault.

João Mendes
  • 1,719
  • 15
  • 32
1

It is a recursive function, decrement only happens when it is back in the calling function. In this case i-- . So i will always be > 5.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Gabson
  • 421
  • 1
  • 4
  • 20
1

Sticking the -- before the variable decrements the value and returns it (hence your function gets a smaller argument each pass).

Sticking the -- after the variable returns the current value, then decrements (hence your function repeatedly passes the same value to itself: the value isn't decreased until the call returns, which it won't because it recursively has the same problem).

Eventually, you fill up the stack with function calls to yourself, and the program crashes (the stack is how the program knows where to "return to": it adds the return address to the stack each time you enter a function, and removes the address each time we come back. We never come back, so eventually we run out of space).

KidneyChris
  • 857
  • 5
  • 12
  • And by "returns", I mean that the -- operator returns, not MyAdd. Whilst not strictly correct, you can visualise the operators in C as being implied functions that take arguments: you already saw this with "+" which takes two arguments (and is free to evaluate them in any order the compiler likes). The tricky part is that --(something) and (something)-- are **different** operators that do different things. – KidneyChris Nov 18 '13 at 11:30