snprintf

Syntax

int snprintf (char * str, int n, const char * format, ... )

Arguments

str
Pointer to a string buffer to store the string
n
Maximum number of bytes to be written to the string buffer
format
Specifies the format of the resulting string, including types of the variables to be included in the string.

Return

The number of bytes printed if successful, otherwise a negative number if unsuccessful.

Description

snprintf "prints" a formatted string (up to a defined length), except instead of printing it to a terminal console the result is stored in a string variable.

Also see

sprintf

Comments

Wherever possible, you should use snprintf instead of sprintf. It avoids buffer overruns (crashes) that can occur when using sprintf.