开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