[Previous]
[Contents]
[Next]

sprintf()

print formatted output into a string

Synopsis:

#include <stdio.h>

int sprintf( char *buf, const char *format, ... );

Description:

The sprintf() function is equivalent to the fprintf() function, except that the argument buf specifies a character array into which the generated output is placed, rather than to a file. A null character is placed at the end of the generated character string. The format string is described under the description of the printf() function.

Returns:

The number of characters written into the array, not counting the terminating null character. An error can occur while converting a value for output. If an error occurs, errno indicates the type of error detected.

Examples:

#include <stdio.h>

/* Create temporary file names using a counter */

char namebuf[13];
int  TempCount = 0;

char *make_temp_name()
  {
    sprintf( namebuf, "ZZ%.6o.TMP", TempCount++ );
    return( namebuf );
  }

void main()
  {
    FILE *tf1, *tf2;

    tf1 = fopen( make_temp_name(), "w" );
    tf2 = fopen( make_temp_name(), "w" );
    fputs( "temp file 1", tf1 );
    fputs( "temp file 2", tf2 );
    fclose( tf1 );
    fclose( tf2 );
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

_bprintf(), cprintf(), errno, fprintf(), printf(), _vbprintf(), vcprintf(), vfprintf(), vprintf(), vsprintf()


[Previous]
[Contents]
[Next]