来自:http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
转载请说明出处
介绍
int sprintf(char *str, const char *format, ...)
根据一定的格式输出字符串到 str 指针参数中
参数
-
str -- 这个参赛为一个指针字符数组。 将用于存储给格式化后的字符串
-
format -- 这是一个字符串,这个字符串由各种格式和特定字符串构成。 这些格式由下表的字符后面跟一个%号字符构成。 这个字符串中可以有多个这样的格式。 后面的参数将根据顺序替换这些字符,并把替换后的字符放到str指针变量中。
- 转换说明及作为结果的打印输出%a 浮点数、十六进制数字和p-记数法(C99)
%A 浮点数、十六进制数字和p-记法(C99)
%c 一个字符
%d 有符号十进制整数
%e 浮点数、e-记数法
%E 浮点数、E-记数法
%f 浮点数、十进制记数法
%g 根据数值不同自动选择%f或%e.
%G 根据数值不同自动选择%f或%e.
%i 有符号十进制数(与%d相同)
%o 无符号八进制整数
%p 指针
%s 字符串
%u 无符号十进制整数
%x 使用十六进制数字0f的无符号十六进制整数
%X 使用十六进制数字0f的无符号十六进制整数
%% 打印一个百分号 -
The following example shows the usage of sprintf() function.additional arguments -- Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.
Return Value
If successful, the total number of characters written is returned excluding null-character appended at the end of the string, otherwise a negative number is returned in case of failure.
Example
#include <stdio.h> #include <math.h> int main() { char str[80]; sprintf(str, "Value of Pi = %f", M_PI); puts(str); return(0); }
Let us compile and run the above program, this will produce the following result:
Value of Pi = 3.141593
转载于:https://blog.51cto.com/hiandroidstudio/1180274