1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
%a 浮点数、十六进制数字和p-记数法(C99) %A 浮点数、十六进制数字和P-记数法(C99) %c 一个字符 %d 有符号十进制整数 %e 浮点数、e-记数法 %E 浮点数、E-记数法 %f 浮点数、十进制记数法 %g 根据数值不同自动选择%f或%e。%e格式在指数小于-4或者大于等于精度时使用 %G 根据数值不同自动选择%f或%E。%E格式在指数小于-4或者大于等于精度时使用 %i 有符号十进制整数(与%d相同) %o 无符号八进制整数 %p 指针 %s 字符串 %u 无符号十进制整数 %x 使用十六进制数字 0f的无符号十六进制整数 %X 使用十六进制数字 0F的无符号十六进制整数 %% 打印一个百分号 前缀0表示8进制 0x或0X表示16进制 后缀l或L表示long ll或LL表示long long #include <stdio.h> int main(int argc, char const *argv[]) { const int num = 222; printf("*%d*\n", num); //输出*222* printf("*%2d*\n", num); //输出*222* 原样输出(2<3) printf("*%10d*\n", num); //输出* 222* 前面有七个空格(7+3=10) printf("*%-10d*\n", num); //输出*222 * 后面有七个空格(有负号) //输出1f 1F 0x1f 16进制 #表示0x开头 printf("%x %X %#x\n", 31, 31, 31); //输出**32** 32**-32** 负号正好占用那个空格 printf("**%d**% d**% d**\n", 32, 32, -32); //输出** 6** 006**00006** 006** .3表示前置0撑满长度3 优先级高于前置0填充 printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6); getchar(); return 0; } #include <stdio.h> int main(int argc, char const *argv[]) { const double num = 3852.99; printf("*%f*\n", num); //输出*3852.990000* 默认保留6位小数 printf("*%e*\n", num); //输出*3.852990e+03* 科学记数法3.852990*10的3次方 printf("*%4.2f*\n", num); //输出*3852.99* 长度为4,保留两位小数 printf("*%3.1f*\n", num); //输出*3853.0* 长度为3,小于4,取4,保留一位小数,四舍五入 printf("*%10.3f*\n", num); //输出* 3852.990* 长度为10,前置3空格,保留3位小数 printf("*%10.3e*\n", num); //输出* 3.853e+03* 保留3位小数,3.853*10的3次方 printf("*%+4.2f*\n", num); //输出*+3852.99* 增加正号输出,长度4,保留两位小数 printf("*%010.2f*\n", num); //输出*0003852.99* 长度为10,前置3个0,保留两位小数 getchar(); return 0; } #include <stdio.h> #define STR "Authentic imitation! " int main(int argc, char const *argv[]) { printf("/%s/\n", STR); //输出/Authentic imitation! / 默认输出 printf("/%2s/\n", STR); //输出/Authentic imitation! / 长度2<STR长度,输出默认输出 printf("/%24s/\n", STR); //输出/ Authentic imitation! / 长度24>STR,前置空格填充输出 printf("/%24.5s/\n", STR); //输出/ Authe/ 长度24>STR,截取5位,前置空格输出 printf("/%-24.5s/\n", STR); //输出/Authe / 长度24>STR,截取5位,后置空格输出 getchar(); return 0; } #include <stdio.h> int main(int argc, char const *argv[]) { unsigned int un = 3000000000; short end = 200; long big = 65537; long long verybig = 12345678908642; char test = 'A'; char bing = '\a'; //输出 un = 3000000000 and not -1294967296 3000000000超过了int的范围,导致结果溢出 printf("un = %u and not %d\n", un, un); //输出 end = 200 and 200 200在short和int的范围内所有%hd %d都可以 printf("end = %hd and %d\n", end, end); //输出 big = 65537 and not 1 65537超出了short的范围 printf("big = %ld and not %hd\n", big, big); //输出 verybig = 12345678908642 and not 1942899938 12345678908642超出了long的范围 printf("verybig = %lld and not %ld\n", verybig, verybig); //输出test = 65 and A A的int值是65 printf("test = %d and %c\n", test, test); //没有任何输出 \a是响铃符(\007) 如果设备支持会发出一声"嘀" printf("%c",bing); getchar(); return 0; } #include <stdio.h> int main(int argc, char const *argv[]) { // * 在printf中的使用 unsigned width,precision; int num = 256; double weight = 242.5; printf("please key width\n"); scanf("%d", &width); //*会匹配width的值为num的长度 printf("the num is %*d:\n", width, num); printf("please key width and precision\n"); scanf("%d %d", &width, &precision); //*会依次匹配width、precision为weight的长度和精度 printf("weight= %*.*f\n", width, precision, weight); getchar(); return 0; } #include <stdio.h> int main(int argc, char const *argv[]) { // * 在scanf中的使用 int num; printf("please key three num\n"); // 2个*可以过滤掉用户输入的前2个数字,只讲第3个数字赋值给num scanf("%*d %*d %d", &num); printf("the last num is %d\n", num); getchar(); return 0; } |