C 语言中 springf 函数的用法详解
2013-04-17 22:59
344 查看
来自:http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
转载请说明出处
根据一定的格式输出字符串到 str 指针参数中
format[/b] -- 这是一个字符串,这个字符串由各种格式和特定字符串构成。 这些格式由下表的字符后面跟一个%号字符构成。 这个字符串中可以有多个这样的格式。 后面的参数将根据顺序替换这些字符,并把替换后的字符放到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[/b] -- 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.
Let us compile and run the above program, this will produce the following result:
转载请说明出处
介绍
int sprintf(char *str, const char *format, ...)[/b]根据一定的格式输出字符串到 str 指针参数中
参数
str[/b] -- 这个参赛为一个指针字符数组。 将用于存储给格式化后的字符串format[/b] -- 这是一个字符串,这个字符串由各种格式和特定字符串构成。 这些格式由下表的字符后面跟一个%号字符构成。 这个字符串中可以有多个这样的格式。 后面的参数将根据顺序替换这些字符,并把替换后的字符放到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[/b] -- 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
相关文章推荐
- 举例详解Go语言中os库的常用函数用法
- 详解Python中where()函数的用法
- Python基础之函数用法实例详解
- C语言文件读写操作中缓冲区问题和setbuf函数详解
- Linux编程里getopt_long_only函数用法详解
- SetBkMode函数用法详解
- VC++, CString的各个函数用法详解与小示例
- 详解Matlab中 sort 函数用法
- eval()函数用法详解
- MySQL自定义函数用法详解-复合结构自定义变量/流程控制
- 转:Sql server中时间函数用法详解
- sscanf函数用法详解
- Linux编程里getopt_long_only函数用法详解
- 浅谈SQLServer的ISNULL函数与Mysql的IFNULL函数用法详解
- C++指向函数的指针用法详解
- jQuery.trim() 函数及trim()用法详解
- getpid()函数用法详解
- C++ vector用法(详解!!函数,实现)
- 深入php define()函数以及defined()函数的用法详解
- c++中vector的用法详解-函数实现