scanf() leaves the new line char in the buffer
up vote
53
down vote
favorite
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
add a comment |
up vote
53
down vote
favorite
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
May I Ask, Which Book You Are Referring To ?
– Suraj Jain
Feb 19 '17 at 7:34
It is sometimes suggested thatfflush(stdin)
can be used before the call toscanf()
for a single character. Please read Usingfflush(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
add a comment |
up vote
53
down vote
favorite
up vote
53
down vote
favorite
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
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
c scanf
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 thatfflush(stdin)
can be used before the call toscanf()
for a single character. Please read Usingfflush(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
add a comment |
May I Ask, Which Book You Are Referring To ?
– Suraj Jain
Feb 19 '17 at 7:34
It is sometimes suggested thatfflush(stdin)
can be used before the call toscanf()
for a single character. Please read Usingfflush(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
add a comment |
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.
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 inscanf()
format string andscanf()
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
add a comment |
up vote
24
down vote
Use scanf(" %c", &c2);
. This will solve your problem.
add a comment |
up vote
0
down vote
Use getchar()
before calling second scanf()
.
scanf("%c", &c1);
getchar(); // <== remove newline
scanf("%c", &c2);
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
add a comment |
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.
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 inscanf()
format string andscanf()
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
add a comment |
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.
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 inscanf()
format string andscanf()
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
add a comment |
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.
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.
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 inscanf()
format string andscanf()
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
add a comment |
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 inscanf()
format string andscanf()
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
add a comment |
up vote
24
down vote
Use scanf(" %c", &c2);
. This will solve your problem.
add a comment |
up vote
24
down vote
Use scanf(" %c", &c2);
. This will solve your problem.
add a comment |
up vote
24
down vote
up vote
24
down vote
Use scanf(" %c", &c2);
. This will solve your problem.
Use scanf(" %c", &c2);
. This will solve your problem.
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
add a comment |
add a comment |
up vote
0
down vote
Use getchar()
before calling second scanf()
.
scanf("%c", &c1);
getchar(); // <== remove newline
scanf("%c", &c2);
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
add a comment |
up vote
0
down vote
Use getchar()
before calling second scanf()
.
scanf("%c", &c1);
getchar(); // <== remove newline
scanf("%c", &c2);
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
add a comment |
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);
Use getchar()
before calling second scanf()
.
scanf("%c", &c1);
getchar(); // <== remove newline
scanf("%c", &c2);
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
add a comment |
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
add a comment |
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?
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 toscanf()
for a single character. Please read Usingfflush(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