#define _CRT_SECURE_NO_WARNINGS 1
// printf( ):将参数文本输出到屏幕。头文件是:<stdio.h>
// print(打印) f(format(格式化)),表示可以定制输出文本的格式.
// print format - 按照指定的格式打印数据
// 《 占位符:》
// 占位符的第一个字符为‘%’,第二个字符表示占位符的类型
// 如:%a 、%A 、%c 、%d 、%s 、%e 、%E …
#include <stdio.h>
int main()
{
printf("There are %d apples\n", 3);/* 占位符 %d 会被 , 后面的值替换掉 */
printf("%s will come tonight\n", "张三");/* , 后面替换的内容必须是占位符所对应的类型 '%s' */
// 输出文本里可以有多个占位符 如:
printf("%s says it is %d o'clock\n", "李四", 21);/* 占位符的格式必须一一对应,并且顺序也需要一一对应 */
// printf( )允许限定占位符的最小宽度 如:
printf("%d\n", 123); // 123
printf("%5d\n", 123); // __123 【不足5位,以空格填充】
printf("%5d\n", 1234567); // 1234567 【超过5位,自动突破打印】
printf("%5d%d\n", 123, 4567); // __1234567 【默认采用右对齐】
printf("%-5d%d\n", 123,4567); // 123__4567 【“-5d”左对齐5位,不足的以空格填充】
printf("%f\n", 123.45); // 123.450000 【小数(浮点数) 默认显示小数点后6位精度】
printf("%12f\n", 123.45); // __123.450000 【不足12位,会在 小数(浮点数) 最前面添加空格,小数点算一位】
printf("%.2f\n", 123.45); // 123.45 【如果不想要小数点后面默认添加的'0',[ % .(精度)f ] 如:%.2f】
printf("%.3f\n", 123.45); // 123.450 【%. 3(精度) f】
printf("%6.2f\n", 0.5); // __0.50 【% 6(总长度) .2(小数点后面精度) f(以浮点数进行输出),不足6位,最前面以空格填充】
printf("%4.2f\n", 12345.647); // 12345.65 【超过限定总长,自动突破。但小数点后面,只保留限定长度,多余的四舍五入】
printf("%*.*f\n", 6, 2, 0.5); // __0.50 【最小宽度和小数点后 这两个限定值,可以用*代替,通过printf( )的参数传入】
printf("%+d\n", 12); // +12 【如果想要正数也输出 '+'号, %+d 即可。(负号不用)】
printf("%+d\n", -12); // -12
printf("%.5s\n", "hello world");// hello 【输出限定字符串长度:%.(限定)s】
return 0;
}
//输出结果:There are 3 apples
// 张三 will come tonight