atoi查询 [英] atoi query

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

问题描述




我写了这段代码来帮助我学习C但我不知道为什么gcc -

Wall给了我一个我编译时出错


基本上我想读一个字符然后是一个数字然后

操纵这个数字。

我'尝试过scanf,例如scanf("%c%d"& letter,& number);但是当我输入
输入2个字母而不是1个字母和1个数字时,我得到零值

为第二个值,因为它与scanf不匹配期待我这样做我想要fgets。


#include< stdio.h>

#define LINESIZE 4


int main()

{

char line [LINESIZE];

int temp,temp2 ;


printf("请输入一个字母和一个值:");

fgets(line,LINESIZE,stdin);


printf(输入此行:%s \ n,行);


printf(" line [0] is%c \ n",[0]);

printf(" line [1] is%c\ n,line [1]);

temp = atoi(第[1]行);


printf(" temp is%d \ nn",temp);

temp2 = temp + 5;

printf(" temp2 is%d \ nn",temp2);


返回0;

}


然后在编译时,

gcc -Wall getloc_val.c

getloc_val.c:在函数`main''中:

getloc_val.c:17:警告:隐式声明函数`atoi''


我正在提到的这本书提到我可以这样做,但为什么我会收到隐含的警告?

Pat

推荐答案

pt ***** @ gmail.com 写道:
pt*****@gmail.com wrote:

我尝试过scanf,例如scanf("%c%d",& letter,& number);但是当我输入
输入2个字母而不是1个字母和1个数字时,我得到零值

为第二个值,因为它与scanf不匹配期待我是这样的。
尝试fgets。
I''ve tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn''t match what scanf is expecting so I''m
trying fgets.



是的,这是正确的,这是正确的解决方案。

Yup, that''s correct, and that''s the right solution.


# include< stdio.h>

#define LINESIZE 4
#include <stdio.h>
#define LINESIZE 4



(这是一个相当低的值。你真的期望得到A24

_all_时间?你可能想要处理ABC23,A 24和A7632与

a比输入不理解更有用的错误信息,并且

你不希望你的程序接受后者作为A76。)

(This is a rather low value, though. Do you really expect to get A24
_all_ the time? You probably want to handle ABC23, A 24, and A7632 with
a slightly more useful error message than "input not understood", and
you don''t want your program to accept the latter as A76.)


int main()

{

char line [LINESIZE];

int temp,temp2;


printf(" Please enter a letter and a值:");

fgets(行,LINESIZE,stdin);


printf("输入此行:%s \ n" ;,line);


printf(line [0] is%c\ n,line [0]);

printf( line [1]是%c \ n, line [1]);

temp = atoi(第[1]行);


printf(" temp is%d \ n", temp);

temp2 = temp + 5;

printf(" temp2 is%d \ nn",temp2);


返回0;

}


然后在编译时,

gcc -Wall getloc_val.c

getloc_val.c:在函数`main''中:

getloc_val.c:17:警告:隐式声明函数`atoi''
int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;
}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main'':
getloc_val.c:17: warning: implicit declaration of function `atoi''



然而,这不是正确的解决方案。

This, however, is not the right solution.


这本书我提到我可以做到这一点
The book I''m refering to mentions that I can do this



然后这本书是错误的(或者最好不完整),或者你错过了

位。

Then either the book is wrong (or at best incomplete), or you missed a
bit.


但为什么我得到隐含的警告?
but why am I getting the implicit warning ?



因为你没有宣布atoi()。就像你必须#include

< stdio.hfor printf()和< string.hfor strcpy()一样,你还需要

#include< stdlib.hfor atoi()。

但是你不想使用atoi()。它有一些问题,因为它的错误处理并不像不存在那么糟糕。使用strtol()

代替;你会发现它在< stdlib.has中声明了。


Richard

Because you didn''t declare atoi(). Just as you have to #include
<stdio.hfor printf(), and <string.hfor strcpy(), you also have to
#include <stdlib.hfor atoi().
But you don''t want to use atoi(), anyway. It has a few problems, because
its error handling is not so much bad as non-existent. Use strtol()
instead; you''ll find it declared in <stdlib.has well.

Richard


pt ***** @ gmail.com 写道,于18/04/07 12:57:
pt*****@gmail.com wrote, On 18/04/07 12:57:




我写了这段代码来帮助我学习C但是我不知道为什么gcc -

Wall给了我编译
时出错
Hi,

I have written this code to help me learn C but I''m not sure why gcc -
Wall is giving me an error when I compile



你应该使用-ansi -pedantic -Wall -O -W或者对于不完整的C99

支持" -std = c99"而不是-ansi。这将产生更多可疑实践的警告。

You should be using "-ansi -pedantic -Wall -O -W" or for incomplete C99
support "-std=c99" instead of -ansi. This will generate warnings for a
lot more questionable practices.


基本上我想读一个字符然后一个数字然后

操纵数字。

我试过scanf,例如scanf("%c%d",& letter,& number);但是当我输入
输入2个字母而不是1个字母和1个数字时,我得到零值

为第二个值,因为它与scanf不匹配期待
Basically I want to read in a character then a number and then
manipulate the number.
I''ve tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn''t match what scanf is expecting



这比那更糟糕。它没有写任何数字,它包含0 b $ b仅仅是运气。 scanf的返回值将告诉

你只进行了一次转换。

It''s worse than that. It won''t have written anything to number, that it
contained 0 was merely luck. The return value of scanf will have told
you it only did one conversion.


所以我是

尝试fgets。
so I''m
trying fgets.



这是一个更好的解决方案。

This is a much better solution.


#include< stdio.h>

#define LINESIZE 4
#include <stdio.h>
#define LINESIZE 4



这是一个非常短的行限制!

That''s a very short line limit!


int main()

{

char line [LINESIZE];

int temp,temp2;


printf(请输入一个字母和一个值:);
int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");



您应该刷新标准输出,否则在等待输入之前上面的提示可能不会显示为


You should flush stdout otherwise the above prompt might not be
displayed before waiting for input.


fgets(line,LINESIZE,stdin);


printf("输入此行:%s \ n",line);


printf(line [0] is%c\ n,line [0]);

printf(" line [1] is% c\\\
,行[1]);
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);



我想你会发现你需要确定数字的开头

(isdigit会有所帮助,但记得允许标志)在尝试将
转换成它之前。

I think you will find that you need to identify the start of the number
(isdigit will help, but remember to allow for signs) before trying to
convert it.


temp = atoi(line [1]);


printf(temp is%d \ nn,temp);

temp2 = temp + 5;

printf(" temp2 is%d \ n",temp2);


返回0;

}


然后在编译时,

gcc -Wall getloc_val.c

getloc_val.c:函数`main'':

