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

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?