首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 > error: `itoa` was not declared in this scope?怎么办?

error: `itoa` was not declared in this scope?怎么办?
EN

Stack Overflow用户
提问于 2018-01-31 06:48:36
回答 2查看 0关注 0票数 0

我有一个名为itoa.cpp的示例c文件,如下所示:

#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;
}

当我使用以下命令编译上述代码时:

gcc itoa.cpp -o itoa

我得到了这个错误:

[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
EN

回答 2

Stack Overflow用户

发布于 2018-01-31 15:29:25

如果您需要内存字符串格式,更好的选择是使用snprintf。从您的例子中可以看出:

#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;
}
票数 0
EN

Stack Overflow用户

发布于 2018-01-31 16:41:41

如果你只对基数10,8或16感兴趣。你可以用sprintf:

sprintf(buf,"%d",i);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档