2

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

2 Answers 2

8

sprintf

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

Sign up to request clarification or add additional context in comments.

2 Comments

I would upvote this answer were it not for the ridiculous contention that sprintf is not safe. It's perfectly safe if you know what you're doing and people that don't know what they're doing should probably be using BASIC instead of C :-)
Do you recommend snprintf to avoid the risk of buffer overflow?
1

Would sprintf be helpful to you? Link: sprintf

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.