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










  • It is sometimes suggested that fflush(stdin) can be used before the call to scanf() for a single character. Please read Using fflush(stdin) for a discussion of the pros and cons and alternatives to that method (which works, more or less, on Windows, and does not work most other places).
    – Jonathan Leffler
    29 mins ago















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










  • It is sometimes suggested that fflush(stdin) can be used before the call to scanf() for a single character. Please read Using fflush(stdin) for a discussion of the pros and cons and alternatives to that method (which works, more or less, on Windows, and does not work most other places).
    – Jonathan Leffler
    29 mins ago













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,3832471112




5,3832471112












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










  • It is sometimes suggested that fflush(stdin) can be used before the call to scanf() for a single character. Please read Using fflush(stdin) for a discussion of the pros and cons and alternatives to that method (which works, more or less, on Windows, and does not work most other places).
    – Jonathan Leffler
    29 mins ago


















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










  • It is sometimes suggested that fflush(stdin) can be used before the call to scanf() for a single character. Please read Using fflush(stdin) for a discussion of the pros and cons and alternatives to that method (which works, more or less, on Windows, and does not work most other places).
    – Jonathan Leffler
    29 mins ago
















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












It is sometimes suggested that fflush(stdin) can be used before the call to scanf() for a single character. Please read Using fflush(stdin) for a discussion of the pros and cons and alternatives to that method (which works, more or less, on Windows, and does not work most other places).
– Jonathan Leffler
29 mins ago




It is sometimes suggested that fflush(stdin) can be used before the call to scanf() for a single character. Please read Using fflush(stdin) for a discussion of the pros and cons and alternatives to that method (which works, more or less, on Windows, and does not work most other places).
– Jonathan Leffler
29 mins ago












3 Answers
3






active

oldest

votes

















up vote
48
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





















    • This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
      – Jonathan Leffler
      33 mins ago












    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
    48
    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
    48
    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
    48
    down vote



    accepted







    up vote
    48
    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

    116k16176302




    116k16176302










    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,96493655




        1,96493655






















            up vote
            0
            down vote













            Use getchar() before calling second scanf().



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





            share|improve this answer





















            • This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
              – Jonathan Leffler
              33 mins ago

















            up vote
            0
            down vote













            Use getchar() before calling second scanf().



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





            share|improve this answer





















            • This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
              – Jonathan Leffler
              33 mins ago















            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












            • This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
              – Jonathan Leffler
              33 mins ago




















            • This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
              – Jonathan Leffler
              33 mins ago


















            This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
            – Jonathan Leffler
            33 mins ago






            This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != 'n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).
            – Jonathan Leffler
            33 mins ago







            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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)