scanf()vs gets() [英] scanf() vs gets()

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

问题描述

好的我有2个类似的程序,为什么第一个工作,第二个工作

不?基本上问题是程序似乎忽略了获取调用,如果

它是在scanf调用之后发生的。请任何事情,即使是一个暗示将真的

有用,我不能为我的生活看到为什么第二个前卫不会工作...


在scanf之前得到


代码:------------------------------------ ---------------------------------

--------- -

#include< stdio.h>


int a;

char aaa [50];


main()

{

printf("输入字符串\ n");

得到(aaa);

printf("%s \ nn",aaa);


printf("输入数字\ n" );

scanf("%d"& a);

printf("%d",a);

}

----------------------------------------- ---------------------------------

------

scanf之前获得

代码:-------------------------------- -------------------------------------

----- ------

#include< stdio.h>


int a;

char aaa [50] ;


main()

{

printf("输入数字\ nn);;

scanf("%d"& a);

printf("%d",a);


printf( 输入一个字符串\ n;);

得到(aaa);

printf("%s \ n",aaa);

}

OK I have 2 similar programmes, why does the first one work and the second does
not? Basically the problem is that the program seems to ignore the gets call if
it comes after a scanf call. Please anything even a hint would be really
helpful, I cant for the life of me see why the 2nd prog wont work...

gets before scanf

code:---------------------------------------------------------------------
-----------
#include <stdio.h>

int a;
char aaa[50];

main()
{
printf("Enter a string\n");
gets(aaa);
printf("%s\n",aaa);

printf("Enter a number\n");
scanf("%d",&a);
printf("%d",a);
}
--------------------------------------------------------------------------
------
scanf before gets
code:---------------------------------------------------------------------
-----------
#include <stdio.h>

int a;
char aaa[50];

main()
{
printf("Enter a number\n");
scanf("%d",&a);
printf("%d",a);

printf("Enter a string\n");
gets(aaa);
printf("%s\n",aaa);
}

推荐答案

on ************* @ aol.comn ojunk(Teh Charleh)写道:
on*************@aol.comnojunk (Teh Charleh) writes:
#include< stdio。 h>

int a;
char aaa [50];


应该是局部变量。没有理由让它们成为全球性的。

main()


你应该明确写出`int main(void)'。

{
printf("输入数字\ n);;
scanf("%d"& a);
printf("%d",a );

printf("输入一个字符串\ n");
得到(aaa);
printf("%s \ n",aaa);


你应该在这里明确写出return 0;。

}
#include <stdio.h>

int a;
char aaa[50];
Should be local variables. No reason for them to be global.
main()
You should write `int main(void)'' explicitly.
{
printf("Enter a number\n");
scanf("%d",&a);
printf("%d",a);

printf("Enter a string\n");
gets(aaa);
printf("%s\n",aaa);
You should write `return 0;'' explicitly here.
}




问题是scanf()的%d说明符将读取一个整数

然后停止。它不会读取空白区域,包括

与你按Enter

键对应的新行字符。然后gets()将读取换行符并提供一个

空字符串作为aaa []。


顺便说一句,不要使用得到()。它不能安全使用,除了Dan Pop的



-

什么适合主人不适合新手。

你必须在超越结构之前理解道。

- 编程之道



The problem is that scanf()''s %d specifier will read an integer
and then stop. It won''t read following white space, including
the new-line character that corresponds to you pushing the Enter
key. Then gets() will read the new-line character and provide an
empty string as aaa[].

By the way, don''t use gets(). It cannot be used safely, except
by Dan Pop.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming




Teh Charleh <有关************* @ aol.comnojunk>在消息中写道

news:20 *************************** @ mb-m29.aol.com。 ..

"Teh Charleh" <on*************@aol.comnojunk> wrote in message
news:20***************************@mb-m29.aol.com...
好的我有2个类似的程序,为什么第一个工作,第二个
不工作?基本上问题是,如果在scanf调用之后,程序似乎忽略了获取
调用。请任何事情,即使是一个暗示真的很有帮助,我不能为我的生活看到为什么第二个前卫不会工作...

在scanf之前获得

码: - - - - - - - - - - - - - - - - - - - - - - - - ---------------------
-----------
#include< stdio.h>

int a;
char aaa [50];

main()


int main(void)

{
printf("输入字符串\ n");
获取(aaa);


*从不*使用''gets()''。永远,永远,永远,永远不会使用它。永远。

期间。 *不能*安全使用。永远。请改用''fgets()'。


更多信息。

printf("%s \ n",aaa);

printf(输入数字\ n);
scanf("%d"& a);
printf("%d",a);


返回0; / * main()是* required *返回int * /

}
---------------------- -------------------------------------------------- -
------

scanf之前获得

代码:----------------- -------------------------------------------------- -
-----------
#include< stdio.h>

int a;
char aaa [50];

main()


int main(无效)

{
printf("输入数字\ n;
scanf(%d,& a);


''scanf()''将首先跳过任何空白字符,然后

开始提取字符,直到非空白字符为
$ b遇到$ b(未提取)或错误(例如,如果为''%d''说明符读取非数字字符,则为
),

或end到达流。


假设您的输入对于%d是正确的,并且最后一个

输入字符(一个数字)后跟换行符(例如

按键盘上的''Enter''键),然后数字将被提取,转换为二进制数字和结果值

存储在对象'a'中。导致

''scanf()''停止解析的换行符仍将在输入流中,

并将提供给下一个输入请求。


我相信这就是你的情况。

printf("%d",a);
printf(输入一个字符串\ n);
得到(aaa);


如果我的上述假设成立,则调用''gets()''

将检索(并丢弃)等待的换行符,存储

a aaa [0]中的零字符,并返回。

printf("%s \ n",aaa);


返回0;

}
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please anything even a hint would be really
helpful, I cant for the life of me see why the 2nd prog wont work...

gets before scanf

code:---------------------------------------------------------------------
-----------
#include <stdio.h>

int a;
char aaa[50];

main()
int main(void)
{
printf("Enter a string\n");
gets(aaa);
*Never* use ''gets()''. Never, never, never, ever use it. Ever.
Period. It *cannot* be used safely. Ever. Use ''fgets()'' instead.

More below.
printf("%s\n",aaa);

printf("Enter a number\n");
scanf("%d",&a);
printf("%d",a);
return 0; /* main() is *required* to return an int */
}
--------------------------------------------------------------------------
------
scanf before gets
code:---------------------------------------------------------------------
-----------
#include <stdio.h>

int a;
char aaa[50];

main()
int main(void)
{
printf("Enter a number\n");
scanf("%d",&a);
''scanf()'' will first skip over any whitespace characters, then
start extracting characters until a nonwhitespace character is
encountered (which is not extracted), or an error (e.g.
if a nondigit character is read for ''%d'' specifier) occurs,
or end of stream is reached.

Assuming that your input is correct for %d, and that the last
input character (a digit) was followed by a newline (e.g.
pressing ''Enter'' key on a keyboard), then the digits will
be extracted, converted to binary and the resulting value
stored in the object ''a''. The newline character which caused
''scanf()'' to stop parsing will still be in the input stream,
and will be supplied to the next input request.

I believe this is what''s happening in your case.
printf("%d",a);

printf("Enter a string\n");
gets(aaa);
If my above assumptions hold, then this invocation of ''gets()''
will retrieve (and discard) the waiting newline character, store
a zero character in aaa[0], and return.
printf("%s\n",aaa);
return 0;
}




您的问题似乎是因为不了解' 'scanf()''

不会提取任何空格字符,这些字符可能是在提取数据项之后发生的(例如''int'',''float''等等。


再次,*永远*使用''gets()''。永远。


您还应该检查''scanf()''的返回值是否为

错误。


虽然我已经在这里解释了,但请注意你的问题的答案是在C FAQ中,在发布之前应该检查:
http://www.eskimo.com/~scs/C-faq/q12 .18.html


我所表达的关于''gets()''的观点也在

C常见问题中:
http://www.eskimo.com /〜scs/C-faq/q12.23.html


-Mike



Your problem seems to result from not understanding that ''scanf()''
does not extract any whitespace characters which might occur after
an extracted data item (e.g. an ''int'', ''float'', etc.).

And again, *NEVER* use ''gets()''. EVER.

You should also check the return value from ''scanf()'' for
errors.

Although I''ve explained it here, note that the answer to your
question is in the C FAQ, which should be checked before posting:
http://www.eskimo.com/~scs/C-faq/q12.18.html

Also the sentiments I expressed about ''gets()'' are also in the
C FAQ:
http://www.eskimo.com/~scs/C-faq/q12.23.html

-Mike


>问题是scanf()的%d说明符将读取整数
>The problem is that scanf()''s %d specifier will read an integer
然后停止。它不会读取空格,包括与你按Enter键相对应的新行字符。然后gets()将读取换行符并提供一个空字符串作为aaa []。


这非常有意义,并解释了我的问题的行为。现在请问这个问题我该如何告诉gets()忽略新行字符
扫描后输入
...

顺便说一句,不要使用gets()。它不能安全使用,除了Dan Pop。
and then stop. It won''t read following white space, including
the new-line character that corresponds to you pushing the Enter
key. Then gets() will read the new-line character and provide an
empty string as aaa[].
This makes perfect sense and explains the behavior of my problem. This now begs
the question how do I go about telling gets() to ignore the newline charecter
input after the scanf...
By the way, don''t use gets(). It cannot be used safely, except
by Dan Pop.




我这里的书C编程简单的步骤肯定是推荐它

scanf如果我想输入多个单词作为字符串!



My book here C programming in easy steps is definately recommending it over
scanf if I want to input more than one word as a string!


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

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