1.基本用法
C语言学习时会大量用到printf()函数,这里先介绍一下这个函数。
printf()的作用是将参数文本输出到屏幕。它名字里面的f代表format(格式化),表示可以定制输出文本的格式。
|
1 |
printf("Hello World"); |
上面命令会在屏幕上输出一行文字“Hello World”。
printf()不会在行尾自动添加换行符,运行结束后,光标就停留在输出结束的地方,不会自动换行。为了让光标移到下一行的开头,可以在输出文本的结尾,添加一个换行符\n。
|
1 |
printf("Hello World\n"); |
如果文本内部有换行,也是通过插入换行符来实现。
|
1 |
printf("Hello\nWorld\n"); |

上面示例先输出一个Hello,然后换行,在下一行开头输出World,然后又是一个换行。
上面示例也可以写成两个printf(),效果完全一样。
|
1 2 |
printf("Hello\n"); printf("World\n"); |

printf()是在标准库的头文件stdio.h定义的。使用这个函数之前,必须在源码文件头部引入这个头文件。
上面示例中,只有在源码头部加上#include <stdio.h>,才能使用printf()这个函数。
2.占位符
printf()可以在输出文本中指定占位符。所谓“占位符”,就是这个位置可以用其他值代入。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("I am %i years old\n", 16); return 0; } |
上面示例中,I am %i years old\n是输出文本,里面的%i就是占位符,表示这个位置要用其他值来替换。占位符的第一个字符一律为百分号%,第二个字符表示占位符的类型,%i表示这里代入的值必须是一个整数(integer)。
printf()的第二个参数就是替换占位符的值,上面的例子是整数16替换%i。执行后的输出结果就是I am 16 years old。

常用的占位符除了%i,还有%s表示代入的是字符串。

上面示例中,%s表示代入的是一个字符串,所以printf()的第二个参数就必须是字符串,这个例子是Sixteen。执行后的输出就是I am sixteen years old。
输出文本里面可以使用多个占位符。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("I am %i(%s) years old\n", 16, "sixteen"); return 0; } |

上面示例中,输出文本 I am %i(%s) years old 有两个占位符 , 第一个是字符串占位符%i,第二个是整数占位符%s,分别对应printf()的第二个参数(16)和第三个参数(sixteen)。执行后的输出就是 I am 16(sixteen) years old。
printf()参数与占位符是一一对应关系,如果有n个占位符,printf()的参数就应该有n + 1个。如果参数个数少于对应的占位符,printf()可能会输出内存中的任意值。
printf()的占位符有许多种类,与 C 语言的数据类型相对应。下面按照字母顺序,列出常用的占位符,方便查找,具体含义在后面章节介绍。
%a:浮点数。%A:浮点数。%c:字符。(char)%d:十进制整数。(decimal)%e:使用科学计数法的浮点数,指数部分的e为小写。%E:使用科学计数法的浮点数,指数部分的E为大写。%i:整数,基本等同于%d。(integer)%f:小数(包含float类型和double类型)。(float)%g:6个有效数字的浮点数。整数部分一旦超过6位,就会自动转为科学计数法,指数部分的e为小写。%G:等同于%g,唯一的区别是指数部分的E为大写。%hd:十进制 short int 类型。%ho:八进制 short int 类型。%hx:十六进制 short int 类型。%hu:unsigned short int 类型。%ld:十进制 long int 类型。%lo:八进制 long int 类型。%lx:十六进制 long int 类型。%lu:unsigned long int 类型。%lld:十进制 long long int 类型。%llo:八进制 long long int 类型。%llx:十六进制 long long int 类型。%llu:unsigned long long int 类型。%Le:科学计数法表示的 long double 类型浮点数。%Lf:long double 类型浮点数。%n:已输出的字符串数量。该占位符本身不输出,只将值存储在指定变量之中。%o:八进制整数。%p:指针。%s:字符串。%u:无符号整数(unsigned int)。%x:十六进制整数。%zd:size_t类型。%%:输出一个百分号。
3.输出格式
printf()可以定制占位符的输出格式。
(1)限定宽度
printf()允许限定占位符的最小宽度。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("%8d\n", 4567); // 输出为 " 4567" return 0; } |

上面示例中,%8d表示这个占位符的宽度至少为8位。如果不满8位,对应的值的前面会添加空格。
输出的值默认是右对齐,即输出内容前面会有空格;如果希望改成左对齐,在输出内容后面添加空格,可以在占位符的%的后面插入一个-号。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("%-8dhere\n", 4567); // 输出为 " 4567" return 0; } |

上面示例中,输出内容4567的后面添加了四个空格。加“Here”的目的是看清空格的数目。
对于小数,这个限定符会限制所有数字的最小显示宽度。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("here|%12f\n", 567.89); return 0; } |

上面示例中,%12f表示输出的浮点数最少要占据12位。由于小数的默认显示精度是小数点后6位,所以567.890000输出结果的头部会添加2个空格。
(2)总是显示正负号
默认情况下,printf()不对正数显示+号,只对负数显示-号。如果想让正数也输出+号,可以在占位符的%后面加一个+。
|
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> #include <stdlib.h> int main() { printf("%+d\n", 89); // 输出 +89 printf("%+d\n", -89); // 输出 -89 return 0; } |

上面示例中,%+d可以确保输出的数值,总是带有正负号。
(3)限定小数位数
输出小数时,有时希望限定小数的位数。举例来说,希望小数点后面只保留两位,占位符可以写成%.3f。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("Number is %.3f\n", 0.5); return 0; } |

上面示例中,如果希望小数点后面输出5位(0.50000),占位符就要写成%.5f。
这种写法可以与限定宽度占位符,结合使用。
|
1 2 3 4 5 6 7 |
#include <stdio.h> #include <stdlib.h> int main() { printf("Here|%7.3f\n", 0.5); } |

上面示例中,%7.3f表示输出字符串最小宽度为7,小数位数为3。所以,输出字符串的头部有两个空格。
最小宽度和小数位数这两个限定值,都可以用*代替,通过printf()的参数传入。
|
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> #include <stdlib.h> int main() { printf("%*.*f\n", 7, 3, 0.5); // 等同于 printf("%7.3f\n", 0.5); } |

上面示例中,%*.*f的两个星号通过printf()的两个参数7和3传入。
(4)输出部分字符串
%s占位符用来输出字符串,默认是全部输出。如果只想输出开头的部分,可以用%.[m]s指定输出的长度,其中[m]代表一个数字,表示所要输出的长度。
|
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { // 输出 My Dea printf("%.6s\n", "My Dear Friend!"); } |

印度地图
|
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 |
// C program to print map of India #include <stdio.h> int main() { int a = 10, b = 0, c = 10; // The encoded string after removing first 31 characters // Its individual characters determine how many spaces // or exclamation marks to draw consecutively. char* str = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq " "TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL" "OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm " "SOn TNn ULo0ULo#ULo-WHq!WFs XDt!"; while (a != 0) { // read each character of encoded string a = str[b++]; while (a-- > 64) { if (++c == 90) // 'Z' is 90 in ascii { // reset c to 10 when the end of line is reached c = 10; // '\n' is 10 in ascii // print newline putchar('\n'); // or putchar(c); } else { // draw the appropriate character // depending on whether b is even or odd if (b % 2 == 0) putchar('!'); else putchar(' '); } } } return 0; } |
输出结果
