首页 > 技术文章 > [c/c++] programming之路(10)、格式符后续

little-monkey 2017-08-02 14:47 原文

一、格式符

1.  f格式符

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

2.  e格式符

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void main(){
 5     printf("%e",10000000000);//printf不会进行数据转换
 6     printf("\n%e",10000000000.0);
 7     printf("\n%e",.0000000001);
 8     printf("\n%.7e",.0000000001);//小数点后面保留7位
 9     printf("\n%30.7e",.0000000001);//位宽30
10     printf("\n%030.7e",.0000000001);//填充0
11     printf("\n%-30.7e",.0000000001);//左对齐
12     printf("\n%-030.7e",.0000000001);//左对齐加0不起作用
13     system("pause");
14 }

3.  g格式符

二、printf说明

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

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

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void main0(){
 5     puts("锄禾日当午,编程真是苦");//自动换行
 6     printf("锄禾日当午,编程真是苦");//不会自动换行
 7     system("pause");
 8 } 
 9 void main(){
10     while (1)
11     {
12         char str[50];
13         gets(str);//初始化str
14         system(str);
15     }
16     system("pause");
17 } 

三、scanf

 命令行颜色和标题

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

 scanf详细说明

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

 

推荐阅读