C語言學習【printf函數和scanf函數】
printf()
函數和scanf()
函數可以讓用戶與程序交流,是輸入/輸出
函數
printf()函數
請求printf()
函數打印數據的指令要與待打印數據的類型相匹配。例如,打印整數時使用%d
,打印字符時使用%c
。這些符號被稱為轉換說明(conversion specification),它們指定了如何把數據轉換成可顯示的形式
如下圖所屬我i欸轉換說明及其打印的輸出結果

/* 使用轉換說明 */
#include "stdio.h"
#define PI 3.1415926int main(void)
{int number = 7;float pies = 12.75;int cost = 7800;printf("The %d contestants ate %f berry pies.\n", number, pies);printf("The value of pi is %f.\n", PI);printf("Farewell! thou art too dear for my possessing, \n");printf("%c%d\n", '$', 2 * cost);
}
程序運行結果
The 7 contestants ate 12.750000 berry pies.
The value of pi is 3.141593.
Farewell! thou art too dear for my possessing,
$15600
printf()
函數格式為
printf(格式字符串, 待打印項 1, 待打印項 2,...);
printf()
輸出百分號:使用兩個%
即可
/* printf 輸出 % */
#include "stdio.h"int main(void)
{int radio = 12;printf("%d%%", radio);
}
程序運行結果
12%
printf()的轉換說明修飾符
下圖所示為printf()
的修飾符

printf()
中的標記

