错误:`itoa`不是在这个范围内声明 [英] error: `itoa` was not declared in this scope
本文介绍了错误:`itoa`不是在这个范围内声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下称为itoa.cpp一个示例C文件:
I have a sample c file called itoa.cpp as below:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
return 0;
}
当我编译上面code与下面的命令:
When i compile the above code with the below command:
gcc itoa.cpp -o itoa
我得到这个错误:
i am getting this error:
[root@inhyuvelite1 u02]# gcc itoa.cpp -o itoa
itoa.cpp: In function "int main()":
itoa.cpp:10: error: "itoa" was not declared in this scope
什么是错的这个code?如何摆脱这种?
What is wrong in this code? How to get rid of this?
推荐答案
itoa不是ANSI C标准,你应该避免。下面是一些滚你自己的实现,如果你真的想这样用:
itoa is not ansi C standard and you should probably avoid it. Here are some roll-your-own implementations if you really want to use it anyway:
http://www.strudel.org.uk/itoa/
如果您在内存中字符串格式化需要,更好的选择是使用的snprintf。从你的工作例如:
If you need in memory string formatting, a better option is to use snprintf. Working from your example:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
snprintf(buffer, sizeof(buffer), "%d", i);
printf ("decimal: %s\n",buffer);
return 0;
}
这篇关于错误:`itoa`不是在这个范围内声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文