2009-08-04

Converting integer to string in ANSI C


const char *base = "BLAH BLAH BLAH";
unsigned int idx = 0;
char *buf = NULL;

buf = (char *) malloc (strlen (base) + sizeof (unsigned int));
sprintf (buf, "%s%d", buf, idx);
printf ("buf: %s\n", buf);


Output
BLAH BLAH BLAH0