Connecting Tech Pros Worldwide Forums | Help | Site Map

scanf problem

main()
Guest
 
Posts: n/a
#1: Sep 8 '06
I know this is the problem that most newbies get into.

#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a);
return 0;
}

Out of curiosity I tried this also,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}

which gave me expected output.

But my question is (finally!) , why the following code doesn't work,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.


Thanks for your time,
Yugi.


valis.eric.ykchan@gmail.com
Guest
 
Posts: n/a
#2: Sep 8 '06

re: scanf problem


I use fscanf to read from a file and the newline is consumed, using
both MS-VC 6 and gcc 3.3.5. What are you using?

Robert Gamble
Guest
 
Posts: n/a
#3: Sep 8 '06

re: scanf problem


main() wrote:
Quote:
I know this is the problem that most newbies get into.
>
#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}
>
This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.
>
I made this attempt to solve this problem
>
#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a);
return 0;
}
This will only work (the way you seem to want it to) if the second
character in the first input line is a newline. You introduced a new
variable here, b, did you mean to do something with it?
Quote:
Out of curiosity I tried this also,
>
#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}
>
which gave me expected output.
Any whitespace in the format string will match any amount of whitespace
in the input, including none. Your newline will match any number of
spaces, tabs, newlines, etc., until a non-whitespace character is
encountered.
Quote:
But my question is (finally!) , why the following code doesn't work,
>
#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}
>
My assumption was 1st scanf will consume the newline left in the input
stream.
The first scanf call will store the first character into a and consume
of any immediately following whitespace (including newlines and any
whitespace that follows). You didn't specify what the input was that
you used or how the output differed from your expectations, so it's
difficult to help you understand what the "problem" is, feel free to
clarify.

See also my response to the thread entitled "cannot use char and
integer in the same program" on August 10th 2006 in this group.

Robert Gamble

main()
Guest
 
Posts: n/a
#4: Sep 8 '06

re: scanf problem



Robert Gamble wrote:
Quote:
main() wrote:
Quote:
I know this is the problem that most newbies get into.

#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a);
return 0;
}
>
This will only work (the way you seem to want it to) if the second
character in the first input line is a newline. You introduced a new
variable here, b, did you mean to do something with it?
>
Quote:
Out of curiosity I tried this also,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}

which gave me expected output.
>
Any whitespace in the format string will match any amount of whitespace
in the input, including none. Your newline will match any number of
spaces, tabs, newlines, etc., until a non-whitespace character is
encountered.
>
Quote:
But my question is (finally!) , why the following code doesn't work,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.
>
The first scanf call will store the first character into a and consume
of any immediately following whitespace (including newlines and any
whitespace that follows). You didn't specify what the input was that
you used or how the output differed from your expectations, so it's
difficult to help you understand what the "problem" is, feel free to
clarify.
>
Hi,

for the last code snippet
this is the output i got

y
z
y
z

when i entered y , it didn't print anything
when i entered z, it printed both y and z.
What i expected is this,

y
y
z
z

Thanks for your time,
Yugi.

Shashank
Guest
 
Posts: n/a
#5: Sep 8 '06

re: scanf problem


To solve this problem i modified the code as follows:

#include<stdio.h>
main(void)
{
char a;
flushall();
//scanf("%c\n",&a); /* 1st scanf */
a=getchar();
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;

}


but here i found more interesting part as, it does not stop for getting
input for 2nd scanf statement. Can someone , why??????????

Keith Thompson
Guest
 
Posts: n/a
#6: Sep 8 '06

re: scanf problem


"Shashank" <smaratkar@gmail.comwrites:
Quote:
To solve this problem i modified the code as follows:
>
#include<stdio.h>
main(void)
{
char a;
flushall();
//scanf("%c\n",&a); /* 1st scanf */
a=getchar();
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
>
}
What is flushall()? There is no such function in standard C. What is
it supposed to do?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Shashank
Guest
 
Posts: n/a
#7: Sep 8 '06

re: scanf problem


Quote:
>
What is flushall()? There is no such function in standard C. What is
it supposed to do?
flushall(); flushes the buffer to aviod the entry of garbage values.

Richard Heathfield
Guest
 
Posts: n/a
#8: Sep 8 '06

re: scanf problem


Shashank said:
Quote:
>
Quote:
>>
>What is flushall()? There is no such function in standard C. What is
>it supposed to do?
>
flushall(); flushes the buffer to aviod the entry of garbage values.
On your system, perhaps it does; but the C Standard does not guarantee this.
On some other system, it might do something else entirely.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Ratan
Guest
 
Posts: n/a
#9: Sep 8 '06

re: scanf problem


There is NO flushall() function.

There is one function fflush(), which flushes out the i/o stream data.

Richard Heathfield wrote:
Quote:
Shashank said:
>
Quote:
Quote:
>
What is flushall()? There is no such function in standard C. What is
it supposed to do?
flushall(); flushes the buffer to aviod the entry of garbage values.
>
On your system, perhaps it does; but the C Standard does not guarantee this.
On some other system, it might do something else entirely.
>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Richard Heathfield
Guest
 
Posts: n/a
#10: Sep 8 '06

re: scanf problem


Ratan said:
Quote:
There is NO flushall() function.
Correction: the C standard library does not provide one. Implementation
providers are free to provide a function by that name in a third-party
library if they choose, and at least one such provider does precisely that.
Quote:
There is one function fflush(), which flushes out the i/o stream data.
Indeed - and fflush(NULL) flushes out /all/ data currently pending on
streams open for output or update. This is guaranteed by the Standard.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
CBFalconer
Guest
 
Posts: n/a
#11: Sep 8 '06

re: scanf problem


