Improving an itoa function
Incorrect size check
itoa(num, 2, buffer); forms &quot;17&quot;, which needs 3 char, even though only 2 provided.
Perhaps OP meant 2 to be the string length allowed and not the string size needed.  In which case sizeof buffer / sizeof *buffer is not the length allowed.
Missing header
Add #include &lt;stdbool.h&gt;
Missing size check
Corner case: strcpy(buffer, &quot;0&quot;); occurs even if sz == 1.

A simple alternative to buffer overflow concerns is to form the initial characters in a local buffer that is right-sized for worst case (INT_MIN) like char local_buf[12].  Check idx against sz.  Then reverse into the true destination.
// Re-write end-of-code.
// ....
// Prior code does not check `sz` nor change sz as characters are saved in ample sized local_buf

if (idx + 1 &gt; sz) {
  if (sz &gt; 0) buffer[0] = 0;  // Set buffer[] to &quot;&quot; if possible
  return NULL;
}

for (int i=0; i&lt;idx; i++)
    buffer[idx-i-1] = local_buf[i];
buffer[idx] = '\0';
return buffer;


Rather than hard-code 12 use code to scale to the size needed.  E.g.
// Scale by log10(2)
#define INT_STR_SZ (sizeof(int)*CHAR_BIT*301030/1000000 + 3)
char local_buf[INT_STR_SZ];