0

I want to use itoa() in C . Which library do I have to download?

I would prefer to include a specific library instead of using my own code, but that may be wrong.

I have seen several examples of how to use itoa() but my problem is how to get the library file.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

4

itoa is not part of ANSI-C. Nor is it part of any POSIX standard. On Linux + gcc, for instance, itoa is not present in stdlib.h. Including stdlib.h (as suggested at one stage in the comments) on such platforms will not fix your problem:

 $ grep -w -R itoa /usr/include
 $

nope - it's not there!

Don't use itoa. Instead, use something portable, for instance snprintf or (if you are fine with gcc only) asnprintf.

If you insist on using itoa, then perhaps #include <stdlib.h> will help; if so the answer to your question would be that on that platform it's part of the standard C library, and not a library you need to download. If it doesn't work, then it's not part of the standard C library, and you should use a portable solution instead.

Also, be wary of itoa anyway. From a random man page on the internet:

itoa does not allocate new memory space for the string, but rather returns a pointer to the internal buffer where the conversion takes place

itoa is not reentrant (therefore not thread safe), and solutions like the first one here cannot be either whilst maintaining the same API. A better solution is the second answer here: Where is the itoa function in Linux?

Community
  • 1
  • 1
abligh
  • 24,573
  • 4
  • 47
  • 84