[c/c++] programming之路(10)、格式符后续

时间:2020-12-09 15:43:31

一、格式符

1.  f格式符

 #include<stdio.h>
#include<stdlib.h> void main(){
printf("%f",10.23456789);//默认保留6位
printf("\n%.10f",10.234567);//小数点后保留10位
printf("\n%20.10f",10.234567);//位宽为20
printf("\n%-20.10f",10.234567);//-左边对齐
printf("\n%020.10f",10.234567);//位宽为20,前方填充0
printf("\n%1.10f",10.234567);//位宽为1,小于实际宽度则按照实际宽度 printf("\n\n%f",);//printf不会进行数据转换
printf("\n%f",10000000000.0); system("pause");
}

[c/c++] programming之路(10)、格式符后续

2.  e格式符

 #include<stdio.h>
#include<stdlib.h> void main(){
printf("%e",);//printf不会进行数据转换
printf("\n%e",10000000000.0);
printf("\n%e",.);
printf("\n%.7e",.);//小数点后面保留7位
printf("\n%30.7e",.);//位宽30
printf("\n%030.7e",.);//填充0
printf("\n%-30.7e",.);//左对齐
printf("\n%-030.7e",.);//左对齐加0不起作用
system("pause");
}

[c/c++] programming之路(10)、格式符后续

3.  g格式符

[c/c++] programming之路(10)、格式符后续

二、printf说明

 #include<stdio.h>
#include<stdlib.h> void main(){
//当“格式控制”中格式符个数少于输出表中的输出项时,多余的输出项不予输出。
//当“格式符”多于输出项时,结果为不定值。
printf("%d,%d,%d",,,);
printf("\n%d,%d,%d,%d,%d,%d",,,);
printf("\n%d\n",,,);
system("pause");
}

[c/c++] programming之路(10)、格式符后续

 #include<stdio.h>
#include<stdlib.h> void main(){
//printf("%%");//%%输出%,%不会输出
char str[];
sprintf(str,"for /l %%i in (1,1,5) do calc");
system(str);
system("pause");
}
 #include<stdio.h>
#include<stdlib.h>
//XEGC可以大写,G(影响输出字母E,e的大小写)
//其余都得小写
void main(){
printf("%D",);//%对应空,D输出D,%d对应整数
printf("\n%O",);//%对应空,O输出O,%o对应八进制
printf("\n%x",);//%x,%X不影响16进制
printf("\n%u",);
printf("\n%U",);
printf("\n%e",1000000000.0);//%e,%E都能正常输出
printf("\n%C",'A');//%C,%cE都能正常输出
printf("\n%S","ABC");//%S 不能正常输出,输出为空
printf("\n%G",1000000000000000.0);//指数g,G影响E,e的输出
printf("\n%g",100.123456); system("pause");
}

[c/c++] programming之路(10)、格式符后续

 #include<stdio.h>
#include<stdlib.h> void main0(){
puts("锄禾日当午,编程真是苦");//自动换行
printf("锄禾日当午,编程真是苦");//不会自动换行
system("pause");
}
void main(){
while ()
{
char str[];
gets(str);//初始化str
system(str);
}
system("pause");
}

三、scanf

命令行颜色和标题

[c/c++] programming之路(10)、格式符后续

 #include<stdio.h>
#include<stdlib.h> void main(){
char str[];
system("color 1E");
system("title 中国网络监管中心");
printf("你的电脑已处于监控,速度去自首,Y/N\n"); scanf("%s",str);
system(str); system("pause");
}

[c/c++] programming之路(10)、格式符后续

scanf详细说明

 #include<stdio.h>
#include<stdlib.h> void main(){
/*float f1;
scanf("%f",&f1);
printf("%f",f1);*/ double f1=1.0;
//scanf("%f",&f1);//%f扫描对于double类型无效
scanf("%lf",&f1);//double类型需要%lf
printf("%f",f1); system("pause");
}
 #include<stdio.h>
#include<stdlib.h> void main(){
int num1,num2,num3;
scanf("%3d%3d%d",&num1,&num2,&num3);//3意味着截取三位宽度
printf("%d,%d,%d",num1,num2,num3); system("pause");
}

[c/c++] programming之路(10)、格式符后续