Home > Software engineering >  How to tally the expression with operator precedence table in C?
How to tally the expression with operator precedence table in C?

Time:10-12

I know the following code is not a good way to write expressions in C and there might be a lot of post already discouraging such post, but I am sure the output is not compiler dependent.

Here is what the code looks like:

#include <stdio.h>

int main() {
    int x = 2;
    int y = 1;
    int z = x ||   y;
    
    printf("%d %d %d\n", x, y, z);
    return 0;
}

I have the following questions:

  1. Now in this I know that short-circuiting would happen and y is never evaluated but if I am not wrong, unary operators have higher precedence than logical operator and hence y should happen first and the expression should short circuit from the reverse direction right?

  2. Also one more issue is that if the short-circuit starts happening from the left side, then x is evaluated first and then because of short-circuiting, the value of x should be assigned to z, but it is getting answer to be 1. Why so?

CodePudding user response:

The order of evaluation article on Cppreference is a useful resource here.

Question 1

There is a difference between the operator associativity and the order of evaluation. The former specifies how a (sub)expression is parsed, or in other words "where to put the parentheses". The latter deals with if and in what order subexpressions are evaluated.

Quoting the example given on Cppreference:

[..] the expression f1() f2() f3() is parsed as (f1() f2()) f3() due to left-to-right associativity of operator , but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time.

There are several rules concerning the order of evaluation. An important concept here, is the sequence point:

If a sequence point is present between the subexpressions E1 and E2, then both value computation and side effects of E1 are sequenced-before every value computation and side effect of E2.

The statement int z = x || y; contains a sequence point, due to the use of the logical-OR operator:

  1. There is a sequence point after evaluation of the first (left) operand and before evaluation of the second (right) operand of the following binary operators: && (logical AND), || (logical OR), and , (comma).

Combining the two quotations, this means that x will be evaluated first. Since x is non-zero, the right side of the OR expression is not evaluated and the logical-OR operator has the result 1 (see here). This also means that the side-effect of y does not take place.

This behavior is defined according to the C standard, and is thus not compiler dependent (i.e. not implementation defined).

Question 2

The logical-OR operator does not result in x here, because it represents a boolean value. In C, for all boolean expressions, this the int type 1 for a true expression and the int type 0 for a false expression.

Output

The code will print 2 1 1 for x y z, as x and y are unchanged since their initialization and the logical-OR operator resulted in 1.

  • Related