about scanf()

Å

å´å¹³

int get_int(void)
{
int input;
char ch;
while(scanf("%d",&input)!=1)
{
while((ch=getchar())!='\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an integer value, such as 25,-178 or 3: ");
}
return input;
}

///////////////////////////////////////////////////
Here is the code from C Primer Plus, when the input is not an integer, it print the wrong input.
My question is, if scanf() gets the wrong input, it doesn't through out the wrong input, but give it to next receiver?
Poor English, sorry for that.
 
I

Ike Naar

int get_int(void)
{
int input;
char ch;
while(scanf("%d",&input)!=1)
{
while((ch=getchar())!='\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an integer value, such as 25,-178 or 3: ");
}
return input;
}

///////////////////////////////////////////////////
Here is the code from C Primer Plus, when the input is not an integer, it print the wrong input.
My question is, if scanf() gets the wrong input, it doesn't through out the wrong input, but give it to next receiver?

That is correct, but scanf only has to "unread" (push back into
the input stream) a single character, i.e. the first character that
does not match the format specifier.

For instance, if the input to the "%d" format is "silly", scanf
notices that an integer cannot start with 's', pushes back the 's'
into the input stream, and returns unsuccessfully.

If the input is "123xyz", scanf notices that "123" is the longest
prefix that matches the "%d" format, and that "123x" doesn't match
the format, so "123" is converted to an int, and the 'x' is pushed
back into the input stream.
 
Å

å´å¹³

在 2012å¹´7月17日星期二UTC+8下åˆ2æ—¶23分47秒,Ike Naar写é“:
On 2012-07-17, ?? <[email protected]> wrote:
> int get_int(void)
> {
> int input;
> char ch;
> while(scanf("%d",&input)!=1)
> {
> while((ch=getchar())!='\n')
> putchar(ch);
> printf(" is not an integer.\nPlease enter an integer value, such as 25,-178 or 3: ");
> }
> return input;
> }
>
> ///////////////////////////////////////////////////
> Here is the code from C Primer Plus, when the input is not an integer, it print the wrong input.
> My question is, if scanf() gets the wrong input, it doesn't through out the wrong input, but give it to next receiver?

That is correct, but scanf only has to "unread" (push back into
the input stream) a single character, i.e. the first character that
does not match the format specifier.

For instance, if the input to the "%d" format is "silly", scanf
notices that an integer cannot start with 's', pushes back the 's'
into the input stream, and returns unsuccessfully.

If the input is "123xyz", scanf notices that "123" isthe longest
prefix that matches the "%d" format, and that "123x" doesn't match
the format, so "123" is converted to an int, and the 'x' is pushed
back into the input stream.

Sorry for reply so late, and thanks for your answer. I get it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top