Create a string in C with a format specifier
Is there a standard C function that allows you to build strings using format specifiers?
Right now I'm doing this:
char buffer[256];
char *name = "Fred";
strcpy(buffer, "Hello, ");
strcat(buffer, name);
strcat(buffer, ". How are you today?\n");
Is there a way to add the message to buffer in one function?
Something like this:
makestr(buffer, "Hello, %s. How are you today?\n", name开发者_如何学Go);
sprintf
Be careful when using it because sprintf is not a safe function and can suffer from buffer overflows.
Would sprintf be helpful to you? Link: sprintf
精彩评论