title: Read no more than size of string with scanf()
body: <p>Edit:  for my class I have to use <code>scanf</code>. So recommending other ways of input is not the solution I am looking for (if there is one that involves scanf).</p>
  <hr />
  <p>If I am reading in user input for a small project (for example, a game).   Lets say I ask <code>would you like to play?</code>  This would accept a <code>yes</code> or <code>no</code> answer.  So i write up some simple code like this:</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void)
  {
       char string[3]; //The max number of letters for &quot;yes&quot;.
  
       printf(&quot;Would you like to play?&quot;);
       scanf(&quot;%s&quot;, string);
  }
  </code></pre>
  <p>So this code should simply ask them to input <code>yes</code> or <code>no</code>.  I am setting the length of my <code>char array</code> to size <code>3</code>.  This way it is large enough to hold <code>yes</code> and also <code>no</code>.  But if someone were to enter invalid input such as <code>yesss</code>, I know how to compare the string afterwards to handle such an event, but wouldn't this technically/possibly overwrite other local variables I have declared because it would extend outside the length of my array?  If so, is there a way to handle this to restrict 3 input characters or something?  And if not, why/how does it know to only input for the size of 3?</p>