user397232
user397232

Reputation: 1790

C programming ++ operator

Why does this code always produce x=2?

unsigned int x = 0;
x++ || x++ || x++ || x++ || ........;
printf("%d\n",x);

Upvotes: 12

Views: 887

Answers (10)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Because of short circuit in boolean expression evaluation and because || is a sequence point in C and C++.

Upvotes: 9

John Bode
John Bode

Reputation: 123578

The || operator evaluates the left-hand expression, and if it is 0 (false), then it will evaluate the right-hand expression. If the left hand side is not 0, then it will not evaluate the right hand side at all.

In the expression x++ || x++ || x++ || ..., the first x++ is evaluated; it evaluates to 0, and x is incremented to 1. The second x++ is evaluated; it evaluates to 1, and x is incremented to 2. Since the second x++ evaluated to a non-zero value, none of the remaining x++ expressions are evaluated.

Upvotes: 1

Mike Marshall
Mike Marshall

Reputation: 7850

Because the first "x++ || x++" evaluates to "true" (meaning it is non zero because "0 || 1" is true. Since they are all logical OR operators the rest of the OR operations are ignored.

Mike

Upvotes: 1

slacy
slacy

Reputation: 11783

When you're evaluating "a || b || c || d || e || ..." you can stop evaluating at the first non-zero value you find.

The first "x++" evaluates to 0, and increments x to 1, and evaluating the expression continues. The second x++ is evaluated to 1, increments x to 2, and at that point, you need not look at the rest of the OR statement to know that it's going to be true, so you stop.

Upvotes: 2

John Knoeller
John Knoeller

Reputation: 34198

Because of early out evaluation of comparisons.

This is the equivalent of

 0++ | 1++

The compiler quits comparing as soon as x==1, then it post increments, making x==2

Upvotes: 1

RC.
RC.

Reputation: 28267

Because logical OR short-circuits when a true is found.

So the first x++ returns 0 (false) because it is post-increment. (x = 1) The second x++ returns 1 (true) - short-circuits. (x = 2)

Prints x = 2;

Upvotes: 1

Earlz
Earlz

Reputation: 63905

trying replacing || with |.--

It is the short circuiting of logical operators.

It's the same reason when you do

if (returns_true() || returns_true()){ }

returns_true will only get called once.

Upvotes: 0

Sudhanshu
Sudhanshu

Reputation: 2871

x++ || x++ || x++ || x++ || ........;

  • First x++ evaluates to 0 first for the conditional check, followed by an increment. So, first condition fails, but x gets incremented to 1.
  • Now the second x++ gets evaluated, which evaluates to 1 for the conditional check, and x gets incremented to 2. Since expression evaluates to 1 (true), there's no need to go further.

Upvotes: 11

Zano
Zano

Reputation: 2761

|| short-circuits. Evaluated from left, when a true value is found (non-zero) it stops evaluating, since the expression now is true and never can be false again.

First x++ evaluates to 0 (since it's post-increment), second to 1 which is true, and presto, you're done!

Upvotes: 5

cobbal
cobbal

Reputation: 70775

the 1st x++ changes x to 1 and returns 0
the 2nd x++ changes x to 2 and returns 1

at which point the or short circuits, returns true, and leaves x at 2.

Upvotes: 30

Related Questions