-1

I was searching on Stack Overflow for the answer to this question but I haven't found an exact answer. I came up with this code. I know how operators are supposed to work but I don't understand them in this kind of problem. For example, in the first case, how can z and y still be 1 if there I am using ++y and ++z?

#include <stdio.h>

int main(void) {
    int x, y, z;

    x = y = z = 1;
    ++x || ++y && ++z;
    printf("x = %d y = %d z = %d\n", x, y, z);

    x = y = z = 1;
    ++x && ++y || ++z;
    printf("x = %d y = %d z = %d\n", x, y, z);

    x = y = z = 1;
    ++x && ++y && ++z;
    printf("x = %d y = %d z = %d\n", x, y, z);

    x = y = z = -1;
    ++x && ++y || ++z;
    printf("x = %d y = %d z = %d\n", x, y, z);

    x = y = z = -1;
    ++x || ++y && ++z;
    printf("x = %d y = %d z = %d\n", x, y, z);

    x = y = z = -1;
    ++x && ++y && ++z;
    printf("x = %d y = %d z = %d\n", x, y, z);

    return 0;
}

As results i get:

x = 2 y = 1 z = 1
x = 2 y = 2 z = 1
x = 2 y = 2 z = 2
x = 0 y = -1 z = 0
x = 0 y = 0 z = -1
x = 0 y = -1 z = -1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Žiga Gazvoda
  • 173
  • 1
  • 9
  • 1
    So megadup:( http://stackoverflow.com/questions/17432730/precedence-of-over – Martin James Feb 13 '16 at 17:08
  • Possible duplicate of [Why does "++x || ++y && ++z" calculate "++x" first, even though operator "&&" has higher precedence than "||"](https://stackoverflow.com/questions/3700352/why-does-x-y-z-calculate-x-first-even-though-operator-ha) – phuclv Aug 18 '18 at 11:21

3 Answers3

3

This is a result of the evaluation of the logical expressions: as soon as it has been determined that an expression is false (or true), the remaining operators are not evaluated anymore. E.g.:

++x || ++y && ++z;

As x is one, the expression will be true independent of what z or y are, so ++y and ++z are not performed anymore.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 3
    So? That does not mean `++x && ++z` will be evaluated _first_. – Paul Ogilvie Feb 13 '16 at 16:29
  • See what you got on `++x && ++y || ++z;` ;-) – Frankie_C Feb 13 '16 at 16:31
  • Second example: `x = y = z = 1; ++x && ++y || ++z;` — Since `++x` is true, `x` is 2 and the RHS of `&&` is evaluated; since `++y` is true, `y` is 2 and the RHS of `||` does not get evaluated. So, the output is `x = 2, y = 2, z = 1`. – Jonathan Leffler Feb 13 '16 at 16:37
  • Jonathan, interesting note to make: the `++` on `x` will _always_ be performed. If `x` was `0`, it will now be `true` (1). – Paul Ogilvie Feb 13 '16 at 16:40
  • Does that happen because ++ on x has greater priority than && that comes next. So if x was 0, the && would stil execute because x woud become 1? – Žiga Gazvoda Feb 13 '16 at 16:43
  • Yup; the increment is always performed; the only time the `++y` will not occur is if `x = -1` initially; then `++x` is `0` or false and the RHS will not be evaluated. – Jonathan Leffler Feb 13 '16 at 16:44
  • Yes; the pre-increment has a very high priority, much higher than the logical operators. – Jonathan Leffler Feb 13 '16 at 16:44
  • Ziga, see it as the evaluator starts at the left (there is no higher level operator to be evaluated first). It must increment `x` _before_ evaluating `x` as the `++` on the left is the prefix increment operator. So `x` is now `1`. Now the evaluator evaluates `x` and sees it is `1`, hence `true`. – Paul Ogilvie Feb 13 '16 at 16:47
  • Does - cause number to be false or is it only 0 that means false statement? – Žiga Gazvoda Feb 13 '16 at 16:47
  • 1
    Ziga, please read a book on C. It is all spelled out there. FYI: yes, only `0` means `false`. – Paul Ogilvie Feb 13 '16 at 16:48
  • I am reading one curently, but doesnt exactly explain operators very well. – Žiga Gazvoda Feb 13 '16 at 16:49
  • one last question. What happens to fist x in x = x || y++ if x is 5 and y is 6. – Žiga Gazvoda Feb 13 '16 at 16:59
  • Ziga, that is an assignment: `x = (x || y++) `. Is that what you mean? Well, as `x` is 5, hence `true`, `y++` is not evaluated. `x` will finally be assigned a non-zero value, but that can be `1`, or any other non-zero value. That is because you assign the result of a Boolean expression to `x`. – Paul Ogilvie Feb 13 '16 at 17:04
  • So the first x becomes whatever the result on the other side of = is. – Žiga Gazvoda Feb 13 '16 at 17:07
  • Yes, with the addition that "the other side" is a Boolean value that is either `true` (any integer value that is not zero) or `false` (integer value `0`). So x could well be 6328471, or -16885, or 5, or ... – Paul Ogilvie Feb 13 '16 at 17:09
  • But why in z = x++ - y / x-- ; if x is 20 y is 30 and z is 10, x doesnt change at all and if usage of printf it shows the value of 20. – Žiga Gazvoda Feb 13 '16 at 17:14
  • But if you have the code y = x++; the x does change – Žiga Gazvoda Feb 13 '16 at 17:15
  • `x` has changed twice! Once up with `x++`, once down with `x--`, so it looks "unchanged" afterwards. And this is an arithmetic expression, that gets evaluated all the way, just as at school... – Paul Ogilvie Feb 13 '16 at 17:16
  • x = 20; y = 30; z = 10; z = (x == 4 || y <= 5) - How does code work now when u have commparing and logical operators combined? – Žiga Gazvoda Feb 13 '16 at 17:33
3

Due to precedence rules, the expression in the first example is identical to ( && has higher precedence than || ):

++x || ( ++y && ++z ) ;

So we're left with the operator || and its two operands ++x and ( ++y && ++z ). This operator is evaluated from left to right, so ++x is evaluated first. But this operator also short-circuits, which means that if the first operand evaluates to true, as in this case ++x does, the second operand ( ++y && ++z ) won't be evaluated.

2501
  • 25,460
  • 4
  • 47
  • 87
0

The reason is that you used || and &&.

&& and || operators short-circuit evaluations, that is, for && if the first operand evaluates to false, the second operand is never evaluated because the result would always be false. Similarly, for || if the result of the first operand is true, the second operand is never operated.

The single ampersand "&" can be said as "bit-wise AND" operator and The double ampersand "&&" can be mentioned as "Logical AND" operator.

for example, in this line: ++x || ++y && ++z; x is 1 so the boolean value of ++x is true. because you used || the statment "++y && ++z" is not run at all and the value of y and z is 1.

if you will use & and | the value will increase to 2.

Raz Omessi
  • 1,812
  • 1
  • 14
  • 13
  • 1
    I don't understand your answer. "does not abide by operator rules" reads like a syntax issue. Why introduce the bitwise operators? Has nothing to do with the logical operators here. – Paul Ogilvie Feb 13 '16 at 16:43
  • 'Short circuit' is a good term to use. But the operators always abide by the rules. – Jonathan Leffler Feb 13 '16 at 16:45