-6

in the below code i am trying to add two elements of array with the increment operator, but i am getting the output sum to be wrong. kindly help me if i made any mistake in the code

#include <stdio.h>
int main(void) {
int a[2];
int top=-1;
a[++top]=10;
a[++top]=20;
printf("a0 is %d \n",a[0]);
printf("a1 is %d \n",a[top]);
printf("value of sum is %d \n",a[top]+a[--top]);
}

the output of last line should be 30 as i and summing the two values in a array. but the out put i get is as follows

a0 is 10 
a1 is 20 
value of sum is 20 
chris
  • 60,560
  • 13
  • 143
  • 205
  • 4
    [*warning: unsequenced modification and access to 'top' \[-Wunsequenced\]*](http://coliru.stacked-crooked.com/a/0235e7909cd2da0d) – chris Sep 05 '15 at 04:17
  • 5
    STEP AWAY FROM THE PRE/POSTINCREMENT OPERATORS. NOW. – Ignacio Vazquez-Abrams Sep 05 '15 at 04:19
  • 1
    Again, the C specification should require that compliant compilers detect this garbage, output an extraordinarily nasty error message, and abort the compilation. That saves the OP from being baffled by his own ... – user3386109 Sep 05 '15 at 04:53
  • I just copied and pasted the code in the question and compiled it, and I could not reproduce the OP's problem. I got 30 as the final sum. Does the issue in the question vary between compilers and versions of `C`? I used `gcc 4.8.1` with the `-std=c99` option. – Sharif Sep 05 '15 at 05:10

4 Answers4

4

Your program has unspecified behavior. In the expression a[top]+a[--top], the C and C++ language standards do not specify which of a[top] and a[--top] will be evaluated first, and they don't specify when the -- operator is executed relative to other expressions. Compilers can evaluate this as they see fit. Yours is computing --top first, setting top to 0, and then is calculating a[0]+a[0], yielding 20.

Don't use a variable twice in an expression in which you pre- or post- increment or decrement it.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
1

To understand why this is happening, change your last printf to print out top and the sum in one line, like this:

printf("top is %d and the value of the sum is %d \n",top,a[top]+a[--top]);

This should make it apparent that the pre-increment operation is not happening at the point in execution that you think it is.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
1

Try to calculate and print

int y1 = a[top]; 
int y1 += a[--top]; 

top = 1;
int y2 = a[top] + a[--top];

this could help you. The point is that the execution of a[--top]; in your code is done before the execution of a[top] because they are not specified.

jimifiki
  • 5,377
  • 2
  • 34
  • 60
-1

Here, in this line:

printf("value of sum is %d \n",a[top]+a[--top]);

Due to --top, top becomes 0 again (--top is --1). Then a[0] and a[0] are added, and it does not give expected output. I think it's due to high precedence order of -- operator.

As has been said already, it's better to calculate separately and then print.

PS: Please tell me if anything of the above is wrong or not fully correct. I like to learn from mistakes.

  • 1
    Precedence isn't relevant here ... it only applies when operators are adjacent at the same level of expression, e.g., in `expr+expr*expr`, the precedence determines that it is evaluated as `expr+(expr*expr)` rather than `(expr+expr)*expr`. It the OP's case, the language simply doesn't specify when `--` is evaluated relative to the evaluation of `a[top]` ... if it happens before, you get 20. If it happens after, you get 30. A conforming implementation can do it either way. – Jim Balter Sep 05 '15 at 06:00
  • To my understanding, the `--` is evaluated before a statement if it's a preincrement operator and after a statement if it's a postincrement operator. In OP's case, it's a preincrement operator and therefore, it's evaluated before the `printf` –  Sep 05 '15 at 06:08
  • @NoobCoder Your understanding is completely incorrect. Pre vs. post only determine the value of the primary expression they appear in (e.g., if x is 10, the value of ++x is 11 and the value of x++ is 10); they have no bearing on when the operator is evaluated. See my answer, and the "Undefined behavior and sequence points" link at the top of the page. – Jim Balter Sep 05 '15 at 20:39
  • @NoobCoder P.S. If you had read the comments under your own answer, you would know that your understanding couldn't possibly be correct. – Jim Balter Sep 05 '15 at 20:49
  • 1
    Okay, I see. So, it is a compiler-based issue ultimately? – Anirudh Khanna Sep 06 '15 at 07:06