What are the differences between printf, fprintf, sprintf, snprintf, printf_s and fprintf_s?

5

Searching I noticed that there are several variations of printf , but I did not understand the differences and which to use in certain circumstances.

I would like to know the purpose of each with their differences and how they can be used.

    
asked by anonymous 04.09.2016 / 05:56

3 answers

6

All are set to <stdio.h> :

int printf( const char *format, ... );                                                 <C99
int printf( const char *restrict format, ... );                                         C99+

printf writes in output stdout .

int fprintf( FILE *stream, const char *format, ... );                                  <C99
int fprintf( FILE *restrict stream, const char *restrict format, ... );                 C99+

fprintf writes to an output stream defined by stream .

int sprintf( char *buffer, const char *format, ... );                                  <C99
int sprintf( char *restrict buffer, const char *restrict format, ... );                 C99+

sprintf writes to a character buffer defined by buffer . The behavior is undefined if string and its terminator is larger than the array .

int snprintf( char *restrict buffer, int bufsz, const char *restrict format, ... );     C99+

snprintf writes in string buffer , up to maximum bufsz - 1 , ending with null, unless bufsz is zero. If it is zero, nothing is written, but the number of bytes that would be written is calculated and returned anyway. In this specific case, buffer can be null pointer

int printf_s( const char *restrict format, ...);                                        C11+
int fprintf_s( FILE *restrict stream, const char *restrict format, ...);                C11+
int sprintf_s( char *restrict buffer, rsize_t bufsz, const char *restrict format, ...); C11+
int snprintf_s( char *restrict buffer, rsize_t bufsz, const char *restrict format, ...);C11+

These functions suffixed by _s are equivalent to the first four, but the following errors are detected in these versions, and they call the handler of constraint installed:

>
  • Specifier %n is present in the format
  • any argument corresponding to %s is null pointer
  • Format or buffer is null pointer
  • bufsz is zero or greater than RSIZE_MAX
  • character conversion errors
  • (only for sprintf_s ) the string with terminator is greater than bufsz


Note: availability of fprintf_s , sprintf_s , and snprintf_s is only guaranteed if the implementation set __STDC_LIB_EXT1__ and user set __STDC_WANT_LIB_EXT1__ as 1 before adding% to%.


Reference:

  

link

    
04.09.2016 / 06:22
3

% format_text (%) writes formatted text to the standard output stream ( printf ).

formatted

stdout ( string print formatted ) writes text formatted to a string.

fprintf ( safe sprintf ) the same as sprintf , but is not susceptible to buffer overflow.

By logic, snprintf and sprintf would be the safe versions of printf_s and fprintf_s , respectively, that is, versions where the memory locations are checked, so that there is no buffer overflow or access to another memory location. However, I admit, I've never used / never seen these functions.

You can find good examples in the documentation. Example: printf . In the search, change the function name and you will see definition, parameters, return type, examples and more.

I hope you have helped!

    
04.09.2016 / 06:15
1

The operation of all is similar, so I see no need for examples. I assume you know how to use printf() . All use the convention of a "mask" string followed by a vararg, that is, a variable number of arguments that match the% in the mask. The functions you mentioned differ basically in what happens to the result string.

printf : prints the result in stdout (standard output, which in the case of command line execution is the terminal).

fprintf : sends the result to the opened file passed as the first parameter (type FILE).

sprintf : sends the result to a buffer, passed as the first parameter. This function is considered unsafe because the result may be larger than this buffer, especially if the vararg parameters are provided by an external agent, opening the port for a stack overflow.

snprintf : secure version of sprintf . The second parameter passed is the size of the buffer passed in the first parameter. If the result is larger than the declared size of the buffer, it will be filled to the limit, but without stack overflow .

asprintf : secure version of sprintf that is only offered on GNU systems (eg Linux). The result is returned as a dynamically allocated string, in the required size. It is better than snprintf because it is not necessary to calculate the largest possible size of the result.

printf_s , sprintf_s , etc: idem versions without suffix _s , however some additional checks are made in order to avoid the typical bugs when using the printf family:

1) close the mask %n , source of a whole class of bugs and attacks

2) check if any of the parameters corresponding to %s is NULL

3) Check whether format or buffer is NULL

4) checks to see if the buffer size is valid

5) The sprintf_s function requires the buffer size as the second parameter, and the result is only buffered if it is different, than snprintf ( _s ) that stores the part of the result that fits. / p>     

04.09.2016 / 06:14