scanf() leaves the new line char in the buffer









up vote
53
down vote

favorite
33












I have the following program:



int main(int argc, char *argv)

int a, b;
char c1, c2;
printf("Enter something: ");
scanf("%d",&a); // line 1
printf("Enter other something: ");
scanf("%d", &b); // line 2

printf("Enter a char: ");
scanf("%c",&c1); // line 3
printf("Enter another char: ");
scanf("%c", &c2); // line 4

printf("Done"); // line 5

system("PAUSE");

return 0;



As I read in the C book, the author says that scanf() left a new line character in the buffer, therefore, the program does not stop at line 4 for user to enter the data, rather it stores the new line character in c2 and moves to line 5.



Is that right?



However, does this only happen with char data types? Because I did not see this problem with int data types as in line 1, 2, 3. Is it right?










share|improve this question























  • May I Ask, Which Book You Are Referring To ?
    – Suraj Jain
    Feb 19 '17 at 7:34














up vote
53
down vote

favorite
33












I have the following program:



int main(int argc, char *argv)

int a, b;
char c1, c2;
printf("Enter something: ");
scanf("%d",&a); // line 1
printf("Enter other something: ");
scanf("%d", &b); // line 2

printf("Enter a char: ");
scanf("%c",&c1); // line 3
printf("Enter another char: ");
scanf("%c", &c2); // line 4

printf("Done"); // line 5

system("PAUSE");

return 0;



As I read in the C book, the author says that scanf() left a new line character in the buffer, therefore, the program does not stop at line 4 for user to enter the data, rather it stores the new line character in c2 and moves to line 5.



Is that right?



However, does this only happen with char data types? Because I did not see this problem with int data types as in line 1, 2, 3. Is it right?










share|improve this question























  • May I Ask, Which Book You Are Referring To ?
    – Suraj Jain
    Feb 19 '17 at 7:34












up vote
53
down vote

favorite
33









up vote
53
down vote

favorite
33






33





I have the following program:



int main(int argc, char *argv)

int a, b;
char c1, c2;
printf("Enter something: ");
scanf("%d",&a); // line 1
printf("Enter other something: ");
scanf("%d", &b); // line 2

printf("Enter a char: ");
scanf("%c",&c1); // line 3
printf("Enter another char: ");
scanf("%c", &c2); // line 4

printf("Done"); // line 5

system("PAUSE");

return 0;



As I read in the C book, the author says that scanf() left a new line character in the buffer, therefore, the program does not stop at line 4 for user to enter the data, rather it stores the new line character in c2 and moves to line 5.



Is that right?



However, does this only happen with char data types? Because I did not see this problem with int data types as in line 1, 2, 3. Is it right?










share|improve this question















I have the following program:



int main(int argc, char *argv)

int a, b;
char c1, c2;
printf("Enter something: ");
scanf("%d",&a); // line 1
printf("Enter other something: ");
scanf("%d", &b); // line 2

printf("Enter a char: ");
scanf("%c",&c1); // line 3
printf("Enter another char: ");
scanf("%c", &c2); // line 4

printf("Done"); // line 5

system("PAUSE");

return 0;



As I read in the C book, the author says that scanf() left a new line character in the buffer, therefore, the program does not stop at line 4 for user to enter the data, rather it stores the new line character in c2 and moves to line 5.



Is that right?



However, does this only happen with char data types? Because I did not see this problem with int data types as in line 1, 2, 3. Is it right?







c scanf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 18 '17 at 17:53









Peter Mortensen

13.3k1983111




13.3k1983111










asked Mar 9 '11 at 2:56









ipkiss

5,3782471111




5,3782471111











  • May I Ask, Which Book You Are Referring To ?
    – Suraj Jain
    Feb 19 '17 at 7:34
















  • May I Ask, Which Book You Are Referring To ?
    – Suraj Jain
    Feb 19 '17 at 7:34















May I Ask, Which Book You Are Referring To ?
– Suraj Jain
Feb 19 '17 at 7:34