Ratan wrote:
Quote:
>
There is NO flushall() function.
>
There is one function fflush(), which flushes out the i/o stream data.
Please do not top-post. Your answer belongs after, or intermixed
with, the snipped data to which you reply. See the links below in
my sig.

fflush DOES NOT deal with input streams. In fact, attempting to
apply it to one results in undefined behaviour. A useful function
to have around is:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
} /* flushln */

which will flush an input stream through the next '\n'.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Flash Gordon
Guest
 
Posts: n/a
#12: Sep 8 '06

re: scanf problem


Richard Heathfield wrote:
Quote:
Ratan said:
>
Quote:
>There is NO flushall() function.
>
Correction: the C standard library does not provide one. Implementation
providers are free to provide a function by that name in a third-party
library if they choose, and at least one such provider does precisely that.
>
Quote:
>There is one function fflush(), which flushes out the i/o stream data.
>
Indeed - and fflush(NULL) flushes out /all/ data currently pending on
streams open for output or update. This is guaranteed by the Standard.
However, it does *not* flush input streams. Standard C has no support
for flushing input streams.

I know you know this Richard, but I suspect Ratan does not.
--
Flash Gordon
Richard Heathfield
Guest
 
Posts: n/a
#13: Sep 8 '06

re: scanf problem


Flash Gordon said:
Quote:
Richard Heathfield wrote:
Quote:
>>
>[...] fflush(NULL) flushes out /all/ data currently pending on
>streams open for output or update. This is guaranteed by the Standard.
>
However, it does *not* flush input streams. Standard C has no support
for flushing input streams.
Right. That's why I said it flushes *out* all data currently pending on
streams open for *output* or *update*.
Quote:
I know you know this Richard, but I suspect Ratan does not.
That's why I corrected him.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Keith Thompson
Guest
 
Posts: n/a
#14: Sep 8 '06

re: scanf problem


CBFalconer <cbfalconer@yahoo.comwrites:
Quote:
Ratan wrote:
Quote:
>>
>There is NO flushall() function.
>>
>There is one function fflush(), which flushes out the i/o stream data.
>
Please do not top-post. Your answer belongs after, or intermixed
with, the snipped data to which you reply. See the links below in
my sig.
>
fflush DOES NOT deal with input streams. In fact, attempting to
apply it to one results in undefined behaviour. A useful function
to have around is:
>
int flushln(FILE *f) {
int ch;
>
while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
} /* flushln */
>
which will flush an input stream through the next '\n'.
Yes, that's one possible meaning of "flushing" an input stream.

At least one system I know of defines fflush() for input streams:

<SYSTEM_SPECIFIC>
Flushing an input stream discards any buffered input and adjusts
the file pointer such that the next input operation accesses the
byte after the last one read.
</SYSTEM_SPECIFIC>

This is perfectly legitimate; one of the infinitely many possible
consequences of undefined behavior is to behave in a manner defined by
the implementation.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Robert Gamble
Guest
 
Posts: n/a
#15: Sep 9 '06

re: scanf problem


On Thu, 07 Sep 2006 23:15:56 -0700, main() wrote:

Quote:
Robert Gamble wrote:
>
Quote:
>main() wrote:
Quote:
I know this is the problem that most newbies get into.
>
#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}
>
This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.
>
I made this attempt to solve this problem
>
#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/ scanf("%c",&a);
printf("%c\n",a);
return 0;
}
>>
>This will only work (the way you seem to want it to) if the second
>character in the first input line is a newline. You introduced a new
>variable here, b, did you mean to do something with it?
>>
Quote:
Out of curiosity I tried this also,
>
#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}
>
which gave me expected output.
>>
>Any whitespace in the format string will match any amount of whitespace
>in the input, including none. Your newline will match any number of
>spaces, tabs, newlines, etc., until a non-whitespace character is
>encountered.
>>
Quote:
But my question is (finally!) , why the following code doesn't work,
>
#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}
>
My assumption was 1st scanf will consume the newline left in the
input
stream.
>>
>The first scanf call will store the first character into a and consume
>of any immediately following whitespace (including newlines and any
>whitespace that follows). You didn't specify what the input was that
>you used or how the output differed from your expectations, so it's
>difficult to help you understand what the "problem" is, feel free to
>clarify.
>>
>>
Hi,
>
for the last code snippet
this is the output i got
>
y
z
y
z
>
when i entered y , it didn't print anything when i entered z, it
printed both y and z. What i expected is this,
>
y
y
z
z
>
Thanks for your time,
stdin is typically line-buffered, your input is seen by scanf one line at
a time. Scanf doesn't automatically return when it encounters an end of
line character, it returns when it finishes matching the format string or
when there is an input failure (end of file) or a matching failure
(something is encountered which doesn't match the format string). As
previously mentioned, any whitespace in the format string will match any
amount of whitespace in the input stream, your newline character will
match any amount of whitespace, the function will not return until it
encounters a non-whitespace character (or encounters end of file, etc.).
After reading the first character, 'y', the call to scanf is going to
continue reading from stdin, one character at a time, until it encounters
a non-whitespace character before returning. In the first line it assigns
the character 'y' to the variable a and then reads the newline which is a
whitespace character. Since scanf has not yet encountered a
non-whitespace character it is going to wait until the next line of input
is available and not return until it sees the 'z' on the second line. When
scanf encounters the 'z' it will see that it is not a whitespace
character, push it back onto the stream and return having finished
matching its format specifier. The second call to scanf will read the
character 'z' that the first call just pushed back and return immediately.

If you want to process input a line at a time your best bet is probably to
use fgets and sscanf.

Robert Gamble

Closed Thread


Similar C / C++ bytes