printf函數

printf

From: http://www.cplusplus.com/reference/cstdio/printf/

int printf ( const char * format, ... );
Print formatted data to stdout
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers(subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

Parameters

format
C string that contains the text to be written to stdout.
It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.

format specifier follows this prototype: [see compatibility note below] 
%[flags][width][.precision][length]specifier 

Where the specifier character at the end is the most significant component, since it defines the type and the interpretation of its corresponding argument:
specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.

% % followed by another % character will write a single % to the stream. %

The format specifier can also contain sub-specifiers: flagswidth.precision and modifiers (in that order), which are optional and follow these specifications:

flags description
- Left-justify within the given field width; Right justification is the default (see width sub-specifier).
+ Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
(space) If no sign is going to be written, a blank space is inserted before the value.
# Used with ox or X specifiers the value is preceeded with 00x or 0X respectively for values different than zero.
Used with aAeEfFg or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
0 Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).

width description
(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

.precision description
.number For integer specifiers (diouxX): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For aAeEf and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision0 is assumed.
.* The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

The length sub-specifier modifies the length of the data type. This is a chart showing the types used to interpret the corresponding arguments with and without length specifier (if a different type is used, the proper type promotion or conversion is performed, if allowed):

specifiers
length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char



signed char*
h short int unsigned short int



short int*
l long int unsigned long int
wint_t wchar_t*
long int*
ll long long int unsigned long long int



long long int*
j intmax_t uintmax_t



intmax_t*
z size_t size_t



size_t*
t ptrdiff_t ptrdiff_t



ptrdiff_t*
L

long double



Note that the c specifier takes an int (or wint_t) as argument, but performs the proper conversion to a charvalue (or a wchar_t) before formatting it for output.

Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99. See <cinttypes> for the specifiers for extended types.
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.

Return Value

On success, the total number of characters written is returned.

If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.

Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
/* printf example */ #include <stdio.h> int main() { printf ("Characters: %c %c \n", 'a', 65); printf ("Decimals: %d %ld\n", 1977, 650000L); printf ("Preceding with blanks: %10d \n", 1977); printf ("Preceding with zeros: %010d \n", 1977); printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100); printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416); printf ("Width trick: %*d \n", 5, 10); printf ("%s \n", "A string"); return 0; }


Output:
 Characters: a A Decimals: 1977 650000 Preceding with blanks: 1977 Preceding with zeros: 0000001977 Some different radices: 100 64 144 0x64 0144 floats: 3.14 +3e+000 3.141600E+000 Width trick: 10 A string 



printf函數html


轉載自:百度百科git


目錄app

功能ide

用法函數

1.      typeui

2.      flagsthis

3.      widthurl

4.      precspa

5.      F|N|h|lprototype

函數返回值

程序舉例

1.      例一

2.      例二

3.      例三

4.      例四

命令

1.      用途

2.      語法

3.      格式

4.      描述

5.      語法詳細介紹

6.      示例

7.      文件

 

功能

c語言中產生格式化輸出的函數(定義在 stdio.h 中)。向終端輸出(顯示器、控制檯等)

用法

int printf(const char *format,[argument]);

format 參數輸出的格式,定義格式爲:%[flags][width][.prec][F|N|h|l]type

規定數據輸出方式,具體以下:[1]

type

含義以下:

字符

輸入數據類型

含義

d、i

int

有符號10進制數,i是老式寫法

o

unsigned int

無符號8進制數

u

unsigned int

無符號10進制數

x、X

unsigned int

無符號16進制數,x用abcdef,X用ABCDEF表示10~15的數

f

double

小數

e、E

double

科學計數法表示的數,大小寫表明用的「e」的大小寫

g、G

double

使用以上兩種中最短的形式,大小寫的使用同%e和%E

c

char

把輸入的數字轉換爲對應的字符

s、S

char *、wchar_t *

字符串

p

void *

以16進制形式輸出指針

n

int *

到此字符以前爲止,一共輸出的字符個數,不輸出文本

%

不輸入

輸出字符「%」自己

注:%g、%G在小數點位數四位或指數大於等於精度時用%e、%E,不然用%f。

flags

規定輸出格式,取值和含義以下:

字符

名稱

說明

 

空白

右對齊,左邊填充0和空格

-

減號

左對齊,右邊填充空格

+

加號

在數字前增長符號 + 或 -

0

數字零

將輸出的前面補上0,直到佔滿指定列寬爲止(不能夠搭配使用「-」)

 

空格

輸出值爲正時加上空格,爲負時加上負號

#

井號

type是o、x、X時,增長前綴0、0x、0X

type是e、E、f、g、G時,必定使用小數點

type是g、G時,尾部的0保留

width

用於控制顯示數值的寬度,取值和含義以下

n(n=1,2,3...) 寬度至少爲n位,不夠以空格填充

0n(n=1,2,3...) 寬度至少爲n位,不夠左邊以0填充

* 格式列表中,下一個參數仍是width

prec

用於控制小數點後面的位數,取值和含義以下:

無 按缺省精度顯示

0 當type=d,i,o,u,x時,沒有影響

type=e,E,f時,不顯示小數點

n(n=1,2,3...) 當type=e,E,f時表示的最大小數位數

type=其餘,表示顯示的最大寬度

.* 格式列表中,下一個參數仍是width

F|N|h|l

表示指針是不是遠指針或整數是不是長整數

F遠指針

n 近指針

h 短整數或單精度浮點數

l 長整數或雙精度浮點數

函數返回值

printf函數的通常形式爲:

int printf(const char *format,[argument]);

以上形式,咱們在Visual C++裏輸入「printf(」將會看到。

說明printf函數類型爲整型,其返回值是整型值。

其值實際爲printf控制輸出的字符數。

printf()函數其實是將全部參數按字符輸出,根據該函數的參數1(const char *format),咱們不難理解。

例如:

int a,b;

a=printf("gelin\n"); //a的值爲6,

b=printf("the value of printf is:%d",a); //b的值爲24

printf("\n%d\n",b);

以上程序將會輸出:

  

程序舉例

例一

#include <stdio.h>

#defineC"gelin"

int main(void)

{

int a=12345;

float b=5.12345678;

char e,d,f;

scanf("%c%c%c",&e,&d,&f);

//分別演示:%d、%4d、%.4d、%d%*d%d

printf("int is:%d\n",a);

//分別演示:%d、%9d、%.9d、%8.4d、%-8.4d、%+8.4d

printf("float is:%f\n",b);

//分別演示:%f、%8f、%.4f、%8.4f、%-8.4f

printf("char is:%s\n",C);

//分別演示:%s、%8s、%.4s、%8.4s、%-8.4s

return 0;

}

例二

printf也能夠這樣用:

printf("123\n""456\n""789\n");

輸出:

123

456

789

注意:

printf("123\n" "456\n" "789\n");的輸出結果與printf("123\n");printf("456\n");printf("789\n");相同。

所以輸出多行時,也並不須要每行調用一次printf。

例三

妙用printf判斷閏年程序

#include<stdio.h>

int main(void)

{

int a;

scanf("%d",&a);

printf("%s",a%(a%100?4:400)?"NO":"YES");

return 0;

}

例四

#include <stdio.h>

#include<string.h>

int main()

{

char ch[20];

int m,n;

strcpy(ch,"Happy!");

scanf("%d%d",&m,&n);

printf("%*.*s\n",m,n,ch);

return 0;

}

其中前邊*定義的是總的寬度,後邊*是指定輸出字符個數。分別對應外邊參數m和n。

輸入: 10 3

輸出: Hap

命令

用途

經過標準輸出設備輸出一組數據。

語法

printf Format [ Argument ... ]

格式

printf(格式控制,輸出表列),格式控制由要輸出的文字和數據格式說明組成。要輸出的的文字除了能夠使用字母、數字、空格和一些數字符號意外,還能夠使用一些轉義字符表示特殊的含義。

例:printf("Hello!");

printf("%f,%f,the max is %f\n",a,b,c);

描述

printf 命令轉換、格式化並寫 Argument 參數到標準輸出。Argument 參數是由 Format 參數控制格式化的。格式化輸出行不能超出 LINE_MAX 字節長度。

下列環境變量影響 printf 命令的執行:

LANG 在 LC_ALL 和相應的環境變量(以 LC_ 開頭)沒有指定語言環境時,肯定語言環境編目使用的語言環境。

LC_ALL 肯定用於覆蓋由 LANG 或其它任何 LC_環境變量設置的任何語言環境編目值的語言環境。

LC_CTYPE 肯定把文本字節數據順序解釋爲字符的語言環境;例如,單一字節對應多字節字符的參數。

LC_MESSAGES 肯定寫消息使用的語言。

LC_NUMERIC 肯定數字格式編排的語言環境。此環境變量影響使用 e、E、f、g 和 G 轉換字符編寫的數字的格式。

Format 參數是包含三種對象類型的一個字符串:

* 無格式字符複製到輸出流。

* 轉換規範,每一個規範致使在值參數列表中檢索 0 個或更多個項。

* 如下轉義序列。在複製到輸出流時,這些序列致使它們的相關操做在有此功能的設備上顯示:

\\反斜槓

\a 警告

\b 退格

\f 換頁

\n 換行

\r 回車

\t 跳格

\v 垂直跳格

\ddd ddd 是 一、2 或 3 位八進制數字。這些轉義序列做爲由八進制數指定的具備數字值的字節顯示。

Argument 參數是一個或多個字符串的列表,它在Format 參數的控制下被寫到標準輸出。

Format 參數在必要的狀況下會常常從新使用以知足Argument 參數。將好像提供了空字符串Argument同樣評估任何額外的 c 或者 s 轉換規範;其它額外轉換規範將好像提供了 0Argument 同樣評估。此處 Format 參數不包含轉換規範僅出現 Argument 參數,結果是不肯定的。

語法詳細介紹

每一個 Format 參數中的轉換規範都具備以下順序的語法:

1. % (百分號)。

2. 零或更多的選項,修改轉換規範的含義。選項字符和它們的含義是:

- 轉換結果在字段中左對齊。

+ 符號轉換結果常以符號(+ 或者 -)開始。

空格 若是符號轉換的第一個字符不是符號,結果的前綴將是空格。若是空格和 + 選項字符都顯示,則忽略空格選項字符。

# 此選項指定值轉換到備用格式。對於 c、d、i, u 和 s 轉換,選項沒有做用。對於 o 轉換,它增長精度來強制結果的第一數字是 a、0(零)。對於 x 和 X 轉換,非零結果分別具備 0x 或 0X 前綴。對於 e、E、 f、g 和 G 轉換,結果一般包含基數字符,即便基數字符後沒有數字。對於 g 和 G 轉換,結尾零不象一般同樣除去。

0 對於 d、i、o、 u、x、e、 E、f、g 和 G 轉換,前導零(跟在符號或底數的後面)用於填充字段寬度,將不用空格填充。若是顯示 0(零)和 -(減號)選項,0(零)選項被忽略。對於 d、i、o、u、x 和 X 轉換,若是指定精度,0(零)選項將被忽略。

注:

其它轉換,沒有定義其行爲。

3. 可選的指定最小值字段寬度的十進制數字字符串。若是轉換值字符少於字段寬度,該字段將從左到右按指定的字段寬度填充。若是指定了左邊調整選項,字段將在右邊填充。若是轉換結果寬於字段寬度,將擴展該字段以包含轉換後的結果。不會發生截斷。然而,小的精度可能致使在右邊發生截斷。

4. 可選的精度。精度是一個 .(點)後跟十進制數字字符串。若是沒有給出精度,按 0(零)對待。精度指定:

* d、o、i、 u、x 或 X 轉換的最少數字顯示位數。

* e 和 f 轉換的基數字符後的最少數字顯示位數。

* g 轉換的最大有效數字位數。

* s 轉換中字符串的最大打印字節數目。

5. 指示要應用的轉換類型的一個字符,例如:

% 不進行轉換。打印一個 %(百分號)。

d, i 接受整數值並將它轉換爲有符號的十進制符號表示法。

o 接受整數值並將它轉換爲有符號的八進制符號表示法。精度指定顯示的最小數字位數。若是值轉換後能夠用更少的位數來表示,將使用前導零擴展。缺省精度是 1。精度爲零的零值轉換的結果是空字符串。用零做爲前導字符來指定字段寬度,致使用前導零填充字段寬度值。不用八進制值表示字段寬度。

u 接受整數值並將它轉換爲無符號的十進制符號表示法。

x, X 接受整數值並將它轉換爲十六進制符號表示法。字母abcdef 用於 x 轉換,字母 ABCDEF 用於 X 轉換

f 接受浮點或者雙精度值並將它轉換爲十進制符號表示法,格式爲[-] ddd.ddd。基數字符(在這裏顯示爲十進制點)後的數字位數等於規定的精度。 LC_NUMERIC 語言環境編目肯定在這個格式中使用的基數字符。若是不指定精度,則輸出六個數字。若是精度是 0(零),將不顯示基數字符

e, E 接受浮點或者雙精度值並將它轉換爲指數表示的形式[-] d.dde{+|-}dd。在基數字符前有一個數字(在這裏顯示爲十進制點),基數字符後的數字位數等於規定的精度。 LC_NUMERIC 語言環境編目肯定在這個格式中使用的基數字符。若是不指定精度,則輸出六個數字。若是精度是 0(零),將不顯示基數字符。E 轉換字符在指數前生成帶 E 而不是帶 e 的數字。指數一般至少包含兩個數字。然而,若是要打印的指數值大於兩個數字,必要時須要打印附加指數數字。

g、G 接受浮點和雙精度值並轉換爲 f 或 e 轉換字符的樣式(或在 G 轉換的狀況下是 E),用精度指定有效數字的個數。尾零將從結果中除去。基數字符只有在其後是數字時顯示。使用的樣式取決於轉換的值。樣式 g 僅在轉換的指數結果小於 -4,或大於或等於精度時使用。

c 接受值將其做爲字符串並打印字符串中的第一個字符。

s 接受值將其做爲字符串並打印字符串中的字符直到字符串結束或者達到精度指示的字符個數。若是沒有指定精度,打印所有字符直到出現第一個空字符

b 接受值將其做爲字符串,可能包含反斜槓轉義序列。打印來自轉換字符串的字節直到字符串結束或者達到精度規範指示的字節數。若是沒有指定精度,打印所有字節直到出現第一個空字符。

支持下列反斜槓轉義序列:

* 先前列出的反斜槓轉義序列在 Format 參數描述下。這些轉義序列將被轉換到它們表示的單個字符

* \c(反斜槓c)序列,它不顯示並使 printf 命令忽略 Format 參數中的字符串參數包含的剩餘的全部字符串,全部剩餘的字符串參數和全部附加字符。

退出狀態

該命令返回如下出口值:

0 成功完成。

>0 發生錯誤。

示例

1. 輸入下列命令:

printf ("%5d%4d\n",1213,43);

產生下列輸出:

_1213_ _43

三次使用 Format 參數打印全部給定字符串。0(零)由 printf 命令提供以知足最後的 %4d 轉換規格。

2. 輸入下列命令

printf ("%c %c\n",78,79);

產生下列輸出:

N_O

文件

/usr/bin/printf 包含printf 命令。

相關文章
相關標籤/搜索