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.
 
% % followed by another % character will write a single % to the stream. %

问:double如何表示,用%lf。

The format specifier can also contain sub-specifiers: flagswidth.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 ox or X specifiers the value is preceeded with 00x or 0X respectively for values different than zero.
Used with aAeEfFg 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.
n(n=1,2,3...) 宽度至少为n位,不够以空格填充。(前面以空格填充)
0n(n=1,2,3...) 宽度至少为n位,不够左边以0填充。(正如前面所讲,flag=0;输出前面补0)。
 
* 格式列表中,下一个参数还是width
.precision description
.number For integer specifiers (diouxX): 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 aAeEf 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 precision0 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格式。

printf()函数是格式输出函数,请求printf()打印变量的指令取决与变量的类型.例如,在打印整数是使用%d符号,在打印字符是用%c 符号.这些符号被称为转换说明.因为它们指定了如何不数据转换成可显示的形式.下列列出的是ANSI C标准peintf()提供的各种转换说明.
 
          转换说明及作为结果的打印输出
%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的无符号十六进制整数
%%    打印一个百分号
  使用printf ()函数
 printf()的基本形式: printf("格式控制字符串",变量列表);
 
#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格式控制详解的更多相关文章

  1. C 语言 printf格式控制详解

    闲来无事,整理了一下C语言printf() 的格式控制语句. PS:详细来源于网络. printf的格式控制的完整格式: %  -  0  m.n  l或h  格式字符 下面对组成格式说明的各项加以说 ...

  2. [转帖]IP /TCP协议及握手过程和数据包格式中级详解

    IP /TCP协议及握手过程和数据包格式中级详解 https://www.toutiao.com/a6665292902458982926/ 写的挺好的 其实 一直没闹明白 网络好 广播地址 还有 网 ...

  3. 利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解

    本文转载自利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解 导语 由于最近工作需要利用 Jenkins 远程 API 操作 Jenkins 来完成一些列操作,就抽空研究 ...

  4. java中printf中用法详解

    目前printf支持以下格式: %c 单个字符 %d 十进制整数 %f 十进制浮点数 %o 八进制数 %s 字符串 %u 无符号十进制数 %x 十六进制数 %% 输出百分号% printf的格式控制的 ...

  5. printf()格式化输出详解

    % - 0 m.n l或h 格式字符 下面对组成格式说明的各项加以说明: ①%:表示格式说明的起始符号,不可缺少. ②-:有-表示左对齐输出,如省略表示右对齐输出. ③0:有0表示指定空位填0,如省略 ...

  6. printf中用法详解

    %c 单个字符 %d 十进制整数 %f 十进制浮点数 %o 八进制数 %s 字符串 %u 无符号十进制数 %x 十六进制数 %% 输出百分号% printf的格式控制的完整格式: %  -  0  m ...

  7. Odoo权限控制详解

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826105.html 一:Odoo中的权限设置主要有以下5种 1)菜单.报表的访问权限 Odoo可以设置菜 ...

  8. 基于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 ...

  9. js键盘事件全面控制详解

      js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件 ...

随机推荐

  1. c#语言基础之组成结构

    一.项目结构 .cs---    源文件(程序代码) .csproj---项目文件(管理文件项) .sln---   解决方案文件(管理项目) .config---配置文件 函数的四要素:名称.输入. ...

  2. BZOJ 2006: [NOI2010]超级钢琴( RMQ + 堆 )

    取最大的K个, 用堆和RMQ来加速... ----------------------------------------------------------------- #include<c ...

  3. chown命令详解

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...

  4. C#中foreach语句的迭代器实现机制

    C#中的foreach语句可用于循环遍历某个集合中的元素,而所有的只要支持了IEnumerable或IEnumerable<T>泛型接口的类型都是可以 用foreach遍历的.其具体的遍历 ...

  5. chrome浏览器强制采用https加密链接

    在chrome地址栏输入chrome://net-internals/#hsts,然后把www.google.com添加到domain,并且Include subdomains全部沟上添加就可以了.

  6. C语言之固定格式输出当前时间

    固定格式输出当前时间 编程实现将当前时间用以下形式输出:星期  月  日   小时:分:秒   年 代码如下: #include<stdio.h>#include<stdlib.h& ...

  7. 自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版

    Nginx比apache要好,优点很多,随便去搜寻引擎找一下就能找到一大把资料,这不是我们讨论的重点,我们的重点是自己做一个运行组合!     為何我不從網上下載一個別人已經封裝好的現成的版本呢?因為 ...

  8. python类继承

    面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...

  9. JavaEE Tutorials (7) - 在会话bean中使用异步方法调用

    7.1异步方法调用88 7.1.1创建异步业务方法88 7.1.2从企业bean客户端调用异步方法897.2async示例应用90 7.2.1async—war模块的架构91 7.2.2运行async ...

  10. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...