getloc_val.c:17:警告:隐式声明功能'atoi''


这本书我指的是我可以这样做,但为什么我会收到隐含警告?
得到隐含的警告?
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;
}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main'':
getloc_val.c:17: warning: implicit declaration of function `atoi''

The book I''m refering to mentions that I can do this but why am I
getting the implicit warning ?



atoi在stdlib.h中声明,如果你的书没有告诉你那么它

可能是时候得到一本新书了。此外,通常最好使用

strtol,这样你就有机会进行错误检查。

-

Flash Gordon

atoi is declared in stdlib.h, if your book did not tell you this then it
is probably time to get a new book. Also, it is generally better to use
strtol so that you have a chance to do error checking.
--
Flash Gordon


pt*****@gmail.com 说:

< snip>
pt*****@gmail.com said:
<snip>

char line [LINESIZE];
char line[LINESIZE];



< snip>

<snip>


temp = atoi(line [1]);
temp = atoi(line[1]);



其他人已经注意到你应该使用更长的缓冲区并且

strtol会比atoi更好地满足你的需求,但似乎没有人

注意到你对atoi的争论存在问题。


atoi(当你咬紧牙关并选择使用它时,

strtol)必须提供一个字符串,而不是字符,用于将

转换为整数。 line [1]不是字符串,而是单个字符。字符串是

由第一个空字符终止的字符序列。如果你想要告诉atoi从你的

字符串的第二个字符开始,你可以通过传递第二个字符的地址

的字符串,如下所示:


temp = atoi(& line [1]);


顺便说一下,将单个字符转换为数字很容易。提供

/它是一个数字,你可以简单地从它中减去''0',因为''0'','1'',

' 2,3,......8,9(不像字母表,唉)保证

有连续和升序编码点。换句话说,数字

字符在他们自己的一个小块中,并从该块中的任何值减去''0'

将给你"距离"从''0'开始。

因此,''4'' - ''0''给你4,''7'' - ''0''给你7等等。 br $>

-

Richard Heathfield

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

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

Others have noted that you should be using a longer buffer and that
strtol would serve your needs better than atoi, but nobody appears to
have noticed the problem with your argument to atoi.

atoi (and, when you bite the bullet and choose to use it instead,
strtol) must be supplied with a string, not a character, for converting
to an integer. line[1] is not a string, but a single char. A string is
a sequence of characters terminated by the first null character. If you
want to tell, say, atoi to start at the second character of your
string, you can do this by passing the address of that second character
of the string, like this:

temp = atoi(&line[1]);

Incidentally, to convert a single character to a digit is easy. Provided
it /is/ a digit, you can simply subtract ''0'' from it, because ''0'', ''1'',
''2'', ''3'', ... ''8'', ''9'' (unlike the alphabet, alas) are guaranteed to
have contiguous and ascending coding points. In other words, the digit
characters are in a little block all of their own, and subtracting ''0''
from any value in that block will give you the "distance" from ''0''.
Thus, ''4'' - ''0'' gives you 4, ''7'' - ''0'' gives you 7, etc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


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

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