3

I think I will get 12, not 7. w++, then w will be 4, which is 100, and w++, w will be 8, 1000; so w++|z++ will be 100|1000 = 1100 will be 12.

what's wrong with me?

int main()
{
    int  w=3, z=7; 
    printf("%d\n", w++|z++);
}
Yamaneko
  • 3,433
  • 2
  • 38
  • 57
user1279988
  • 757
  • 1
  • 11
  • 27

6 Answers6

3

You are misunderstanding the postfix ++ operator. The value of the variable is used before the variable is incremented. Your analysis would be correct for the prefix ++ operator, as in ++w|++z.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

The problem is that by using w++|z++, you're first using the value of w and ORing that by the current value of z, then incrementing each. Use ++w|++z instead, and the numbers will first be incremented, then used.

int main()
{
    int x = 10;

    // prints 10
    printf("%d\n", x++);
    // prints 11
    printf("%d\n", x);

    x = 10;
    // prints 11
    printf("%d\n" ++x);
    // prints 11
    printf("%d\n" x);
}

The same can be done with --x and x--. For more information, see this relevant question.

Community
  • 1
  • 1
Lander
  • 3,369
  • 2
  • 37
  • 53
2

Those are post increment operators; they take effect after the operation, so 3 and 7 are used in the operation.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Actually, `w` and `z` _are_ already `4` and `7` when `w++|z++` is used, it just happens that `w++` and `z++` evaluate the the old values of `w` and `z`. – Mankarse Mar 20 '12 at 04:55
1

x++ increments x, but it evaluates to the old value of w.

So w++|z++ evaluates to 3|7 (which happens to be 7 on your implementation), and increments w and z as a side effect.

If you wanted the behaviour that you were expecting, you could use the prefix operator ++x, which increments its x and evaluates to the new value of x.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
1

You are doing post-increment (i++) which takes the value of i first, then only increases it's value.

If you want to achieve what you said in your question, do this: ++w | ++z

Vishnu Haridas
  • 7,355
  • 3
  • 28
  • 43
0

You have probably misunderstood the post-increment operator which is very common among the beginners, so don't worry. Over time you'll get it right.

Take a look at the word post-increment. There is a post word in it which generally signifies after. It means that the increment will happen after everything else has been executed. This is how I used to remember it.

So, if you take a look at your program now -

int main()
{
    int  w=3, z=7; 
    printf("%d\n", w++|z++);
}

then it will become clear that after the printf function itself has been executed, the increment will happen. So you will get the value of w and z as 3 and 7, respectively in the evaluation of the second argument expression of printf.

The official C++11 standard, (§5.2.6, final version) says -

The value of a postfix ++ expression is the value of its operand. [Note:the value obtained is a copy of the original value — end note]

So that means the value of the postfix w++ expression, is the value of the operand itself, that is, the value of the w, which is 3 and the value of the second expression z++ will be 7. These values will then be used in the calculation of 3|7 and after that, the variables will be incremented.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178