int get_digits (int num)
   if(num < 10)
      return 1;

   if(num < 100)
      return 2;

    if(num < 1000)
      return 3;

    if(num < 10000)
      return 4;

    if(num < 100000)
      return 5;

    if(num < 1000000)
      return 6;

    if(num < 10000000)
      return 7;

    if(num < 100000000)
      return 8;

    if(num < 1000000000)
      return 9;

    return 10; /* num > 1000000000 */

char *itoa (int n)
    static char temp[10]; // MN that can be replaced by some def?
    int nDigits = 0;
    int i = 0;
    if(n == 0)
        temp[0] = '0';
        temp[1] = '\0';
       return temp; // or just return "0"; ?
    nDigits = get_digits(n); // fast function to count digits..
    temp[nDigits] = '\0'; // ..needed just here

   for(i = n; i >= 1; i /= 10) // whole method stinks
        temp[--nDigits] = ((i % 10) + '0'); // modulo is quite slow
    return temp;

This is my implementation of the infamous function itoa(), which isn't available everywhere and more importantly not available in my environment. Generally, the implementation of this function is ought to be different anyway. Performance and memory optimization is important. This function converts an integer to a string. The function owns the reference to the returned string, which is statically allocated and the caller is responsible to make a copy of the returned string if he plans on changing it or preserving it across subsequent calls.

  • \$\begingroup\$ Some minor comments: You don't need to special case n == 0 (since you will get the correct output with the rest of the code). get_digits (which should be completely eliminated) could possibly be improved a bit with some nested conditionals (if (num < 100) return num < 10 ? 1 : 2;), although smaller numbers will be more common that larger ones. \$\endgroup\$ Jun 12, 2020 at 18:04
  • 1
    \$\begingroup\$ @1201ProgramAlarm the loop at the bottom has i >= 1, therefore the special case for n == 0 is indeed needed. \$\endgroup\$ Jun 13, 2020 at 5:02
  • \$\begingroup\$ @RolandIllig Ah, I missed seeing that. However, you can easily change that loop into i = n; do { ... } while ((i /= 10) >= 1); to avoid the overhead of a probably rarely needed special case. \$\endgroup\$ Jun 13, 2020 at 17:56

1 Answer 1


The code almost works.

To make it work in all cases, test the program with Valgrind, which detects undefined behavior because of invalid memory access. This will prove that the buffer needs to be 11 bytes long, not only 10.

What about platforms where int has 64 bits instead of just 32? For these you need a larger buffer. Until then, you should use a compile-time assertion (static_assert) to ensure this implicit assumption.

What about negative numbers? -6 is a valid integer as well, and it should be converted appropriately.

If this function is the bottleneck of your whole program because it is too slow, have a look at how the Go programming language converts integers to strings. It's in the strconv package and uses lots of nice tricks to cut down the number of integer divisions, since that's the most expensive machine instruction in your code.

You can get rid of the get_digits function if you have the end of the string at a fixed address. Start with:

char *p = buf + sizeof buf - 1;
*p = '\0';

and then continue to fill the buffer from right to left by doing *(--p) = '0' + digit. At the end just return p, which will point to the first digit.

The return type should be const char * instead of char * since the caller is not supposed to do anything to the buffer.

  • \$\begingroup\$ I just don't like referring to "Go" in a way that suggests that I (or everyone) actually know this language and can read it. Please talk C \$\endgroup\$
    – Edenia
    Jun 14, 2020 at 15:12
  • \$\begingroup\$ I provided you with the Go code as additional information because it is quite simple to read for a C programmer. In contrast, the C libraries often use hard to read code or just use the straight-forward, unoptimized algorithm: NetBSD, GNU libc, which is quite boring for learning tricks. \$\endgroup\$ Jun 14, 2020 at 16:30
  • \$\begingroup\$ I don't know, personally me, with over 10 years of experience in C, I am completely fine with the way C looks and feels. Also don't want to distract myself with Go, and make an effort to learn it, at least for now. \$\endgroup\$
    – Edenia
    Jun 14, 2020 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.