-1

I'm currently debugging a program for a school asignment and is currently stuck at understanding this line of code.

All variables are integers.

unfinished = count == 2;

Output of that is mainly 0, but does the "==" (comparison?) actually have an affect to the values or is it completly ignored?

Program language is C

uzr
  • 1,210
  • 1
  • 13
  • 26

2 Answers2

7

Operator precedence means that

unfinished = count == 2;

is evaluated as

unfinished = (count == 2);

Which is equivalent to

if (count == 2)
    unfinished = 1;
else
    unfinished = 0;
simonc
  • 41,632
  • 12
  • 85
  • 103
2

if countis equal to 2, unfinished will be 1 (true), else it will be 0 (false)

Tim
  • 41,901
  • 18
  • 127
  • 145
  • *Right of the = is evaluated first*: No. No guarantee that right operand of `=` operator evaluates first. Order of evaluation is not guaranteed with this operator. – haccks Feb 12 '14 at 21:12
  • This table say it is http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm – Tim Feb 12 '14 at 21:15
  • Are you mixing Associativity with order of evaluation? – haccks Feb 12 '14 at 21:20
  • 1
    @TimCastelijns: Precedence does not guarantee order of evaluation; the result of `count == 2` must be evaluated before it can be *assigned* to `unfinished`, but that doesn't mean `unfinished` can't be evaluated first. – John Bode Feb 12 '14 at 21:23
  • 1
    I think you are confused with **associativity** and **order of evaluation**. Read [this](http://blogs.msdn.com/b/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx?Redirected=true) article by [Eric Lippert](http://stackoverflow.com/users/88656/eric-lippert). – haccks Feb 12 '14 at 21:23
  • I have done some reading and came the the conclusion i was indeed mixing up some words.. thanks for correcting me :-) – Tim Feb 12 '14 at 21:25