Sprintf function Usage Details, sprintf function details
Function
Write formatted data to a character buffer.
Header files
Stdio. h
Prototype
Int sprintf (char * buffer, const char * format, [argument]… );
Parameter List
Buffer: a char pointer pointing to the buffer of the string to be written.
Format: format the string.
[Argument]...: optional parameter, which can be any type of data.
Return Value
Returns the number of characters written to the buffer. If an error occurs,-1 is returned. if the buffer or format is a null pointer and continues without errors, the function returns-1 and errno is set to EINVAL.
Sprintf returns the number of bytes written to the buffer when argument is in format, the ending character '\ 0' is not included (this function will automatically add' \ 0' to the end '). That is, if "Hello" is written into a buffer with enough space, the sprintf function returns 5. At the same time, the buffer content will be changed.
Example
J = sprintf (buffer, "String: % s \ n", s );
J + = sprintf (buffer + j, "Character: % c \ n", c );
J + = sprintf (buffer + j, "Integer: % d \ n", I );
J + = sprintf (buffer + j, "Real: % f \ n", fp );
J + = sprintf (buffer + j, "Integer: % d % 02d", I, j); // % 02d indicates that if there are not two numbers, 0 is automatically filled, if the number is greater than two, it will not be affected.