首页 > 其他 > 详细

printf()用法总结

时间:2014-10-03 00:49:13      阅读:469      评论:0      收藏:0      [点我收藏+]

/*使用转换说明符*/
#include<stdio.h>
#include<string.h>
#define PI 3.141593 //创建符号常量的第一种方法
#define PAGES 93179962
#define BLURB "Authentic imtation !"
#define WORDS 65618
#define PAGES2 336
int main(void)
{
 printf("\n\n\t\t/*使用转换说明符*/\n\n");
 int number = 5;
 int pc = 2*6;
 float expresso = 13.5;
 int cost = 3100;
 printf("\n\n*****\tThe %d CEOs drank %f cups of expresso.\t*******\n\n",
 number,expresso);//打印变量
 printf("_____The value of pi is %f ._______\n\n",PI);//打印常量
 printf("\aFarewell ! thou art too dear for my possessing.\n\n" );//打印语句
 printf("%c%d\n\n\t",‘$‘,2*cost);//打印字符常量与惩罚表达式
 
 printf("Only %d%% of Sally‘s gribbles were edible.\n\n\n",pc);//打印%号 ,注意是两个%%
 
 /*字段宽度*/
    printf("\t\t/*字段宽度*/\n\n");
 printf("\t*%d*\n",PAGES); //整数宽度相同的字段
 printf("\t*%2d*\n",PAGES);/*它指示要生成宽度为2的字段,但由于该
                           整数有4位数字,所以自动扩展以适应数字长度*/
 
 printf("\t*%10d*\n",PAGES); /*这将产生一个有10空格那么宽的字段,
                             于是星号之间有7个空白和3个数字,
           并且数字位于整个字段的右端*/
 printf("\t*%+10d*\n",PAGES);/*带有加号*/
 printf("\t*%-10d*\n",PAGES);/*这将产生一个有10空格那么宽的字段,
                             于是星号之间有7个空白和3个数字,
           并且数字位于整个字段的左端*/
 
 /*一些浮点数的组合*/
 printf("\n\n\t\t/*一些浮点数的组合*/\n\n");
          
 const double RENT = 3852.99;//创建符号常量的第二方法
 
 printf("\t*%f*\n",RENT); /*注意%f默认格式*/
 printf("\t*%e*\n",RENT);/*注意%e默认格式*/
 printf("\t*%4.2f*\n",RENT);
 printf("\t*%3.1f*\n",RENT);//四舍五入
 printf("\t*%10.3f*\n",RENT);
 printf("\t*%10.3e*\n",RENT);//四色五入
 printf("\t*%+4.2f*\n",RENT);
 printf("\t*%010.2f*\n",RENT);/*0标志产生前导零,以使结果填充整个字段
                                 说明符010中第一个0是标志,
         剩余10使指定字段宽度*/
 /*一些格式标志的使用示例*/
 printf("\n\n\t\t/*一些格式标志的使用示例*/\n\n");
 
 printf("%x %X %#x\n",31,31,31);
 printf("**%d**% d**% d**\n",42,42,42);
 printf("**%5d**%5.3d**%05d**%05.3d**\n",6,6,6,6);
    //0标志和精度说明符标志同时出现,那么0标志就会被忽视
   
    /*字符串的格式化*/
 printf("\n\n\t\t/*一些格式标志的使用示例*/\n\n\n");
 printf("/%2s/\n",BLURB);
 printf("/%24s/\n",BLURB);
 printf("/%24.5s/\n",BLURB);
 printf("/%-24.5s/\n",BLURB);
 
  /*应用*/
  printf("\n\n/*应用*/\n\n");
 
  char name[40];
  float cash;
  printf("please input the name and cash\n");
  scanf("%s %f",name,&cash);
  printf("The %s family just may be %c%.2f richer !\n",name,‘$‘,cash);
 
  /*一些不匹配的整数转换*/
  printf("\n\n\t\t/*一些不匹配的整数转换*/\n\n");
 
  short num = PAGES2;
  short mnum = -PAGES2;
 
  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 short and char :%d %c\n",num,num);
  printf("WORDS as int ,short and char :%d %hd %c\n",
          WORDS,WORDS,WORDS);
  
 
  /*不匹配浮点转化*/
  printf("\n\n\t\t/*不匹配浮点转化*/\n\n");
 
  float n1 = 3.0;
  double n2 = 3.0;
  long n3 = 2000000000;
  long n4 = 1234567890;
 
  printf("%.1e %.1e %.1e %.1e\n ",n1,n2,n3,n4);
  printf("%ld %ld\n",n3,n4);
  printf("%ld %ld %ld %ld\n",n1,n2,n3,n4);
  //字节丢失
 
 
  /*--发现printf()的返回值*/
  printf("\n\n\t\t/*--发现printf()的返回值*/ \n\n");
  int bph2o = 212;
  int rv;
 
  rv = printf("%d F is water‘s boiling point.\n",bph2o);
  printf("The printf() function printed %d character.\n",
          rv);
         
   /*打印较长的字符串的 3 种方法*/
  
  printf("\n\n\t/*打印较长的字符串的 3 种方法*/\n\n");
  printf("Here‘s one way to print a");
  printf("a long string.\n");
  printf("Here‘s another way to print a \
long string\n");
  printf("Here‘s newest way to print a"
         "long string . \n");
 
 return 0;
}
 

printf()用法总结

原文:http://www.cnblogs.com/me-ziqiyu/p/4004630.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!