正确使用scanf [英] Proper use of scanf

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

问题描述



您好,


我知道很多人都对使用scanf很担心,

因为这样做不恰当可能是危险的。我有

试图找到一个关于所有来龙去脉的好教程

of scanf()但是没有成功。


是否有一个受人尊敬的(由clc人群)书

或教程真正涵盖了scanf的详细信息?


Hello,

I am aware that a lot of people are wary of using scanf,
because doing it improperly can be dangerous. I have
tried to find a good tutorial on all the ins and outs
of scanf() but been unsuccessful.

Is there a well-respected (by the c.l.c crowd) book
or tutorial that really covers scanf in detail?

推荐答案

Lefty Bigfoot写道:
Lefty Bigfoot wrote:

你好,

我知道很多人都对使用scanf很谨慎,
因为做得不好可以很危险我已经试图找到一个关于scanf()的所有细节的好教程但是没有成功。

是否有一个备受尊重的(由clc人群)预订
或详细介绍scanf的教程?

Hello,

I am aware that a lot of people are wary of using scanf,
because doing it improperly can be dangerous. I have
tried to find a good tutorial on all the ins and outs
of scanf() but been unsuccessful.

Is there a well-respected (by the c.l.c crowd) book
or tutorial that really covers scanf in detail?




我不知道。

这里''一个安全使用scanf的方法的两个例子。

如果输入超过LENGTH个字符,

额外的字符将被自动丢弃。

如果您有疑问,我可以尝试解释。

scanf的整个主题有点冗长。


/ * BEGIN anystring.c * /


#include< stdio.h>


#define LENGTH 20

#define str(x)# x

#define xstr(x)str(x)

int main(无效)

{

char string [LENGTH + 1];

int rc;


printf("输入任何字符串或输入空字符串以退出:" ;);

fflush(stdou t);

rc = scanf("%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",string);

if(!feof(stdin)){

getchar();

}

while(rc == 1){

printf(" your string is%s \ n" ;,字符串);

printf(" scanf()返回%d \ n",rc);

printf("输入任何字符串或输入空要退出的字符串:");

fflush(stdout);

rc = scanf("%" xstr(LENGTH)" [^ \ n] %* [^ \ n]",string);

if(!feof(stdin)){

getchar();

}

}

printf(" scanf()返回%d \ n",rc);

返回0;

}


/ * END anystring.c * /

/ * BEGIN grade.c * /

#include< stdlib.h>

#include< stdio.h>


#define LENGTH 3

#define str(x)#x

#define xstr(x)str(x)


int main(无效)

