开发者

error: `itoa` was not declared in this scope

开发者 https://www.devze.com 2023-03-15 06:15 出处:网络
I have a sample c file called itoa.cpp as below: #include <stdio.h> #include <stdlib.h> int main ()

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);
  pr开发者_Go百科intf ("decimal: %s\n",buffer);
  return 0;
}

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

What is wrong in this code? How to get rid of this?


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/

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


If you are only interested in base 10, 8 or 16. you can use sprintf

sprintf(buf,"%d",i);


Look into stdlib.h. Maybe _itoa instead itoa was defined there.

0

精彩评论

暂无评论...
验证码 换一张
取 消