 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
guoliang Guest
|
Posted: Fri May 11, 2007 9:12 am Post subject: scanf() quesion? |
|
|
help:
#include<stdio.h>
int main(void)
{
int a;
char b;
printf("int=");
scanf("%d",&a);
fflush(stdin);
printf("char=");
scanf("%c",&b);
printf("a=%d, str=%c\n",a,b);
return 0;
}
input:1 and return;
output:char=a=1, b=
how can i assigment :a=1,b='x'? |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Fri May 11, 2007 9:12 am Post subject: Re: scanf() quesion? |
|
|
guoliang said:
| Quote: | help:
#include<stdio.h
int main(void)
{
int a;
char b;
printf("int=");
|
If you want the prompt to appear before the program blocks for input,
ensure that this happens, either by completing the line or by flushing
the stream with fflush(stdout); this is because the standard output
stream is typically line-buffered.
When you call scanf, you are asking for a resource (in this case,
information from the standard input stream). Whenever you ask for a
resource, the possibility exists that you don't get what you ask for,
and you should anticipate and deal with this possibility.
The behaviour of fflush is defined only for streams open for output or
update. Since stdin is not such a stream, you invoke undefined
behaviour.
| Quote: | printf("char=");
scanf("%c",&b);
printf("a=%d, str=%c\n",a,b);
return 0;
}
input:1 and return;
output:char=a=1, b=
|
The only reason this isn't impossible is that the fflush(stdin) rendered
the behaviour of your program undefined.
| Quote: | how can i assigment :a=1,b='x'?
|
a = 1;
b = 'x';
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: scanf() quesion? |
|
|
Chris Torek said:
| Quote: | If you have access to a Linux or Unix box, try:
./prog | tee save_output
and observe the fact that printf() output does not appear as
desired unless the fflush(stdout) calls are in place.
|
Ouch! A lesson learned.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Mon May 14, 2007 9:12 am Post subject: Re: scanf() quesion? |
|
|
guru.jois (AT) gmail (DOT) com said:
| Quote: | On May 11, 12:47 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
guoliang said:
snip |
| Quote: | scanf("%d",&a);
When you call scanf, you are asking for a resource (in this case,
information from the standard input stream). Whenever you ask for a
resource, the possibility exists that you don't get what you ask for,
and you should anticipate and deal with this possibility.
|
<snip>
| Quote: | or do this
scanf (" %c", &c); // space before %c..
|
See above.
I sometimes think the ability to read is becoming unfashionable.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
guru.jois@gmail.com Guest
|
Posted: Mon May 14, 2007 9:12 am Post subject: Re: scanf() quesion? |
|
|
On May 11, 12:47 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
| Quote: | guoliang said:
help:
#include<stdio.h
int main(void)
{
int a;
char b;
printf("int=");
If you want the prompt to appear before the program blocks for input,
ensure that this happens, either by completing the line or by flushing
the stream with fflush(stdout); this is because the standard output
stream is typically line-buffered.
scanf("%d",&a);
When you call scanf, you are asking for a resource (in this case,
information from the standard input stream). Whenever you ask for a
resource, the possibility exists that you don't get what you ask for,
and you should anticipate and deal with this possibility.
fflush(stdin);
The behaviour of fflush is defined only for streams open for output or
update. Since stdin is not such a stream, you invoke undefined
behaviour.
printf("char=");
scanf("%c",&b);
printf("a=%d, str=%c\n",a,b);
return 0;
}
input:1 and return;
output:char=a=1, b=
The only reason this isn't impossible is that the fflush(stdin) rendered
the behaviour of your program undefined.
how can i assigment :a=1,b='x'?
a = 1;
b = 'x';
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
|
or do this
scanf (" %c", &c); // space before %c.. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|