asad_hussain
asad_hussain

Reputation: 2011

What is the order of evaluation of the statements in this code?

I came across this question.

#include <stdio.h>
int main()
{
int k=8;
int x=0==1||k++;
printf("%d  %d",x,k);
    return 0;
}

The output is 1 9.

As this answer suggests that

Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated.

I am unable to understand how the statement int x=0==1||k++ is evaluated,due to which the value of x and k becomes 1,9 respectively.

Can someone explain how such statements are evaluated by the compiler in c ?

Upvotes: 0

Views: 180

Answers (3)

babon
babon

Reputation: 3774

x is 1 because the expression: 0==1||k++ turns out to be true (which is 1 in C land). Why you ask? There are two sequence points here: 0 == 1 and k++. Since the first sequence point evaluates to false (0 in C land), the second sequence point is evaluated (because the short circuit operator is ||). The second sequence returns true (or 1). So, you the entire expression breaks down to: 0 || 1. Hence x is 1.

k is 9 because of k++;

HTH.

Upvotes: 1

eRaisedToX
eRaisedToX

Reputation: 3361

"Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated."

yes it's true ...

to make it clear first make sure that you are aware of these basics

1) (1 || any_var) is 1

2) Operator precedence is as follows

++

then

==

then

||

NOW coming to your doubt of || vs |

note that | (single pipe) operator ..,will execute both LHS and RHS , no matter what)

whereas

|| (double pipe) evaluates LHS of || first and if it is 1 it need not evaluate RHS (for speed)

RHS of || operator will not be evaluted if LHS comes out to be true.

but here 0==1 is false i.e 0==1 returns 0

hence RHS will be evalauted

so the statement

k++ is executed

but 8 is used (as property of post increment operator says--> first use then increment)

so 0||8 is definitely true (1) so x evaluates to 1

and then k is incremented after sequence point ie k is made equal to 9

hence output x=1 and k=9

I hope it clears your doubt :)

Upvotes: 5

Pedro David
Pedro David

Reputation: 377

The Operator || (OR) evaluates to true in the cases:

ex: A || B

  • A is true,
  • B is true,
  • Both are true

Because this operation uses Short-Circuit Evaluation if A is evaluated to true, it means that the statement is true already and it won't evaluate B.

In your case 0==1 (0 equals 1) is clearly false, so it will evaluate k++. k++ is a tricky one (in my opinion). In the world of C true/false evaluation is based on being 0 or not (except for the times false means less than 0...) but in true/false evaluation 0 is false, everything else is true.

K++ means evaluate K then increment, so, if K is 0 it will be false and become 1, if it is anything else, it will be true and then increment.

In your case k == 8 so the result of k++ is TRUE and k will become 9. K being true means x evaluation resulted in TRUE (it was FALSE OR TRUE).

So the output is 1(True) 9(8++)

Upvotes: 1

Related Questions