printf_s, wprintf_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 printf_s(const char * restrict format, [, argument, ...]);

int wprintf_s(const wchar_t * restrict format, [, argument, ...]);

Description

Replaces printf, wprintf adding security enhancements.

printf_s is equivalent to printf, wprintf, adding run-time constraints that restrict format and argument from beings null pointers.

Note: For Win32 GUI applications, stdout must be redirected.

Return Value

On success, printf_s returns the number of bytes output, otherwise it returns a negative value.

Example

#include <stdio.h>
#include <string.h>
#define I 555
#define R 5.5
int main(void)
{
  int i,j,k,l;
  char buf[7];
  char *prefix = buf;
  char tp[20];
  printf_s("prefix  6d      6o      8x        10.2e        "
  "10.2f\n");
  strcpy(prefix,"%");
  for (i = 0; i < 2; i++)
  {
   for (j = 0; j < 2; j++)
     for (k = 0; k < 2; k++)
       for (l = 0; l < 2; l++)
       {
       if (i==0)  strcat(prefix,"-");
         if (j==0)  strcat(prefix,"+");
            if (k==0)  strcat(prefix,"#");
                if (l==0)  strcat(prefix,"0");
                    printf_s("%5s |",prefix);
                    strcpy(tp,prefix);
                    strcat(tp,"6d |");
                    printf_s(tp,I);
                    strcpy(tp,"");
                    strcpy(tp,prefix);
                    strcat(tp,"6o |");
                    printf_s(tp,I);
                    strcpy(tp,"");
                    strcpy(tp,prefix);
                    strcat(tp,"8x |");
                    printf_s(tp,I);
                    strcpy(tp,"");
                    strcpy(tp,prefix);
                    strcat(tp,"10.2e |");
                    printf_s(tp,R);
                    strcpy(tp,prefix);
                    strcat(tp,"10.2f |");
                    printf_s(tp,R);
                    printf_s("  \n");
                    strcpy(prefix,"%");
     }
  }
return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

printf_s

+

+

+

+

wprintf_s

+

See Also