0

I have the following line in my code:

1 || printf("A");

I was surprised to see that A is not printed; I am guessing this is due to compiler optimizations: 1 was evaluated as true, and, because the entire OR expression must have been true, the printf("A") wasn't even evaluated... Can someone confirm this? Would the program behave like this with different compilers?

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
  • How about `printf("A\n")`? – arrowd Apr 30 '16 at 16:31
  • 4
    See [Short circuit evaluation and side effects](http://stackoverflow.com/questions/3635722/short-circuit-evaluation-and-side-effects). This is not just an optimization; it is *required* behavior. I would suggest that you find a better C programming book. This is a pretty fundamental concept. – Raymond Chen Apr 30 '16 at 16:32
  • NEVER use `1 || (insert anything here)` because `1 ||` equals expression is true and any smart compiler wouldn't process the rest of the expression. – Mike -- No longer here Apr 30 '16 at 17:01
  • "*I have the following line in my code*" Why? :-S – alk Apr 30 '16 at 17:21
  • @alk Well not really *my* code. I'd never write that, it's from a piece of "demo" code which isn't supposed to do anything useful. :D – Eutherpy Apr 30 '16 at 21:45

2 Answers2

6

In the expression a || b, b is only evaluated when a is false (similarly in a && b, b is only evaluated if a is true). This is known as short-circuiting and it's not an optimization, it's behavior mandated by the standard. If the compiler did anything else, it'd not be a valid implementation.

This allows you to do things like a != NULL && a->isValid() and be sure that you're not dereferencing a null pointer.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
0

I am guessing this is due to compiler optimizations: 1 was evaluated as true, and, because the entire OR expression must have been true, the printf("A") wasn't even evaluated..

The decision to execute the printf("A") function or not is not made during compilation but it is made during execution of the program. So, this rule out your explanation of compilation optimization.

In the expression exp1 || exp2, exp1 is evaluated first. If it evaluates to true, then exp2 is not evaluated as result of logical operator is true in-spite of the value of exp2. However, if exp1 is false, then exp2 will be evaluated. This behavior is mandated by the standard.

So, in the expression 1 || printf("A"), there is no need to evaluate printf("A") as the result of the expression will be true in-spite of the evaluation of printf("A") due to the first expression which is 1.

Similarly, expressions are evaluated(from left to right) when logical AND operator && is used in between two expressions.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • When the LHS of the `||` operator can be evaluated at compile time, as in the question, the decision does not need to be made at run-time. In the example in the question, even with minimal optimization, the compiler will simply drop the `printf()` from the code; it cannot be executed. – Jonathan Leffler May 01 '16 at 01:14