sprintf_s, swprintf_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 sprintf_s(char * restrict s, rsize_t n, const char * restrict format, ...);

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

Description

Replaces sprintf adding security enhancements.

sprintf_s is equivalent to sprintf, except for the run-time constraint that restricts s and format from being null pointers. Other constraints require that every format specifier in format that is %s have respective non-null arguments and the %n specifier not be used in format.

If a run-time constraint violation occurs, s is not a null pointer, and n is greater than zero and less than RSIZE_MAX, then the function puts '\0' to s[0].

Unlike snprintf_s, snwprintf_s, in case n is bigger than the array that is pointed to by s, it raises a run-time constraint exception.

Return Value

On success, sprintf_s returns the number of characters output, not including the terminal '\0'. In case of an encoding error, sprintf_s returns a negative value. Any other error case returns zero.

Example

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

Portability

POSIX Win32 ANSI C ANSI C++

sprintf_s

+

+

+

+

swprintf_s

+

+

+

See Also