snprintf

Writes output to a specified character array under format control.

Library:LibC
Classification:ANSI
Service:File and Directory I/O

Syntax

  #include <stdio.h> 
   
  int snprintf (
     char         *str,
     size_t        n,
     const char   *format,
     ... );
  

Parameters

str

(OUT) Points to the character array into which to place the output.

n

(IN) Specifies the number of characters to generate for the str parameter. Output beyond n-1 is discarded, and the array in str is null terminated.

format

(IN) Points to the format control string. See Print Format Control Strings.

...

(IN) Points to an argument for a conversion specifier. The number of arguments is determined by the format string.

Return Values

If successful, returns the number of characters written into the array, not counting the null-terminating character. Otherwise, returns a negative value and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

The file descriptor is not a valid file descriptor open for writing.

5

ENOMEM

Insufficient storage space is available.

9

EINVAL

There are insufficient arguments.

12

ENOSPC

No free space remains on the device containing the file.

24

EAGAIN

The O_NONBLOCK flag is set for the file descriptor, and the process cannot immediately perform the write operation.

25

ENXIO

A request was made of a nonexistent device, or the request was outside the capabilities of the device.

28

EIO

A physical I/O error has occurred.

32

EPIPE

An attempt was made to write to a pipe or FIFO that is not open for reading by any process.

63

EINTR

A signal interrupted the write operation.

71

EFBIG

An attempt was made to write to a file that exceeds the maximum file size.

81

EOVERFLOW

The value of the n parameter is greater than INT_MAX or the number of bytes needed to hold the output (excluding the null-terminating character) is greater than INT_MAX.

101

EILSEQ

A wide-character code that does not correspond to a valid character has been deleted.

Remarks

The snprintf function is equivalent to sprintf, except that the n parameter specifies the number of characters to generated for output. A null-terminating character is placed at the end of the generated character string.

See Also