C语言学习记录4

#include<stdio.h>
int main()
{
	const double rent=3852.99;//const 常量
	printf("*%f*\n",rent); 
	printf("*%e*\n",rent); 
	printf("*%4.2f*\n",rent); 
	printf("*%3.1f*\n",rent); 
	printf("*%10.3E*\n",rent); 
	printf("*%10.3f*\n",rent); 
	printf("*%+4.2f*\n",rent); 
	printf("*%010.2f*\n",rent); 
	return 0;
} 


按照要求的格式打印数字,%f默认打印小数点后6位,%e默认小数点后六位的科学计数法,%4.2f数字宽度为4,保留小数点后2位,%3.1f数字宽度为3,保留小数点后一位,可四舍五入,当数字宽度大于要求宽度时,按原来数字宽度打印,%010.2f,第一个0代表宽度不够前面补0,后面10代表打印宽度为10,%+4.2f,+表示对于正数前面添+号,对于负数前面添-号;


#include<stdio.h>
#define BLURB "Authentic imitation"
int main()
{
printf("[%2s]\n",BLURB);
printf("[%24s]\n",BLURB);
printf("[%24.5s]\n",BLURB);
printf("[%-24.5s]\n",BLURB);
return 0;	
}

注意,虽然第一个转换说明是%2s,但是字段被扩大为可容纳字符串中所有的字符,还需要注意的是,精度限制了待打印字符的个数。.5告诉printf只打印5个字符。另外,-标记使得文本左对齐输出。

如何打印较长的字符串:

#include<stdio.h>
int main()
{
	
	printf("there is a way to print a");
	printf("long string.\n");
	printf("there is another way to print a\
	long string.\n");
    printf("there is a new way to print a "
		   "long string.\n");
		   return  0;
} 

字符串对齐:

#include<stdio.h>
#include<string.h>
int main()
{
	char f_name[10];
	char l_name[10];
	int a ;
	int b;
	printf("please input your name:");
	scanf("%s %s",f_name,l_name);
	a=strlen(f_name);
	b=strlen(l_name);
	printf("%s %s\n",f_name,l_name);
	printf("%*d %*d\n",a,a,b,b);
	printf("%s %s\n",f_name,l_name);
	printf("%-*d %-*d\n",a,a,b,b);
	return  0;
} 

猜你喜欢

转载自blog.csdn.net/sinat_38151275/article/details/79723027
今日推荐