If you're OK with dynamically allocated memory, you can use asprintf
instead. This function will allocate the proper amount of memory to hold the string.
char *buf;
int result = asprintf(&buf, "%u", x);
if (result == -1) {
perror("asprintf failed");
} else {
...
free(buf);
}