According to a top answer on SO this is what I should do to convert an integer to a string in C:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>
#define digits(x) ((int)((ceil(log10(abs(x)))+1)*sizeof(char)))
int main () {
    int a = 345908220;
    long b = 23094809284358;
    unsigned int c = 3456789234;
    uint8_t d = 242;
    int64_t e = -840958202029834;
    int dstr_len = digits(a) + digits(b) + digits(c) +
                   digits(d) + digits(e) + 5;
    char dstr[dstr_len];
    sprintf(dstr, "%d %ld %u %u %" PRIu64, a, b, c, d, e);
    printf("%s\n", dstr);
    return 0;
}
However, this seems ridiculously inefficient. I have to link my program to libmath, and make three math calls for every integer that I want to print. Also note that I had to add 5 to my buffer and not just 1 for the NUL terminator, by counting the number of spaces in my format string. This also seems error-prone, and could lead to a buffer overflow.
So, is there any nice, standard function, that will compute the size of my buffer for me?
I'm trying to write secure C.
 
     
     
     
    