Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Intermediate
Mar 8, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

The snprintf() Function

The C99 standard defines a new version of the function sprintf(), namely snprintf(), with the following prototype:

  int snprintf(char * restrict s, size_t n,
                    const char * restrict format, ...);

The additional argument n (highlighted) contains the maximal number of characters that the snprintf() copies to the buffer s. If the source buffer contains more characters than n, only n-1 characters are written to s and a null is appended at the end. Note that unlike sprintf(), which stops copying characters to s only after it has reached a null character, snprintf() might copy only a portion of the source string to s if n isn't larger than the number of characters in the source buffer. In other words, to copy the source buffer to s completely, n must be larger than the source's size. The argument n ensures that snprintf() doesn't cause a buffer overflow by writing past the end of s.

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date