C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

scanf() quesion?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
guoliang
Guest





PostPosted: Fri May 11, 2007 9:12 am    Post subject: scanf() quesion? Reply with quote



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





PostPosted: Fri May 11, 2007 9:12 am    Post subject: Re: scanf() quesion? Reply with quote



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.

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.

Quote:
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.

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





PostPosted: Sat May 12, 2007 9:11 am    Post subject: Re: scanf() quesion? Reply with quote



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





PostPosted: Mon May 14, 2007 9:12 am    Post subject: Re: scanf() quesion? Reply with quote

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





PostPosted: Mon May 14, 2007 9:12 am    Post subject: Re: scanf() quesion? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.