赞
踩
目录
格式控制字符 | 输出数据类型 | 标 志 | 意义 |
---|---|---|---|
%p | 指针 | - | 结果左对齐,右边填空格 |
%c | char | + | 输出符号(正号或负号) |
%s | string | 空格 | 输出值为正时冠以空格,为负时冠以负号 |
%f | float | # | 对c、s、d、u类无影响; 对o类,在输出时加前缀o; 对x类,在输出时加前缀0x; |
%lf | |||
%a(%A) | 浮点数、十六进制数字和p-(P-)记数法(C99) | ||
%e(%E) | 浮点数指数输出[e-(E-)记数法] | ||
%g(%G) | 浮点数不显无意义的零"0" | ||
%d/%i | int | 格式字符串 | |
%ld | long | [标志][输出最少宽度][.精度][长度]类型 | |
%lld | long long | 用十进制整数来表示输出的最少位数。若实际位数多于定义的宽度,则按实际位数输出,若实际位数少于定义的宽度则补以空格或0。 | |
%u | 无符号十进制整数 | 精度格式符以“.”开头,后跟十进制整数。本项的意义是:如果输出数字,则表示小数的位数;如果输出的是字符,则表示输出字符的个数;若实际位数大于所定义的精度数,则截去超过的部分。 | |
%o | 八进制整数 | ||
%x(%X) | 十六进制整数0f(0F) | ||
%% | % |
- #include <iostream>
- #include <string.h>
-
- int main()
- {
- char c;
- int i1 = -78;
- int i2 = 78;
- unsigned int u = 78;
- long l = 1234567890;
- long long ll = 1234567890123456789;
- float f1 = 3.123456;
- float f2 = 3.100000;
- double x = 123.12345678912345;
- std::string s = "Hello,world";
- c = '\x41';
-
- printf("i1 = %d\n", i1); //%d,十进制int,i1 = -78
-
- printf("i2 = %d\n", i2); //%d,十进制int,i2 = 78
-
- printf("i1 = %i\n", i1); //%d,十进制int,i1 = -78
-
- printf("i2 = %d\n", i2); //%d,十进制int,i2 = 78
-
- printf("u = %u\n", u); //%u,无符号十进制int,u = 78
-
- printf("i2 = %d%%\n", i2); //%d,十进制int,%%输出%号,i2 = 78%
-
- printf("i1 = %o\n", i1); //%o,八进制int,不输出o,i1 = 37777777662
-
- printf("i2 = %o\n", i2); //%o,八进制int,不输出o,i2 = 116
-
- printf("i2 = %#o\n", i2); //%o,八进制int,#输出o,i2 = o116
-
- printf("i1 = %x\n", i1); //%x,十六进制int,不输出0x,i1 = ffffffb2
-
- printf("i2 = %x\n", i2); //%x,十六进制int,不输出0x,i2 = 4e
-
- printf("i2 = %X\n", i2); //%X,十六进制int,不输出0X,i2 = 4E
-
- printf("i2 = %#x\n", i2); //%x,十六进制int,#输出0x,i2 = 0x4e
-
- printf("i2 = %08d\n", i2); //%d,十进制int,8表示输出最小宽度,超过按实际位数,不足的左边补0,i2 = 00000078
-
- printf("i2 = %+8d\n", i2); //%d,十进制int,+输出+号,8表示输出最小宽度,超过按实际位数,不足的左边补空格,i2 = +78
-
- printf("i2 = %-8d\n", i2); //%d,十进制int,-左对齐,8表示输出最小宽度,超过按实际位数,不足的右边补空格,i2 = 78
-
- printf("i1 = % d\n", i1); //%d,十进制int,空格表示输出值为正时加空格,为负时加-号,i1 = -78
-
- printf("i2 = % d\n", i2); //%d,十进制int,空格表示输出值为正时加空格,为负时加-号,i2 = 78
-
- printf("l = %ld\n", l); //%ld,十进制long,l = 1234567890
-
- printf("ll = %lld\n", ll); //%lld,十进制long long,ll = 1234567890123456789
-
- printf("f1 = %f\n", f1); //%f,float,有效数字是7位,f1 = 3.123456
-
- printf("f1 = %6.10f\n", f1); //%f,float,有效数字是7位,最少输出6位,小数点后有8位,超出范围,f1 = 3.1234560013
-
- printf("f1 = %a\n", f1); //%a,十六进制和p-记数法,float,有效数字是7位,f1 = 0x1.8fcd680000000p+1
-
- printf("f1 = %A\n", f1); //%A,十六进制和P-记数法,float,有效数字是7位,f1 = 0x1.8Fcd680000000P+1
-
- printf("f1 = %e\n", f1); //%e,指数e-记数法,float,有效数字是7位,f1 = 3.123456e+00
-
- printf("f2 = %E\n", f2); //%E,指数E-记数法,float,有效数字是7位,f2 = 3.100000E+00
-
- printf("f2 = %g\n", f2); //%g,不显示无意义的0,float,有效数字是7位,f2 = 3.1
-
- printf("x = %lf\n", x); //%x,double,x = 123.123457
-
- printf("x = %10.14lf\n", x); //%x,double,x = 123.12345678912345
-
- printf("c = %c\n", c); //%c,char,c = A
-
- printf("c = %x\n", c); //%x,以十六进制输出字符ascll码,c = 41
-
- printf("s = %s\n", s.c_str()); //%s,输出字符串,s = Hello,world
-
- printf("s = %6.9s\n", s.c_str()); //%s,输出字符串,最多输出9个字符,s = Hello,wor
-
- printf("s = %10.9s\n", s.c_str()); //%s,输出字符串,最多输出9个字符,不足左边补空格,s = Hello,wor
-
- system("pause");
- return 0;
- }
【C++】printf格式化输出:https://blog.csdn.net/u013066730/article/details/84243819
C++---常用printf输出格式:https://blog.csdn.net/qq_40951833/article/details/103104812
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。