scanf()问题? [英] scanf() quesion?

查看:60
本文介绍了scanf()问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助:


#include< stdio.h>


int main(无效)

{

int a;

char b;


printf(" int =");

scanf("%d",& a);

fflush(stdin);

printf(" char =");

scanf("%c"& b);


printf(" a =%d,str =%c \ n",a,b );

返回0;

}

输入:1并返回;

输出:char = a = 1,b =


我该如何分配:a = 1,b =''x''?

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''?

推荐答案

guoliang说:
guoliang said:

help:


#include< stdio.h>


int main(无效)

{

int a;

char b;


printf(" int =");
help:

#include<stdio.h>

int main(void)
{
int a;
char b;

printf("int=");



如果你想让提示出现在程序块输入之前,

确保通过完成该行或通过冲洗

fflush流(stdout);这是因为标准输出

流通常是行缓冲的。

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);
scanf("%d",&a);



当你调用scanf时,你要求一个资源(在这种情况下,
来自标准输入流的
信息)。每当你要求一个

资源时,你可能不会得到你要求的东西,

你应该预料到并处理这种可能性。 />

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);
fflush(stdin);



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);

返回0;

}

输入:1并返回;

输出:char = a = 1,b =
printf("char=");
scanf("%c",&b);

printf("a=%d, str=%c\n",a,b);
return 0;
}
input:1 and return;
output:char=a=1, b=



这是不可能的唯一原因是fflush(stdin)渲染了你的b / b
的行为程序未定义。

The only reason this isn''t impossible is that the fflush(stdin) rendered
the behaviour of your program undefined.


我该如何分配:a = 1,b =''x''?
how can i assigment :a=1,b=''x''?



a = 1;

b =''x'';


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

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.


guoliang写道:
guoliang wrote:

帮助:


#include< stdio.h>


int main(无效)

{

int a;

char b;


printf(" int =");

scanf("%d",& a);

fflush(stdin);

printf(" char =");

scanf("%c"& b);


printf(" a =%d,str =%c \ n" ,a,b);

返回0;

}

输入:1并返回;

输出: char = a = 1,b =


我怎么能分配:a = 1,b =''x''?
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''?



虽然我对你的输入方法并不十分重视,但我们会用这个来工作

。 (顺便说一句,''赋值''是一个名词,''assign''是动词,

我们通常在赋值语句的上下文中使用该术语。

但是让它通过。)


#include< stdio.h>


int main(无效)

{

int a;

char b = 0;


printf(" int =");

fflush(stdout); / * fflush仅在_output_

流上定义,你实际上确实需要它在这里
,但不是因为你认为这是b
。 * /

scanf("%d",& a);

while(getchar()!=''\ n''){

/ *通过行尾吃掉字符* /}

printf(" char =");

fflush(stdout); / *你需要它* /

scanf("%c",& b);

printf(" a =%d,b =' '%c''\ n,a,b);

返回0;

}

While I don''t think very highly of your approach to input, we''ll work
with that. (By the way, ''assignment'' is a noun, ''assign'' is the verb,
and we usually use that term for the context of assignment statement.
But let that pass.)

#include<stdio.h>

int main(void)
{
int a;
char b = 0;

printf("int=");
fflush(stdout); /* fflush is defined only on _output_
streams, and you actually do need it
here, but not for the reason you
think. */
scanf("%d", &a);
while (getchar() != ''\n'') {
/* eat chars through end-of-line */ }
printf("char=");
fflush(stdout); /* and you need it here */
scanf("%c", &b);
printf("a=%d, b=''%c''\n", a, b);
return 0;
}


在int值的第一次scanf之后,你必须跳过一个字节,可能是\r

或空格等。

这里是样本:


int a;

char b;

printf(" Input:\ n");

scanf(" ;%d \\\%c",& a,& b);

printf("%d,%c\ n",a,b);

返回0;

这里接受以下输入:

1?¢

5 + [输入密钥]

A + [输入密钥]


2?¢

1 B + [输入密钥]

但它是使用scanf输入数据的坏主意。

On 5 ??11è?,????3ê±11·?,guoliang < poster .... @ gmail.comwrote:
After the first scanf of int values,you must skip one byte,may be \r
or space etc.
here is sample:

int a;
char b;
printf("Input:\n");
scanf("%d\r%c",&a,&b);
printf("%d,%c\n",a,b);
return 0;
here accept the following input:
1?¢
5 + [enter key]
A + [enter key]

2?¢
1 B+[enter key]
but it is bad idea to input data using scanf.
On 5??11è?, ????3ê±11·?, "guoliang" <poster....@gmail.comwrote:

help:


#include< stdio.h>


int main(无效)

{

int a;

char b;


printf(" int =");

scanf("%d"& a);

fflush( stdin);

printf(" char =");

scanf("%c",& b);


printf(a =%d,str =%c \ n,a,b);

返回0;}


输入:1并返回;

输出:char = a = 1,b =


我该如何分配:a = 1,b ='' X''?
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''?



这篇关于scanf()问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