May I Ask, Which Book You Are Referring To ?
– Suraj Jain
Feb 19 '17 at 7:34












3 Answers
3






active

oldest

votes

















up vote
47
down vote



accepted










The scanf() function removes whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't remove whitespace.



Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.



Note that this still doesn't consume any trailing whitespace left in the input stream, not even to the end of a line, so beware of that if also using getchar() or fgets() on the same input stream. We're just getting scanf to skip over whitespace before conversions, like it does for %d and other non-character conversions.




Note that non-whitespace "directives" (to use POSIX scanf terminology) other than conversions, like the literal text in scanf("order = %d", &order); doesn't skip whitespace either. The literal order has to match the next character to be read.



So you probably want " order = %d" there if you want to skip a newline from the previous line but still require a literal match on a fixed string, like this question.






share|improve this answer


















  • 7




    %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
    – chux
    Dec 12 '15 at 3:12











  • @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
    – Suraj Jain
    Feb 19 '17 at 7:40










  • @SurajJain Yes,
    – chux
    Feb 20 '17 at 2:27






  • 1




    See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
    – Jonathan Leffler
    Oct 17 '17 at 5:28

















up vote
24
down vote













Use scanf(" %c", &c2);. This will solve your problem.






share|improve this answer





























    up vote
    0
    down vote













    Use getchar() before calling second scanf().



    scanf("%c", &c1);
    getchar(); // <== remove newline
    scanf("%c", &c2);





    share|improve this answer



















      protected by Community Nov 6 '17 at 16:25



      Thank you for your interest in this question.
      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



      Would you like to answer one of these unanswered questions instead?














      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      47
      down vote



      accepted










      The scanf() function removes whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't remove whitespace.



      Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.



      Note that this still doesn't consume any trailing whitespace left in the input stream, not even to the end of a line, so beware of that if also using getchar() or fgets() on the same input stream. We're just getting scanf to skip over whitespace before conversions, like it does for %d and other non-character conversions.




      Note that non-whitespace "directives" (to use POSIX scanf terminology) other than conversions, like the literal text in scanf("order = %d", &order); doesn't skip whitespace either. The literal order has to match the next character to be read.



      So you probably want " order = %d" there if you want to skip a newline from the previous line but still require a literal match on a fixed string, like this question.






      share|improve this answer


















      • 7




        %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
        – chux
        Dec 12 '15 at 3:12











      • @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
        – Suraj Jain
        Feb 19 '17 at 7:40










      • @SurajJain Yes,
        – chux
        Feb 20 '17 at 2:27






      • 1




        See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
        – Jonathan Leffler
        Oct 17 '17 at 5:28














      up vote
      47
      down vote



      accepted










      The scanf() function removes whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't remove whitespace.



      Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.



      Note that this still doesn't consume any trailing whitespace left in the input stream, not even to the end of a line, so beware of that if also using getchar() or fgets() on the same input stream. We're just getting scanf to skip over whitespace before conversions, like it does for %d and other non-character conversions.




      Note that non-whitespace "directives" (to use POSIX scanf terminology) other than conversions, like the literal text in scanf("order = %d", &order); doesn't skip whitespace either. The literal order has to match the next character to be read.



      So you probably want " order = %d" there if you want to skip a newline from the previous line but still require a literal match on a fixed string, like this question.






      share|improve this answer


















      • 7




        %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
        – chux
        Dec 12 '15 at 3:12











      • @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
        – Suraj Jain
        Feb 19 '17 at 7:40










      • @SurajJain Yes,
        – chux
        Feb 20 '17 at 2:27






      • 1




        See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
        – Jonathan Leffler
        Oct 17 '17 at 5:28












      up vote
      47
      down vote



      accepted







      up vote
      47
      down vote



      accepted






      The scanf() function removes whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't remove whitespace.



      Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.



      Note that this still doesn't consume any trailing whitespace left in the input stream, not even to the end of a line, so beware of that if also using getchar() or fgets() on the same input stream. We're just getting scanf to skip over whitespace before conversions, like it does for %d and other non-character conversions.




      Note that non-whitespace "directives" (to use POSIX scanf terminology) other than conversions, like the literal text in scanf("order = %d", &order); doesn't skip whitespace either. The literal order has to match the next character to be read.



      So you probably want " order = %d" there if you want to skip a newline from the previous line but still require a literal match on a fixed string, like this question.






      share|improve this answer














      The scanf() function removes whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't remove whitespace.



      Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.



      Note that this still doesn't consume any trailing whitespace left in the input stream, not even to the end of a line, so beware of that if also using getchar() or fgets() on the same input stream. We're just getting scanf to skip over whitespace before conversions, like it does for %d and other non-character conversions.




      Note that non-whitespace "directives" (to use POSIX scanf terminology) other than conversions, like the literal text in scanf("order = %d", &order); doesn't skip whitespace either. The literal order has to match the next character to be read.



      So you probably want " order = %d" there if you want to skip a newline from the previous line but still require a literal match on a fixed string, like this question.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 20 at 0:33









      Peter Cordes

      115k16176301




      115k16176301










      answered Mar 9 '11 at 2:59









      Jeremiah Willcock

      23.6k36072




      23.6k36072







      • 7




        %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
        – chux
        Dec 12 '15 at 3:12











      • @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
        – Suraj Jain
        Feb 19 '17 at 7:40










      • @SurajJain Yes,
        – chux
        Feb 20 '17 at 2:27






      • 1




        See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
        – Jonathan Leffler
        Oct 17 '17 at 5:28












      • 7




        %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
        – chux
        Dec 12 '15 at 3:12











      • @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
        – Suraj Jain
        Feb 19 '17 at 7:40










      • @SurajJain Yes,
        – chux
        Feb 20 '17 at 2:27






      • 1




        See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
        – Jonathan Leffler
        Oct 17 '17 at 5:28







      7




      7




      %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
      – chux
      Dec 12 '15 at 3:12





      %c, %n, % are the 3 specified expectations that do not consume leading whitespace.
      – chux
      Dec 12 '15 at 3:12













      @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
      – Suraj Jain
      Feb 19 '17 at 7:40




      @chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?
      – Suraj Jain
      Feb 19 '17 at 7:40












      @SurajJain Yes,
      – chux
      Feb 20 '17 at 2:27




      @SurajJain Yes,
      – chux
      Feb 20 '17 at 2:27




      1




      1




      See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
      – Jonathan Leffler
      Oct 17 '17 at 5:28




      See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.
      – Jonathan Leffler
      Oct 17 '17 at 5:28












      up vote
      24
      down vote













      Use scanf(" %c", &c2);. This will solve your problem.






      share|improve this answer


























        up vote
        24
        down vote













        Use scanf(" %c", &c2);. This will solve your problem.






        share|improve this answer
























          up vote
          24
          down vote










          up vote
          24
          down vote









          Use scanf(" %c", &c2);. This will solve your problem.






          share|improve this answer














          Use scanf(" %c", &c2);. This will solve your problem.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 27 '17 at 1:43









          Peter Mortensen

          13.3k1983111




          13.3k1983111










          answered Mar 9 '11 at 6:02









          Shweta

          1,95993655




          1,95993655




















              up vote
              0
              down vote













              Use getchar() before calling second scanf().



              scanf("%c", &c1);
              getchar(); // <== remove newline
              scanf("%c", &c2);





              share|improve this answer
























                up vote
                0
                down vote













                Use getchar() before calling second scanf().



                scanf("%c", &c1);
                getchar(); // <== remove newline
                scanf("%c", &c2);





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Use getchar() before calling second scanf().



                  scanf("%c", &c1);
                  getchar(); // <== remove newline
                  scanf("%c", &c2);





                  share|improve this answer












                  Use getchar() before calling second scanf().



                  scanf("%c", &c1);
                  getchar(); // <== remove newline
                  scanf("%c", &c2);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 3 at 13:52









                  Jiwon

                  159113




                  159113















                      protected by Community Nov 6 '17 at 16:25



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?



                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線