Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the final value of j in the below code?

int main()
    {
        int i = 0, j = 0;
        if (i && (j = i + 10))
            //do something
            ;
    }

 

  • A.10
  • B.Depends on the compiler
  • C.Depends on language standard
  • D.0

Answer: D

Correct answer: 0

&& short-circuits: since i is 0 the whole condition is already false, so the right operand (j = i + 10) is never evaluated and the assignment never happens. j keeps its initial value, 0. This guarantee is what makes idioms such as if (p && p->next) safe.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.