printf的转换说明要注意的几点

默认右对齐

-   // 左对齐

+ // 显示符号,正数显示 + ,负数显示 -

空格 // 正数显示空格,负数显示 -

%%  // 打印 %

0   // 不足宽度的,在前面补 0

 1 #include <stdio.h>
 2 #define BLURB "Authentic imitation!"
 3 
 4 int main(void)
 5 {
 6     printf("[%2s]\n",BLURB);
 7     printf("[%24s]\n",BLURB);
 8     printf("[%24.5s]\n",BLURB);
 9     /*
10      * 精度限制了待打印字符的个数
11      * .5 告诉 printf()只打印5个字符
12      * 另外,- 标记使得文本左对齐
13      */
14     printf("[%-24.5s]\n",BLURB);
15 
16     return 0;
17 }

 Result:

[Authentic imitation!]
[    Authentic imitation!]
[                   Authe]
[Authe                   ]

转换说明:

  把二进制格式存储在计算机中值转换成一系列字符以便于显示。

例如:

数字76,在计算机内部存储格式是 0100 1100 

%d 转换说明将 0100 1100 转换成字符 7 和 6,并显示为 76

%x转换说明将  0100 1100 转换成十六进制记数法 4c

%c转换说明将  0100 1100 转换成字符 L

猜你喜欢

转载自www.cnblogs.com/luwudang/p/9690000.html