title: Error using scanf() for user input
body: <p>I am getting a weird problem while using <code>scanf()</code> to store data into a union.</p>
  
  <p>Here's my code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  union Student
  {
      float score;
      char grade;
  };
  
  int main(void)
  {
      union Student jack;
  
      printf("Enter student score : ");
      scanf("%f", &amp;jack.score);
      printf("Score : %f", jack.score);
  
      jack.score=0;
  
      printf("Enter student grade : ");
      scanf("%c", &amp;jack.grade);
      printf("Grade : %c", jack.grade);
  
  }
  </code></pre>
  
  <p>I get the following output</p>
  
  <pre><code>searock@searock-desktop:~/Desktop$ ./union
  Enter student score : 12
  Score : 12.000000Enter student grade : Grade :
  </code></pre>
  
  <p>but if I change my code to:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  union Student
  {
      float score;
      char grade;
  };
  
  int main(void)
  {
      union Student jack;
  
      printf("Enter student grade : ");
      scanf("%c", &amp;jack.grade);
      printf("Grade : %c\n", jack.grade);
  
      printf("Enter student score : ");
      scanf("%f", &amp;jack.score);
      printf("Score : %f\n", jack.score);
  
  }
  </code></pre>
  
  <p>It gives me the exact output [correct output]. I know this is not a good example, but can someone explain me what's going wrong?</p>
  
  <p><strong>Modified Code :</strong> Add \n before format string. [scanf("\n%c", &amp;ch);]</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
      union Student
      {
          float score;
          char grade;
      };
  
      int main(void)
      {
          union Student jack;
  
          printf("Enter student score : ");
          scanf("%f", &amp;jack.score);
          printf("Score : %f", jack.score);
  
          jack.score=0;
  
          printf("Enter student grade : ");
          scanf("\n%c", &amp;jack.grade);
          printf("Grade : %c", jack.grade);
  
      }
  </code></pre>