[Previous] [Contents] [Index] [Next]

vsnprintf()

Write formatted output to a character array, up to a given maximum number of characters

Synopsis:

#include <stdarg.h>
#include <stdio.h>

int vsnprintf( char* buf,
               size_t count,
               const char* format,
               va_list arg );

Library:

libc

Description:

The vsnprintf() function formats data under control of the format control string and stores the result in buf. The maximum number of characters to store, including a terminating null character, is specified by count. For details on the format string, refer to the printf() function.

The vsnprintf() function is equivalent to snprintf(), with a variable argument list.

Returns:

The number of characters written into the array, not counting the terminating null character, or a negative value if count or more characters are requested to be generated. An error can occur while converting a value for output.

If an error occurs, errno contains a value that indicates the type of error detected.

Examples:

The following shows the use of vsnprintf() in a general error message routine:

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

char msgbuf[80];

char *fmtmsg( char *format, ... )
{
    va_list arglist;

    va_start( arglist, format );
    strcpy( msgbuf, "Error: " );
    vsnprintf( &msgbuf[7], 80-7, format, arglist );
    va_end( arglist );
    return( msgbuf );
}

int main( void )
{
    char *msg;

    msg = fmtmsg( "%s %d %s", "Failed", 100, "times" );
    printf( "%s\n", msg );

    return 0;
}

Classification:

Standard Unix

Safety:
Cancellation point No
Interrupt handler Read the Caveats
Signal handler Read the Caveats
Thread Yes

Caveats:

It's safe to call vsnprintf() in an interrupt handler or signal handler if the data isn't floating point.

See also:

errno, fprintf(), printf(), snprintf(), sprintf(), va_arg(), va_end(), va_start(), vfprintf(), vprintf(), vsprintf()


[Previous] [Contents] [Index] [Next]