scanf怀疑 [英] scanf doubt

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

问题描述




对于下面粘贴的程序,scanf不等待第二个用户

输入。

可以有人建议这个理由吗?


无效

main()

{

char c;


scanf("%c",& c);

printf("%c\ n",c);


scanf("%c"& c);

printf("%c\ n",c);


返回(TRUE);

}


输出:

a

a

Hi,

For the program pasted below, scanf is not waiting for the second user
input.
Can someone suggest reason for this?

void
main()
{
char c;

scanf("%c", &c);
printf("%c\n", c);

scanf("%c", &c);
printf("%c\n", c);

return(TRUE);
}

output:
a
a

推荐答案

deepak< de ********* @ gmail.comwrote:
deepak <de*********@gmail.comwrote:

对于下面粘贴的程序,scanf不等待第二个用户

输入。

有人可以说明这个原因吗?
For the program pasted below, scanf is not waiting for the second user
input.
Can someone suggest reason for this?



你错过了


#include< stdio.h>

You''re missing

#include <stdio.h>


void

main()
void
main()



make


int main(void)


特别是因为你的main()函数返回一个值。

make that

int main( void )

especially since your main() function returns a value.


{

char c;
{
char c;


scanf("%c",& c);

printf("%c\ n" ;, C);
scanf("%c", &c);
printf("%c\n", c);



这里你读了一个字母,然后打印出来。

Here you read a single char which then gets printed out.


scanf("%c" ;,& c);

printf("%c\ n",c);
scanf("%c", &c);
printf("%c\n", c);



在这里你读了第二个也打印出来的字符。

只是这个字符是char的字符串< ENTERkey,

''\ n'',你没有直接看到。所以你的程序实际上是
读取两个字符,只是第二个不是你想要的那个。

And here you read a second char that also gets printed out.
It''s just that this char is the char for the <ENTERkey,
''\n'', which you don''t see directly. So your program actually
reads in two chars, just that the second isn''t one you were
expecting.


return(TRUE);
return(TRUE);



什么是真?最好返回0(总是表示

成功)或者按照

< stdlib.h>中定义的EXIT_SUCCESS或EXIT_FAILURE。

What''s ''TRUE''? Better return either 0 (which always indicates
success) or either EXIT_SUCCESS or EXIT_FAILURE as defined in
<stdlib.h>.


}
}



问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


8月13日上午9:46,deepak< deepakpj .. 。@ gmail.comwrote:
On Aug 13, 9:46 am, deepak <deepakpj...@gmail.comwrote:




对于下面粘贴的程序,scanf不等待第二个用户

输入。

有人可以提出理由吗?
Hi,

For the program pasted below, scanf is not waiting for the second user
input.
Can someone suggest reason for this?



缓冲区没有刷新?,在stdin上应用fflush(3)。

另外,scanf从用户那里获取字符输入是不安全的,

尝试getc(3)超过stdin。

The buffer isn''t flushed?, apply fflush(3) over stdin.
Also, scanf to get chars from the user input isn''t safe,
try getc(3) over stdin.


>

void

main()

{

char c;


scanf("%c" ,& c);

printf("%c\ n",c);


scanf("%c",& c);

printf("%c\ nn,c);


返回(TRUE);


}


输出:

a

a
>
void
main()
{
char c;

scanf("%c", &c);
printf("%c\n", c);

scanf("%c", &c);
printf("%c\n", c);

return(TRUE);

}

output:
a
a



问候,

DMW

Regards,
DMW


Daniel Molina Wegener< dm*@coder.clwrote:
Daniel Molina Wegener <dm*@coder.clwrote:

8月13日上午9:46,deepak< deepakpj ... @ gmail.comwrote:
On Aug 13, 9:46 am, deepak <deepakpj...@gmail.comwrote:




对于下面粘贴的程序,scanf没有等待对于第二个用户

输入。

有人可以提出理由吗?
Hi,

For the program pasted below, scanf is not waiting for the second user
input.
Can someone suggest reason for this?


缓冲区没有刷新?,在stdin上应用fflush(3)。
The buffer isn''t flushed?, apply fflush(3) over stdin.



fflush()只能用于输出流,使用它

输入是一个Windows扩展,它不能工作

其他系统。

fflush() can only be used for output streams, using it
for input is a Windows extension which doesn''t work on
other systems.


另外,scanf从用户输入中获取字符是不安全的,

尝试getc(3)over stdin。
Also, scanf to get chars from the user input isn''t safe,
try getc(3) over stdin.



在读取字符时,scanf()的不安全性

用户输入了什么?一个不同的问题是,如果它只是在阅读单个字符时使用getc(),fgetc()或

getchar()更有效...


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http:// toerring .de


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

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