可以用printf中的%*c、%*d,来输出前导空格:运行以下几个小程序即可发现其中奥妙:(%*c,%*d在printf()函数中的 妙用) 明白后可以运行最后的程序:字母金字塔;以体会如何运用。 main(){ char a='A';int d=40; while(d>0) printf("%*c,%c\n",d--,a);} ////////main(){ char a='A';int d=40; while(d>0) printf("%*d,\n",d--,a);} /////////main(){ char a='A';int d=40; while(d>0) printf("%*c,\n",d--,a);} /////////main(){ char a='A';int d=40; while(d>0) printf("%*c\n",d--,a);} 字母金字塔程序代码: #include<stdio.h>#include<ctype.h>void main(void){ char c,c1,c2,top; printf("Input a character:\n"); top=isupper(c=getchar())?'A':(islower(c)?'a':'\0'); if(top) { for(c1=top;c1<=c;++c1) { printf("%*c",40-(c1-top<<1)); for(c2=top;c2<=c1;++c2) printf("%2c",c2); for(c2=c1-1;c2>=top;--c2) printf("%2c",c2); printf("\n"); } }} ////////main(){ char a='A';int d=40; while(d>0) printf("%*c,\n",d--);}

评论