title: Why does C not have a logical assignment operator?
body: <p>I had the need to code a statement of the form</p>
  
  <pre><code>a = a || expr;
  </code></pre>
  
  <p>where <code>expr</code> should be evaluated and the result be assigned to <code>a</code> iff <code>a</code> is not set. this relies on the logical OR's short-circuiting capabilities.</p>
  
  <p>The shorter way to write the above would, of course, be</p>
  
  <pre><code>a ||= expr;
  </code></pre>
  
  <p>but (to my surprise) C does not have logical assignment operators. </p>
  
  <p>So my question is twofold. First, is there a shorter way to write the first statement in standard C (the ternary operator is even worse - <code>a = a ? a : expr</code> requires me to spell out <code>a</code> thrice). </p>
  
  <p>Secondly, why aren't there logical assignments in C? The possible reasons I could think of are:</p>
  
  <ul>
  <li>it makes the grammar harder to parse?</li>
  <li>there is some subtlety in handling short-circuiting for these cases?</li>
  <li>it was considered superfluous (but isn't that an argument against ALL the operator assignments?)</li>
  </ul>
  
  <p><strong>EDIT</strong> </p>
  
  <p><strong>Please unlock this question because:</strong></p>
  
  <ul>
  <li><p>The question it has been linked to (as a alleged duplicate of) HAS NOT BEEN ANSWERED. The (accepted) answer to that question states that <code>||=</code> is not present because duplicates the functionality of <code>|=</code>. That is the wrong answer. <code>|=</code> does not short-circuit.</p></li>
  <li><p>C and C++ are NOT the same languages. I wish to know why C doesn't have it. In fact, the fact that derived languages like C++ and, particularly, Java (which did not suffer from the problems of legacy code as has been suggested in Edmund's answer) makes the question even more interesting.</p></li>
  </ul>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>It now seems like my original intent was wrong. In the statement <code>a = a || expr</code> (where <code>a</code> is integral and <code>expr</code> returns an integral value, first both <code>a</code> and <code>expr</code> will be implicitly converted to "booleans", and then the "boolean" value will be assigned to <code>a</code>. This will be incorrect — the integral value will be lost. Thanks, Jens and Edmund.</p>
  
  <p>So for the first part of the question, the correct ways, not alternatives :), to code my intention would be:</p>
  
  <pre><code>if (!a) a = expr;
  </code></pre>
  
  <p>or </p>
  
  <pre><code>a = a ? a : expr;
  </code></pre>
  
  <p>they should be optimized the same (I think) though personally I would prefer the first one (because it has one less <code>a</code> to type).</p>
  
  <p>However, the second part of the question still remains. The arguments that Jens and Edmund about have given about the ambiguity in <code>a ||= expr</code> apply equally well to <code>a = a || expr</code>. the assignment case can simply be treated as the normal one:</p>
  
  <ul>
  <li>convert <code>a</code> to boolean</li>
  <li>if it is true, the value of the entire expression becomes equal to the boolean value of <code>a</code></li>
  <li>otherwise evaluate <code>expr</code>, convert result to boolean, assign to <code>a</code>, and return it</li>
  </ul>
  
  <p>The steps above seem to be the same for both the assignment and normal case.</p>