snprintf_s, snwprintf_s

From RAD Studio
Jump to: navigation, search

Go Up to stdio.h Index


Header File

stdio.h

Category

Formatted input/output functions

Prototype

int snprintf_s(char * restrict s, rsize_t n, const char * restrict format, ...);

int snwprintf_s(wchar_t * restrict s, rsize_t n, const wchar_t * restrict format, ...);

Description

Sends formatted output to the s buffer of a maximum length specified by the n parameter.

snprintf_s is equivalent to snprintf, adding run-time constraints that restrict format from being null, n being less than zero and less than RSIZE_MAX.

If an error occurs and the run-time constraints are valid, then snprintf_s sets s[0] to null.

snprintf_s, unlike sprintf_s, swprintf_s, will truncate the result to the length of the array that s is pointing to.

Return Value

Number of characters output if s would have been sufficiently large, not including the terminating '\0' character. If a run-time constraint violation occurs, it returns a negative value.

Note: The '\0' character is written only if the return value is greater than or equal to zero and less than n.

Example

#include <stdio.h>
#include <math.h>
int main(void)
{
   char buffer[80];
   snprintf_s(buffer, 40, "An approximation of Pi is %f\n", M_PI);
   puts(buffer);
   return 0;
}

See Also