云海唯C的专栏

不见而观,形随心易。

原创 C语言格式转换说明符以及修饰符的一些程序,帮助巩固收藏

新一篇: setprecision()与setiosflags() | 旧一篇: 过完年了,准备继续

/*转换说明修饰符的几个小程序,其实这些修饰符无非就是+,-,#,0,*,整型数,浮点数*/

#include <stdio.h>
#define PAGES 931
int main(void)
{printf("*%d*\n",PAGES);
 printf("*%2d*\n",PAGES);
 printf("*%10d*\n",PAGES);   //默认是右对齐
 printf("*%-10d*\n",PAGES);
 getchar();
 return 0;
}
//整型的格式控制

///////////////////////////////////////////////////////
#include <stdio.h>
int main(void)
{const double RENT=3852.99;
 printf("*%f*\n",RENT);   //默认小数点右边和%e一样都是6位有效数字
 printf("*%e*\n",RENT);
 printf("*%4.2f*\n",RENT);
 printf("*%3.1f*\n",RENT);
 printf("*%10.3f*\n",RENT);
 printf("*%10.3e*\n",RENT);
 printf("*%+4.2f*\n",RENT);       //+就加正号,默认右对齐;-就左对齐
 printf("*%010.2f*\n",RENT);      //10个位置显示结果,不够填0
 getchar();
 return 0;
}

////////////////////////////////////////////////////////////////////////
#include <stdio.h>
int main(void)
{printf("%x %X %#x\n",31,31,31);         //注意小写x与大写X
 printf("**%d**% d**%d**\n",42,42,-42);  //在说明符中使用空格以在"正值"前产生一个空格
 printf("**%5d**%5.3d**%05d**%05.3d**\n",6,6,6,6);
 //在整数中使用精度说明%5.3,5代表5个位置,.3代表除了6以外还需要添加几个0
 getchar();
 return 0;
}

///////////////////////////////////////////////////////////////////////
#include <stdio.h>
#define BLURE "Authentic imitation!"
int main(void)
{printf("/%2s/\n",BLURE);
 printf("/%24s/\n",BLURE);
 printf("/%24.5s/\n",BLURE); //这里的精度说明.5表示只打印前5个字符
 printf("%-24.5s/\n",BLURE);
 getchar();
 return 0;
}

////////////////////////////////////////////////////////////////////////
//不匹配的转换
#include <stdio.h>
#define PAGES 336
#define WORDS 65618
int main(void)
{short num=PAGES;
 short mnum=-PAGES;
 printf("num as short and unsigned short:%hd %hu\n",num,num);
 printf("-num as short and unsigned short:%hd %hu\n",mnum,mnum);
 printf("num as int and char:%d %c\n",num,num);
 printf("WORDS as int,short,and char:%d %hd %c\n",WORDS,WORDS,WORDS);
 getchar();
 return 0;
}
/* hh和整型转换说明符一起使用,表示一个signed char或unsigned char类型数值%hhd %hhu
   h和整型转换说明符一起使用,表示一个short int或unsigned short int类型数值%hd %hu,
   l和整型转换说明符一起使用,表示一个long int或者unsigned long int   %ld %lu
   ll和整型转换说明符一起使用,表示一个long long int或unsigned long long int   %lld %llu


发表于 @ 2008年03月10日 17:05:00|评论(loading...)|编辑

新一篇: setprecision()与setiosflags() | 旧一篇: 过完年了,准备继续

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 云海唯C