Admin Production ni-theme
Current Publication

snprintf

LabWindows/CVI

snprintf

int snprintf (char targetString[], size_t bufferSize, const char formatString[], ...);

Purpose

Writes output to the specified string according to format specifiers in formatString. A null character is written at the end of the characters written.

If the resulting formatted string is more than bufferSize - 1 bytes long (not including the terminating null character), only the first bufferSize - 1 characters plus a null character are written to targetString. The function always returns the full length of the formatted string, which is greater than bufferSize only if truncation occurs.

Parameters

Input
Name Type Description
bufferSize size_t Pass the maximum number of bytes that may be written to the targetString buffer, including the null byte. This is typically just the size of the buffer.
formatString const char [] Contains the format string that specifies how subsequent arguments are converted for output. Use standard ANSI C format specifiers. If insufficient arguments exist for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated but are otherwise ignored.
Note Note  For more information about the standard ANSI C format specifiers, refer to an external ANSI C reference.
source_s ... This specifies one or more arguments that are to be converted and written to the target string. If there are multiple arguments, they must be separated by commas.
Output
Name Type Description
targetString char [] Contains the string to which the formatted data is written. A null character is written at the end of the characters written to this parameter.

If you want only to query the buffer size required for the formatted string without actually doing the formatting, you may pass NULL for this parameter and 0 for the bufferSize.

Return Value

Name Type Description
formattedStringSize int The number of bytes that would have been written had bufferSize been sufficiently large, or a negative value if an output error occurred. This value can be compared with bufferSize to determine if truncation occurred.
Note Note  The terminating null character is not included in the count.

Additional Information

Library: ANSI C Library

Include file: ansi_c.h

LabWindows/CVI compatibility: LabWindows/CVI 9.0 and later

Example

Refer to compiler\c99extensions\c99extensions.cws for an example of using the snprintf function.