printf

2018-11-11 15:48 更新

 原型:extern void printf(const char *format,...);
 
 用法:#include <stdio.h>
 
 功能:格式化字符串輸出
 
 說明:format指定輸出格式,后面跟要輸出的變量
       目前printf支持以下格式:
         %c        單個字符
         %d        十進制整數(shù)
         %f        十進制浮點數(shù)
         %o        八進制數(shù)
         %s        字符串
         %u        無符號十進制數(shù)
         %x        十六進制數(shù)
         %%        輸出百分號%
       一個格式說明可以帶有幾個修飾符,用來指定顯示寬度,小數(shù)尾書及左對齊等:
         -         左對齊
         +         在一個帶符號數(shù)前加"+"或"-"號
         0         域?qū)捰们皩Я銇硖畛洌皇怯每瞻追?br/>        域?qū)捠且粋€整數(shù),設置了打印一個格式化字符串的最小域。精度使用小數(shù)點后加數(shù)字表示的,
       給出每個轉(zhuǎn)換說明符所要輸出的字符個數(shù)。  

注意:帶修飾符的顯示可能不正常

         
 舉例:


     // printf.c
     
     #include <stdio.h>
     #include <system.h>
     main()
     {
       int i;
       char *str="GGV";
       
       clrscr();
       
       textmode(0x00);
       printf("Printf Demo-%%c");
       printf("--------------");
       printf("%c-%c-%c-%c\n",'D','e','m','o');
       printf("%2c-%2c-%2c-%2c\n",'D','e','m','o');
       printf("%02c-%02c-%02c-%02c\n",'D','e','m','o');
       printf("%-2c-%-2c-%-2c-%-2c\n",'D','e','m','o');
       
       getchar();
       clrscr();
       textmode(0x00);            // not nessary
       i=7412;
       printf("Printf Demo-%%d");
       printf("--------------");        
       printf("%d\n",i);
       printf("%14d",i);
       printf("%+10d\n",i);       // output format not correct(bug)
       printf("%-10d\n",i);
       
       getchar();
       clrscr();
       printf("Printf - d,o,x");
       printf("--------------");        
       printf("%d\n",i);
       printf("%o\n",i);         // %o and %x not implemented
       printf("%x\n",i);
       
       getchar();
       clrscr();
       printf("Printf Demo-%%s");
       printf("--------------");
       printf("   %s\n","Demo End");
       printf("    %s\n","Thanx");
       printf("    %s\n  %s","Golden","Global View");
       
       getchar();
       return 0;
     }      


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號