使用修飾符和標記的示例程序(字段寬度打印輸出整數時的效果)
/* 字段寬度 */
#include "stdio.h"#define PAGES 959int main(void)
{printf("*%d*\n", PAGES);printf("*%2d*\n", PAGES);printf("*%10d*\n", PAGES);printf("*%-10d*\n", PAGES);}
程序運行結果
*959*
*959*
* 959*
*959 *
第一個轉換說明%d
不帶任何修飾符,其對應輸出結果與帶整數字段寬度的轉換說明的輸出結果相同;
第二個轉換說明是%2d
,其對應的輸出結果應該該是2字段度。因為待打印的整數有3位數字,所以字段寬度自動擴大以符合整數的長度;
第3個轉換說明是%10d
,其對應的輸出結果有10個空格寬度,實際上在兩個星號之間有7個空格和3位數字,并且數字位于字段的右側;
最后一個轉換說明是%-10d
,其對應的輸出結果同樣是10個空格寬度,-
標記說明打印的數字位于字段的左側.
浮點型格式效果
/* 一些浮點型修飾符的組合 *//* 一些浮點型修飾符的組合 */#include "stdio.h"int main(void)
{const double RENT = 3852.99; /* const 常量 */printf("*%f*\n", RENT); /* 字段寬度和小數點文書均為系統默認 小數點后打印6位數字 */printf("*%e*\n", RENT); /* %e 編譯器在小數點左側打印一個數字 小數點右側打印6個數字 */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); /* 補齊方式 */
}
程序運行結果
*3852.990000*
*3.852990e+003*
*3852.99*
*3853.0*
* 3852.990*
*3.853E+003*
*+3852.99*
*0003852.99*
其他組合
/* 演示一些格式標記 */#include "stdio.h"int main(void)
{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);}
程序運行結果
1f 1F 0x1f
**42** 42**-42**
** 6** 006**00006** 006**
第1行輸出中,1f
是十六進制數,等于十進制數31
,第1行 printf()
語句中,根據%x
打印出1f
,%F
打印出1F
,%#x
打印出0x1f
;
第 2 行輸出演示了如何在轉換說明中用空格在輸出的正值前面生成前導空格,負值前面不產生前導空格。這樣的輸出結果比較美觀,因為打印出來的正值和負值在相同字段寬度下的有效數字位數相同;
第 3 行輸出演示了如何在整型格式中使用精度(%5.3d)生成足夠的前導 0 以滿足最小位數的要求(本例是 3)。然而,使用 0 標記會使得編譯器用前導 0 填充滿整個字段寬度。最后,如果 0 標記和精度一起出現,0 標記會被忽略。
字符串格式的示例
/* 字符串格式 */#include "stdio.h"#define BLURB "Authentic imitation!"int main(void)
{printf("[%2s]\n", BLURB);printf("[%24s]\n", BLURB);printf("[%24.5s]\n", BLURB);printf("[%-24.5s]\n", BLURB);
}
程序運行結果
[Authentic imitation!]
[ Authentic imitation!]
[ Authe]
[Authe ]
-
標記使得文本左對齊輸出.
轉換(conversion)說明的意義
76
在計算機內部的存儲格式為二進制數0100 1100
, %d
轉換說明將其轉換成字符7
和6
,并顯示為76
;%x
轉換說明把相同的值0100 1100
轉化成十六進制計數法4c
,%c
轉換說明把0100 1100
轉換成字符L
轉換說明應該與待答應值得類型相匹配
以下是一些不匹配的整型轉換示例
/* 一些不匹配的整型轉換 */
#include "stdio.h"#define PAGES 336
#define WORDS 65618int 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);}
程序運行結果
num as short and unsigned short: 336 336
-num as short and unsigned short: -336 65200
num as int and char: 336 P
WORDS as int, short, and char: 65618 82 R
%u
表示無符號;
short int
的大小是2字節;系統采用二進制補碼來表示有符號整數;數字0~32767
代表它們本身,而32768~65535
則表示負數,其中65535
表示-1
,依此類推.
當 printf()使用%c 打印 336 時,它只會查看儲存 336 的 2 字節中的后 1 字節
用%hd 轉換說明打印時,printf()只使用最后 2 個字節
混淆整型和浮點型
/* 不匹配的浮點型轉換 */#include "stdio.h"int main(void)
{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);}
程序運行結果
3.0e+000 3.0e+000 9.9e-315 6.1e-315
2000000000 1234567890
0 0 2000000000 1234567890
第1 行輸出顯示,%e轉換說明沒有把整數轉換成浮點數;
float 類型的值作為 printf()參數時會被轉換成 double 類型。
在本系統中,float 是 4 字節,但是為了 printf()能正確地顯示該值,n1 被擴成 8 字節
參數傳遞
棧(stack)
printf("%ld %ld %ld %ld\n", n1, n2, n3, n4);
n1 被儲存在棧中,占 8 字節(float 類型被轉換成 double 類型)。同樣,n2 也在棧中占 8 字節,而 n3 和 n4 在棧中分別占 4 字節;
%ld 轉換說明表明 printf()應該讀取 4 字節
printf()函數的返回值
printf()函數也有一個返回值,它返回打印字符的個數。如果有輸出錯誤,printf()則返回一個負值
/* printf()的返回值 */#include "stdio.h"int main(void)
{int bph2o = 212;int rv;rv =printf("%d F is water's boiling point.\n", bph2o);printf("The printf() function printed %d characrters.\n", rv);return 0;
}
程序運行結果
212 F is water's boiling point.
The printf() function printed 32 characrters.
在字符串中,可以使用\n 來表示換行字符,但是不能通
過按下 Enter(或 Return)鍵產生實際的換行符。
打印較長字符串的方法
/* 打印較長字符串 */#include "stdio.h"int main(void)
{printf("Here's one way to print a ");printf("long string.\n");printf("Here's another way to print a \long string.\n");printf("Here's the newest way to print a ""long string.\n"); /* ANSI C */return 0;
}
程序運行結果
Here's one way to print a long string.
Here's another way to print a long string.
Here's the newest way to print a long string.
示例二
/* 打印較長字符串 */#include "stdio.h"int main(void)
{printf("Hello, young lovers, wherever you are.\n");printf("Hello, young" " lovers" ", wherever you are.\n");printf("Hello, young lovers" ", wherever you are.");}
程序運行結果
Hello, young lovers, wherever you are.
Hello, young lovers, wherever you are.
Hello, young lovers, wherever you are.