Search code examples
cdebuggingbooleanvariable-assignmentunsigned

Why doesn't "a && (b = 5/a)" assign "5/a" to "b"?


Does a && (b = 5/a) assign 5/a to b (for nonzero a)?

My friend says it doesn't, but I'm confused why it wouldn't.


Solution

  • Your friend is wrong. For nonzero a, the statement a && (b = 5/a) will assign the value 5/a to b.

    If a == 0, then the conditional will short circuit and the assignment will not occur.