|
Writes formatted data to a string. This function works almost exactly like standard C one, but it does not support all format tags and specifiers. If you've never heard about printf routines, here's the general idea: you specify a format line with some text that may contain format tags. For each format tag found, sprintf() takes a parameter for the stack. This method does not allow a compile time check, but it does give you enormous flexibility and effectiveness. So, you code it this way: sprintf( buffer_ptr, "My number is %-10d",123); Here '-10d' is a format tag that says, "there is a decimal value to be printed with left justification in the 10 characters field."
The following format specifiers are allowed:
%[-][0][<size>][{l|L}]{i|d|x|X|s|c|%}
Their meaning is standard:
- leading '-' sets left justification (the default is right)
- if leading zero is specified, leading zeroes will be printed for numbers.
- optional <size> (decimal number) sets the size of the output field
- optional 'l' or 'L' prefix means that number is long
- 'i' and 'd' causes a decimal integer to be printed
- 'x' and 'X' causes a hexadecimal number (with small or capital letters, respectively) to be printed
- 's' refers to a character string (might be UNICODE or bytes)
- 'c' means single character
- '%' means "just put out a percent character without taking arguments."
All other characters in the format line will be just printed out as is. Remember, it is up to you to handle the arguments list and the format line in the correct correspondence. -
Parameters:
-
ptr_buffer
|
a storage location for output. |
sz_format
|
format-control string. @argument optional arguments. |
-
Returns:
-
the number of characters written, not including the terminating null character, or a negative value if an output error occurs.
#include <cybiko.h>
#define FIRST_PLAYER_WINS 0
#define SECOND_PLAYER_WINS 1
...
const char* sz_format = "%s player WINS!";
const char* sz_player_name[2] = {
"First",
"Second"
};
int game_result;
char sz_result_string[24];
...
sprintf( sz_result_string, sz_format, sz_player_name[ game_result ] );
... |