四、字符串和格式化输入/输出
4. print()和scanf()
■输入和输出函数,I/O函数
■两函数都使用格式字符串和参数列表
4.1 print()函数
■转换说明
转换说明及其打印的输出结果
转换说明 | 输出 |
---|---|
%a | 浮点数、十六进制数和p计数法(C99/C11) |
%A | 浮点数、十六进制数和p计数法(C99/C11) |
%c | 单个字符 |
%d | 有符号十进制整数 |
%e | 浮点数,e记数法 |
%E | 浮点数,e记数法 |
%f | 浮点数,十进制计数法 |
%g | 根据值的不同,自动选择%f或%e。%e格式用于指数小于-4或者大于或等于精度时 |
%G | 根据值的不同,自动选择%f或%E。%E格式用于指数小于-4或者大于或等于精度时 |
%i | 有符号十进制整数(与%d相同) |
%o | 无符号八进制整数 |
%p | 指针 |
%s | 字符串 |
%u | 无符号十进制整整 |
%x | 无符号十六进制整数,使用十六进制数0f |
%X | 无符号十六进制整数,使用十六进制数0F |
%% | 打印一个百分号 |
4.2 使用printf()
/* printout.c -- 使用转换说明 */
#include <stdio.h>
#define PI 3.141593
int 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);
return 0;
}
//The 7 contestants ate 12.750000 berry pies.
//The value of pi is 3.141593.
//Farewell! thou art too dear for my possessing,
//$15600
4.3 printf()的转换说明修饰符
/* width.c -- 字段宽度 */
#include <stdio.h>
#define PAGES 959
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
return 0;
}
//*959*
//*959*
//* 959*
//*959 *
// float.c -- 一些浮点型修饰符的组合
#include <stdio.h>
int main(void)
{
const double RENT =3852.99; // const 变量
printf("*%f*\n", RENT);
printf("*%e*\n", RENT);
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);
return 0;
}
//*3852.990000*
//*3852.990e+03*
//*3852.99*
//*3853.0*
//* 3852.990*
//* 3853E+03*
//*+3852.99*
//*0003852.99*
/* flags.c -- 演示一些格式标记 */
#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);
return 0;
}
//1f 1F 0x1f
//**42** 42**-42**
//** 6** 006**00006** 006**
/* stringf.c -- 字符串格式 */
#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);
return 0;
}
//[Authentic imitation!]
//[ Authentic imitation!]
//[ Authe]
//[Authe ]
4.4转换说明的意义
转换说明是翻译说明
■转换不匹配
/* intconv.c -- 一些不匹配的整型转换 */
#include <stdio.h>
#define PAGES 336
#define WORDS 65618
int 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);
return 0;
}
//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
■混淆整型和浮点型
/* floatcnv.c -- 不匹配的浮点型转换 */
#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("%1d %1d\n", n3, n4);
printf("%1d %1d %1d %1d\n", n1, n2, n3, n4);
return 0;
}
//3.0e+00 3.0e+00 3.1e+46 1.7e+266
//2000000000 1234567890
//0 1074266112 0 1074266112
■printf()的返回值
/* prntval.c -- 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 characters.\n", rv);
return 0;
}
//212 F is water's boiling point.
//The printf() function printed 32 characters.
■打印较长的字符串
/* longstrg.c -- 打印较长的字符串 */
//方法一:多个printf()语句
//方法二: 用反斜杠(\)和Enter(或Return)键组合来断行
//方法三: ANSIC引入的字符串连接
#include <stdio.h>
int main(void)
{
printf("Here's one way to print a");
printf("long string.\n");
printf("Here's anther 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 anther way to print a long string.
//Here's the newest way to print a long string.