Your Browser is not longer supported
Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...
{{viewport.spaceProperty.prod}}
snprintf - Formatted output to a string
&pagelevel(4)&pagelevel
Definition | #include <stdio.h> int snprintf(char *s, size_t n, const char *format, argumentlist); snprintf edits data (characters, strings, numerical values) according to specifications in the string format and writes this data to the area pointed to by s.
snprintf only outputs up to the buffer limit specified by the n parameter. This prevents buffer overrun. Apart from that the functionality of snprintf is the same as that of sprintf .
|
Parameters | char *s Pointer to the result string. snprintf terminates the string with the null byte (\0). The maximum length of the output is therefore n-1. size_t n Length of the area reserved for the result string. n may not be greater than INT_MAX . When n = 0, no output takes place. const char *format Format string as described under printf with KR or ANSI functionality (cf. printf ). The only difference is with regard to the way in which the control characters for white space (\n, \t, etc.) are handled. As opposed to printf , sprintf enters the EBCDIC value of the control character in the result string. It is only during output to text files that the control characters are converted to their appropriate effect depending on the type of text file (see section “White space” (Basic terms)). argumentlist Variables or constants whose values are to be converted and formatted for output according to the information in the format statements. If the number of format statements does not match the number of arguments, the following applies: If there are more arguments, the surplus arguments are ignored. If there are fewer arguments, the results are undefined. |
Returnwert | < 0 | n > INT_MAX or output error. |
| = 0 .. n-1 | It was possible to edit the output completely. The return value specifies the length of the output without the terminating NULL character. |
| > n | It was not possible to edit the output completely. The return value specifies the length of the output without the terminating NULL character which a complete output would require. |
Notes | You must ensure that the area to which s points is large enough for the result! snprintf rounds to the specified precision when converting floating-point numbers.
snprintf does not convert one data type to another. A value must be explicitly converted (e.g. with the cast operator) if it is not to be output in conformity with its type.
Maximum number of characters to be output: With KR functionality (applies to C/C++ versions prior to V3.0 only) a maximum of 1400 characters can be output per sprintf call, with ANSI functionality a maximum of 1400 characters per conversion element (e.g. %s). The behavior is undefined if memory areas overlap. Attempts to output non-initialized variables or to output variables in a manner inconsistent with their data type can lead to undefined results. The behavior is undefined if the percent sign (%) in a format statement is followed by an undefined formatting or conversion character. |
See also | printf, fprintf, sprintf, putc, putchar, puts, sscanf |