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实现键盘记录,要关注浏览器的三种按键事件 ...
随机推荐
- BlogUI的使用
- UI界面
http://www.uimaker.com/uimakerhtml/uidesign/uisoft/2016/0323/122862.html http://www.uimaker.com/uima ...
- opencv第一站:配置opencv环境(2015-12-12)
今天论坛申请的书< OpenCV 计算机视觉编程攻略(中国工信出版社)>到了,准备研究研究机器视觉. 晚上安装了 vc2008 及 opencv 最新版 3.0.0,试了各种配置都是错误提 ...
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...
- BZOJ 1230: [Usaco2008 Nov]lites 开关灯( 线段树 )
线段树.. --------------------------------------------------------------------------------- #include< ...
- Apache+php+mysql+phpadmin搭建
一,准备工具. httpd-2.4.12-win32-VC9.zip php-5.4.40-Win32-VC9-x86.zip phpMyAdmin-4.2.13.2-all-languages my ...
- HTML5 画布参考
描述 HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript). 不过,<canvas> 元素本身并没有绘制能力(它仅仅是图形的容器) - 您必 ...
- ASP.NET MVC3 Razor视图引擎-基础语法
I:ASP.NET MVC3在Visual Studio 2010中的变化 在VS2010中新建一个MVC3项目可以看出与以往的MVC2发生了很明显的变化. 1.ASP.NET MVC3必要的运行环境 ...
- AnsiString 在 Delphi 中虽然不可用,但是,在 C++ 中可以用
[C++] C++ Builder 中 Ansi 编码的字符串在Android/iOS程序中显示的问题 呃,这个问题说起来,其实也不麻烦,C++ Builder 本身在 TEncoding 做了处理, ...
- vim Ctags 和taglist安装和使用
Ctags是一个用于从程序源代码树产生索引文件(或tag文件),从而便于文本编辑器来实现快速定位的实用工具.在产生的tag文件中,每一个tag的入口指向了一个编程语言的对象.这个对象可以是变量定义.函 ...