printf格式控制详解
format 参数输出的格式,定义格式为
%[flags][width][.precision][length]specifier
specifier在最后面。定义了数据类型。
Where the specifier character at the end is the most significant component, since it defines the type and the interpretation of its corresponding argument:
| specifier | Output | Example |
|---|---|---|
| d or i | Signed decimal integer | 392 |
| u | Unsigned decimal integer | 7235 |
| o | Unsigned octal | 610 |
| x | Unsigned hexadecimal integer | 7fa |
| X | Unsigned hexadecimal integer (uppercase) | 7FA |
| f | Decimal floating point, lowercase | 392.65 |
| F | Decimal floating point, uppercase | 392.65 |
| e | Scientific notation (mantissa/exponent), lowercase | 3.9265e+2 |
| E | Scientific notation (mantissa/exponent), uppercase | 3.9265E+2 |
| g | Use the shortest representation: %e or %f | 392.65 |
| G | Use the shortest representation: %E or %F | 392.65 |
| a | Hexadecimal floating point, lowercase | -0xc.90fep-2 |
| A | Hexadecimal floating point, uppercase | -0XC.90FEP-2 |
| c | Character | a |
| s | String of characters | sample |
| p | Pointer address | b8000000 |
| n | Nothing printed. The corresponding argument must be a pointer to a signed int. The number of characters written so far is stored in the pointed location. |
|
| % | A % followed by another % character will write a single % to the stream. | % |
问:double如何表示,用%lf。
The format specifier can also contain sub-specifiers: flags, width, .precision and modifiers (in that order), which are optional and follow these specifications:
| flags | description |
|---|---|
| - | Left-justify within the given field width; Right justification is the default (see width sub-specifier). |
| + | Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign. |
| (space) | If no sign is going to be written, a blank space is inserted before the value. |
| # | Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero. Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written. |
| 0 | Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier). |
-左对齐。右边填充空格。
在数字前增加符号 + 或 -
0数字零将输出的前面补上0,直到占满指定列宽为止(不可以搭配使用“-”)
int a=10;
printf("%8d\n",a);
printf("%-8d\n",a);
printf("%08d\n",a);
输出:
10
10 (左对齐,右边填满空格)
00000010
请按任意键继续. . .
| width | description |
|---|---|
| (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. |
| * | The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
| .precision | description |
|---|---|
| .number | For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6). For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. If the period is specified without an explicit value for precision, 0 is assumed. |
| .* | The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
对于浮点数而言,m.n宽度为m,精度为n,n表示小数点后还有几位。m指域宽,即对应的输出项在输出设备上所占的字符数(包括小数点)。N指精度。用于说明输出的实型数的小数位数。为指定n时,隐含的精度为n=6位。
%f:不指定宽度,整数部分全部输出并输出6位小数。
%m.nf :输出共占m列,其中有n位小数,如数值宽度小于m左端补空格。
%0m.nf :输出共占m列,其中有n位小数,如数值宽度小于m左端补0。
%-m.nf:输出共占n列,其中有n位小数,如数值宽度小于m右端补空格。
如
float f;
scanf("%f",&f);
printf("%f\n",f);
printf("%3.3f\n",f);
输出:
3.4321
3.432100
3.432 (小数点后还有3位)
请按任意键继续. . .
上面的%3.3f有点问题,m一般大于n(m包括小数点)。
float f;
scanf("%f",&f);
printf("%f\n",f);
printf("%6.3f\n",f);
输出:
1.1
1.100000
1.100(前面有一个空格,加上小数点,共占6为)。
请按任意键继续. . .
"%m.ns":输出m位,取字符串(左起)n位,左补空格,当n>m or m省略时m=n
e.g. "%7.2s" 输入CHINA
输出" CH"
"%m.nf":输出浮点数,m为宽度,n为小数点右边数位
e.g. "%3.1f" 输入3852.99
输出3853.0 长度:为h短整形量,l为长整形量
The length sub-specifier modifies the length of the data type. This is a chart showing the types used to interpret the corresponding arguments with and without length specifier (if a different type is used, the proper type promotion or conversion is performed, if allowed):
数据类型长度修饰符:
| specifiers | |||||||
|---|---|---|---|---|---|---|---|
| length | d i | u o x X | f F e E g G a A | c | s | p | n |
| (none) | int | unsigned int | double | int | char* | void* | int* |
| hh | signed char | unsigned char | signed char* | ||||
| h | short int | unsigned short int | short int* | ||||
| l | long int | unsigned long int | wint_t | wchar_t* | long int* | ||
| ll | long long int | unsigned long long int | long long int* | ||||
| j | intmax_t | uintmax_t | intmax_t* | ||||
| z | size_t | size_t | size_t* | ||||
| t | ptrdiff_t | ptrdiff_t | ptrdiff_t* | ||||
| L | long double | ||||||
Note that the c specifier takes an int (or wint_t) as argument, but performs the proper conversion to a charvalue (or a wchar_t) before formatting it for output.
signed char c;
scanf("%hhd",&c);
printf("%c",c);
输出48,会输出0字符。
但是运行会错误。stack was corrupted.
一直很奇怪的hhd问题,vc总是会报错。今天终于找到了答案:原因是VS2010还不支持
c99的hhd。
参考这个网页:
http://connect.microsoft.com/VisualStudio/feedback/details/416843/sscanf-cannot-not-handle-hhd-format
Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99. See <cinttypes> for the specifiers for extended types.
... (additional arguments)Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
返回值:
On success, the total number of characters written is returned. 如果成功,
If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.
If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.
/* printf example */
#include <stdio.h> int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", , 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n", , , , , );
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick: %*d \n", , );
printf ("%s \n", "A string");
return ;
}
Characters: a A
Decimals:
Preceding with blanks:
Preceding with zeros:
Some different radices: 0x64
floats: 3.14 +3e+ 3.141600E+000
Width trick:
A string
------------------------------------------------------------------------------
一篇文章:printf格式。
转换说明及作为结果的打印输出
%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的无符号十六进制整数
%% 打印一个百分号
#include<cstdio>
int main()
{
//for int
int i=;
long i2=309095024l;
short i3=;
unsigned i4=;
printf("%d,%o,%x,%X,%ld,%hd,%u ",i,i,i,i,i2,i3,i4);//如果是:%l,%h,则输不出结果
printf("%d,%ld ",i,i2);//试验不出%ld和%d之间的差别,因为long是4bytes
printf("%hd,%hd ",i,i3);//试验了%hd和%d之间的差别,因为short是2bytes //for string and char
char ch1="d";
unsigned char ch2=;
char *str="Hello everyone!";
printf("%c,%u,%s ",ch1,ch2,str);//unsigned char超过128的没有字符对应 //for float and double,unsigned and signed can not be used with double and float
float fl=2.566545445F;//or 2.566545445f
double dl=265.5651445;
long double dl2=2.5654441454; //%g没有e格式,默认6位包括小数点前面的数,
//%f没有e格式,默认6位仅只小数点后面包含6位
//%e采用e格式,默认6位为转化后的小数点后面的6位
printf("%f,%e,%g,%.7f ",fl,dl,dl,dl);
printf("%f,%E,%G,%f ",fl,dl,dl,dl);//%F is wrong
printf("%.8f,%.10e ",fl,dl);
printf("%.8e,%.10f ",fl,dl); //for point
int *iP=&i;
char *iP1=new char;
void *iP2;//dangerous!
printf("%p,%p,%p ",iP,iP1,iP2); //其他知识:负号,表示左对齐(默认是右对齐);%6.3,6表示宽度,3表示精度
char *s="Hello world!";
printf(":%s: :%10s: :%.10s: :%-10s: :%.15s: :%-15s: :%15.10s: :%-15.10s: ",
s,s,s,s,s,s,s,s);
double ddd=563.908556444;
printf(":%g: :%10g: :%.10g: :%-10g: :%.15g: :%-15g: :%15.10g: :%-15.10g: ",
ddd,ddd,ddd,ddd,ddd,ddd,ddd,ddd); //还有一个特殊的格式%*.* ,这两个星号的值分别由第二个和第三个参数的值指定
printf("%.*s ", , "abcdefgggggg");
printf("%*.*f ", ,, 1.25456f); return ;
}
printf格式控制详解的更多相关文章
- C 语言 printf格式控制详解
闲来无事,整理了一下C语言printf() 的格式控制语句. PS:详细来源于网络. printf的格式控制的完整格式: % - 0 m.n l或h 格式字符 下面对组成格式说明的各项加以说 ...
- [转帖]IP /TCP协议及握手过程和数据包格式中级详解
IP /TCP协议及握手过程和数据包格式中级详解 https://www.toutiao.com/a6665292902458982926/ 写的挺好的 其实 一直没闹明白 网络好 广播地址 还有 网 ...
- 利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解
本文转载自利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解 导语 由于最近工作需要利用 Jenkins 远程 API 操作 Jenkins 来完成一些列操作,就抽空研究 ...
- java中printf中用法详解
目前printf支持以下格式: %c 单个字符 %d 十进制整数 %f 十进制浮点数 %o 八进制数 %s 字符串 %u 无符号十进制数 %x 十六进制数 %% 输出百分号% printf的格式控制的 ...
- printf()格式化输出详解
% - 0 m.n l或h 格式字符 下面对组成格式说明的各项加以说明: ①%:表示格式说明的起始符号,不可缺少. ②-:有-表示左对齐输出,如省略表示右对齐输出. ③0:有0表示指定空位填0,如省略 ...
- printf中用法详解
%c 单个字符 %d 十进制整数 %f 十进制浮点数 %o 八进制数 %s 字符串 %u 无符号十进制数 %x 十六进制数 %% 输出百分号% printf的格式控制的完整格式: % - 0 m ...
- Odoo权限控制详解
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826105.html 一:Odoo中的权限设置主要有以下5种 1)菜单.报表的访问权限 Odoo可以设置菜 ...
- 基于C语言的Q格式使用详解
用过DSP的应该都知道Q格式吧: 目录 1 前言 2 Q数据的表示 2.1 范围和精度 2.2 推导 3 Q数据的运算 3.1 0x7FFF 3.2 0x8000 3.3 加法 3.4 减法 3.5 ...
- js键盘事件全面控制详解
js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件 ...
随机推荐
- MySQl 存储过程+游标
DROP PROCEDURE IF exists pro_Insertflightplan_stat; create procedure pro_Insertflightplan_stat(exec ...
- 下载文件夹里面的所有文件,并压缩成.zip压缩包的形式
http://www.aspsnippets.com/Articles/Download-multiple-files-as-Zip-Archive-File-in-ASPNet-using-C-an ...
- html 5 新增标签及简介
作为下一代Web技术的代表,HTML5概念在近些年尤其火热.据了解,HTML5受到垂青最直接的原因就是其跨平台性,除此之外,它不仅仅可以用于表示Web内容,还可能将Web带入一个广阔的生态平台. 下面 ...
- centos 5.8 x64Jetty的安装以及项目部署配置
链接地址:http://blog.csdn.net/shuixin536/article/details/9049821 安装环境 centos 5.8 x64 安装前须知 首先在安装Jetty之前要 ...
- PHP学习建议(来自老手)
框架太多了,有一个用着,先用熟练,因为框架思想区别不大. 用熟悉一个,再看其他,就容易多.看那么多,没有一个熟悉的,还是什么也不知道. 框架还是要用熟悉才行,然后才是产品如何设计,mysql性能真的有 ...
- HTML+CSS笔记 表格,超链接,图片,表单
表格 给表格加入CSS样式,添加表格边框 语法: <style type="text/css"> table tr td,th{border:1px solid #00 ...
- js获取select默认选中的Option (非当前选中值)
js函数方法: <script> function getDefaultSelectedOption(selectId, valIfNull) { var selectId = selec ...
- detain ssh server 设置(也许必须是root来安装?)
ssh connection refused 处理方法 一般这种情况是 opens server 没安装 或 没启动 检查 openssh 是否安装 su 登录root账号,安装 openssh se ...
- Windows Phone 8初学者开发—第12部分:改进视图模型和示例数据
原文 Windows Phone 8初学者开发—第12部分:改进视图模型和示例数据 第12部分:改进视图模型和示例数据 原文地址:http://channel9.msdn.com/Series/Win ...
- BZOJ 1684: [Usaco2005 Oct]Close Encounter
题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MB Description Lacking e ...