title: Check scanf(..) success without using the number of fields — is there a way?
body: <p>A typical idiom is:</p>
  
  <pre><code>if (scanf("%d%d%d", &amp;a, &amp;b &amp;c) != 3)
      handle_the_failure();
  </code></pre>
  
  <p>This <code>3</code>, the number of fields, is redundant. If I change the pattern and the arguments to <code>scanf(..)</code>, but forget to change the <code>3</code>, that's another compile-test-debug cycle, which is a waste of time.</p>
  
  <p>Is there an idiom that allows to check the (absolute) success of <code>scanf(..)</code> without having to write the number of fields in the code? Maybe something like this:</p>
  
  <pre><code>scanf("%d%d%d", &amp;a, &amp;b &amp;c);
  if (lastScanfFailedInAnyWay())
      handle_the_failure();
  </code></pre>
  
  <p>The <a href="http://www.cplusplus.com/reference/cstdio/scanf/" rel="nofollow">documentation</a> (see "Return value" section) talks about four different conditions:</p>
  
  <ol>
  <li><p>End-of-file</p></li>
  <li><p>Reading error</p></li>
  <li><p>Matching failure</p></li>
  <li><p>Encoding error interpreting wide characters</p></li>
  </ol>
  
  <p>The first two are addressed by <code>feof(..)</code> and <code>ferror(..)</code> (I'm assuming <code>feof(..)</code> implies <code>ferror(..)</code>), and the last — by setting <code>errno</code> to <code>EILSEQ</code>. But I'm interested in a catch-all (i.e. including figuring out if a matching failure occurred).</p>
  
  <p>P.S. Looking at the neat out-of-context example above, this may seem like too much to ask, but consider real practice, where you make many changes rapidly, and there are hundreds of little things like this to keep in mind, that every time you change one thing, you have to change another one elsewhere. Then it becomes clear that developing habits that eliminate dependencies of various sorts pays off.</p>