if I want to construct a const char * out of several primitive type arguments, is there a way to build the string using a similar to the printf?
if I want to construct a const char * out of several primitive type
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re probably looking for snprintf.
int snprintf(char *str, size_t size, const char *format, ...);A simple example:
GNU has an example too.
Just to add, you don’t actually need to use
snprintf– you can use the plain oldsprintf(without the size argument) but then it is more difficult to ensure only n characters are written to the buffer. GNU also has a nice function,asprintfwhich will allocate the buffer for you.