mouche
mouche

Reputation: 1903

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);

Upvotes: 2

Views: 1415

Answers (2)

dreadwail
dreadwail

Reputation: 15409

Would sprintf be helpful to you? Link: sprintf

Upvotes: 1

Firas Assaad
Firas Assaad

Reputation: 25750

sprintf

Be careful when using it because sprintf is not a safe function and can suffer from buffer overflows.

Upvotes: 8

Related Questions