4 ms·
You can use snprintf to easily achieve any concatentation you'd like. len = snprintf(buffer, buffersize, "%s%s%d", string_1, string_2, int_1); if (len
by jstimpfle 4y ago
You can use snprintf to easily achieve any concatentation you'd like.
len = snprintf(buffer, buffersize, "%s%s%d", string_1, string_2, int_1);
if (len + 1 /*NUL*/ > buffersize)
{
// not enough space
}
You can also use this to dynamically allocate any formatted string
len = snprintf(NULL, 0, ....);
buffersize = len + 1; /* NUL */
buffer = allocate(buffersize);
snprintf(buffer, buffersize, ... /*same args as before*/);
- tom_ 4y agoI like the printf family too. Any time you're doing a bunch of strcat or whatever it's almost always massively easier to use a format string to get the same result. Very easy to get the desired width/precision/alignment, and if you need numbers, printf has your back. It even does the bounds checking for you! (And how often do you get that in C.) It won't be as fast, but it's almost always not a problem, and the nice thing about C and C++ is that the char-by-char route is still available when it is. I like to use asprintf, when available: https://man7.org/linux/man-pages/man3/asprintf.3.html https://man7.org/linux/man-pages/man3/asprintf.3.html - and when not available, I add it, along the lines of the snippet you present. Here's something I've found a useful upgrade to asprintf, as it frees the passed-in buffer after expanding the format string. You can just pass the same char ** repeatedly and it'll update the char * appropriately each time. int xasprintf(char**p,const char *fmt,..) { int n=0; char *p2=nullptr; if(fmt) { va_list v; va_start(v); n=asprintf(&p2,fmt,v); va_end(v); } if(n>=0) { free(*p); *p=p2; } return n; }