自己总结的一些注意事项都在注释中
/*《C Primer Plus》第四张“字符串和格式化输入/输出复习题练习”*/
/*1.输入名和姓
名和姓之间有一个空格,scanf函数的读取规则是遇到第一个非空白字符停止,下一个字符将作为下一次读取的开头*/
/*2.打印结果*/
#include<stdio.h>
#define BOOK "War and Peace"
#define B "booboo"
#define X 10
#define Q "His Hamlet was funny without being vulgar."
int main(void)
{
printf("He sold the painnting for $2.2f.\n", 2.345e2);//a
/*打印的最终数字为:$2.2f*//*我认为错误的原因是没有定义字符串的数据类型*/
printf("%c%c%c\n", 'H', '105', '\41');//b
/*最终打印的结果为:H5!*//*我认为错误的原因是单打印字符没有空字符\0,导致乱码*/
printf("%s\n has %d characters.\n", Q, strlen(Q));//c
/*最终打印的结果为:
His Hamlet was funny without being vulgar
has %d characters.
输出的内容没有问题,换行后printf()函数会读取空格,有别于scanf()函数*/
printf("Is %2.2e the same as %2.2f?\n", 1201.0, 1201.0);//d
/*输出结果为:Is 1.20e+3 the same as 1201.00?
如果字段不能容纳待打印的数字或字符串,系统会自动使用更宽的字段*/
/*3.输出第二题中c带双引号的Q*/
printf("\"%s\n\" has %d characters.\n", Q, strlen(Q));/*使用转义序列\"可打印双引号*/
/*4.找错误*/
/*正解(部分在代码开头)*/
float age;
char name[40];
printf("Please enter your first name.");
scanf_s("%s", name,40);/*使用scanf_s函数输入字符串时,要在末尾标上存储单元,否则会自动退出*/
printf("All right,%s,what's your age?", name);
scanf_s("%f", &age);
int xp = age + X;
printf("That's a %s !You must be at least %d.\n", B, xp);
/*5.构造语句*/
const float price = 12.99;
float percent = 80.0;
printf("This copy of \"%s\" sells for $%.2f\n", BOOK, price);
printf("That's %%%.0f of list", percent);/*打印百分号:%%*/
/*6.7.8使用什么转换说明*/
/*注意:
6b.无字符十六进制整数大小写分别为%x与%X
7a.unsigned long 整数用%lu
7b.加#显示进制前缀,不加不显示
8b.给定字段宽度的八进制整数:%#o*/
/*9.输入并声明变量和数组*/
int lo = 101;
scanf_s("%d", &lo);
float wn = 22.32;
double it = 8.34E-09;
scanf_s("%f%e", wn, it);
char linguini[40];
scanf_s("%s", linguini,40);
char catch[40];
int hh = 22;
scanf_s("%s %d", catch, hh,40);
printf("%s %d", catch, hh);
scanf_s("%*s %d", catch, hh,40);
/*10.空格、制表符、换行符即为空白*/
/*11.z是修饰符,改成%zd*/
/*12.预处理器分不清什么圆括号可以代替花括号,什么花括号可以代替圆括号,所以不可行*/
}