{

int rc;

char数组[LENGTH + 1];

长数;

const char letter [4] =" DCBA";


fputs(" Enter the Numeric grade:",stdout);

fflush(stdout);

rc = scanf (QUOT;%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof(stdin)){

getchar();

}

while(rc == 1){

number = strtol(array,NULL,10);

if(number> 60){

if(number> 99){

number = 99;

}

数组[0] =字母[(数字 - 60)/ 10];

开关(数字%10){

case 0:

案例1:

array [1] ='' - '';

array [2] =''\ 0 '';

休息;

案例8:

案例9:

array [1] ='' +'';

array [2] =''\''';

break;

默认值:

array [1] =''\ 0'';

休息;

}

}其他{

array [0] =''F'';

array [1] =''\ 0'';

}

printf(字母等级为:%s \ n,数组);

fputs("输入数字等级:",stdout);

fflush(stdout);

rc = scanf("%" xstr(LENGTH)" [^ \ n]%* [^ \ n]" ,数组);

if(!feof(stdin)){

getchar();

}

}

返回0;

}


/ * END grade.c * /

- -

pete



I don''t know of one.
Here''s two examples of one way to use scanf safely.
If you enter more than LENGTH characters,
the extra ones will be automatically discarded.
If you have questions, I can try to explain.
The whole topic of scanf is somewhat lengthy.

/* BEGIN anystring.c */

#include <stdio.h>

#define LENGTH 20
#define str(x) #x
#define xstr(x) str(x)

int main(void)
{
char string[LENGTH + 1];
int rc;

printf("Enter any string or enter an empty string to quit: ");
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string was %s\n", string);
printf("scanf() returned %d\n", rc);
printf("Enter any string or enter an empty string to quit: ");
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
}
printf("scanf() returned %d\n", rc);
return 0;
}

/* END anystring.c */
/* BEGIN grade.c */

#include <stdlib.h>
#include <stdio.h>

#define LENGTH 3
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];
long number;
const char letter[4] = "DCBA";

fputs("Enter the Numeric grade: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
number = strtol(array, NULL, 10);
if (number > 60) {
if (number > 99) {
number = 99;
}
array[0] = letter[(number - 60) / 10];
switch (number % 10) {
case 0:
case 1:
array[1] = ''-'';
array[2] = ''\0'';
break;
case 8:
case 9:
array[1] = ''+'';
array[2] = ''\0'';
break;
default:
array[1] = ''\0'';
break;
}
} else {
array[0] = ''F'';
array[1] = ''\0'';
}
printf("The Letter grade is: %s\n", array);
fputs("Enter the Numeric grade: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

/* END grade.c */
--
pete




Le 19/06/2005 00:54,dans 没有************* *************@news.verizon.net

?* Lefty Bigfoot *? <无**** @ awol.info> écrit*:

Le 19/06/2005 00:54, dans no**************************@news.verizon.net,
?*Lefty Bigfoot*? <no****@awol.info> a écrit*:

你好,

我知道很多人都对使用scanf很谨慎,因为做它不恰当可能是危险的。我已经试图找到一个关于scanf()的所有细节的好教程但是没有成功。

是否有一个备受尊重的(由clc人群)预订
或详细介绍scanf的教程?

Hello,

I am aware that a lot of people are wary of using scanf,
because doing it improperly can be dangerous. I have
tried to find a good tutorial on all the ins and outs
of scanf() but been unsuccessful.

Is there a well-respected (by the c.l.c crowd) book
or tutorial that really covers scanf in detail?




是的,ISO 9899-1999标准,第7.19.6.4节。

我可以发誓它在这里得到了很好的尊重:-)


还有POSIX标准,
http://www.opengroup.org/onlinepubs/000095399/

当然也受到尊重。


如果您需要教程,也许K& RC书可以帮助您。


如果您有UNIX或Linux,请尝试 ; man scanf"



Yes, the ISO 9899-1999 Standard, section 7.19.6.4.
I can swear it''s well respected here :-)

There is also the POSIX standard, at
http://www.opengroup.org/onlinepubs/000095399/
certainly well respected too.

If you want a tutorial, maybe the K&R C book will help you.

If you have UNIX or Linux, try "man scanf"


Lefty Bigfoot写道:
Lefty Bigfoot wrote:

我知道很多人都很谨慎使用scanf,
因为不正确地做这件事可能很危险。我已经试图找到一个关于scanf()的所有细节的好教程但是没有成功。

是否有一个备受尊重的(由clc人群)预订
或详细介绍scanf的教程?

I am aware that a lot of people are wary of using scanf,
because doing it improperly can be dangerous. I have
tried to find a good tutorial on all the ins and outs
of scanf() but been unsuccessful.

Is there a well-respected (by the c.l.c crowd) book
or tutorial that really covers scanf in detail?




看看我的信号中的链接。特别是标有

C库的那个,然后是C99一个。


-

关于C的一些有用的参考:

< http://www.ungerhu.com/jxh/clc.welcome.txt>

< http://www.eskimo.com/~scs /C-faq/top.html>

< http://benpfaff.org/writings/clc/off-topic.html>

< http:/ /anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)

< http://www.dinkumware.com/refxc.html> (C库}

< http://gcc.gnu.org/onlinedocs/>(GNU docs)



Take a look at the links in my sig. Especially the one marked
C-library, and then the C99 one.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)


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

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