W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
原型: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;
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: