//第4章
//example 4.1
//#include <stdio.h>
//#include <string.h> //to include strlrn()
//#define DENSITY 62.4 //to define a constant
//int main()
//{
// float weight,volume;
// int size,letters;
// char name[40]; //to declare a digit group which is used to contain character string
// printf("Hi!What is your first name?\n");
// scanf("%s",name); //for character groups,"%s",no &
// printf("%s,what is your weight in pounds?\n",name);
// scanf("%f",&weight);
// size = sizeof name; //to get the bytes
// letters = strlen(name); //length
// volume =weight / DENSITY;
// printf("Well,%s,your volume is %2.2f cubic feet.\n",name,volume);
// printf("Also,your first name has %d letters,\n",letters);
// printf("and we have %d bytes to store it.\n",size);
// return 0;
// }
//example4.2.2
//#include <stdio.h>
//#define PRAISE "You are an extraordinary being."
//int main(void)
//{
// char name[40];
// printf("What is your name?");
// scanf("%s",name);
// printf("Hellow,%s.%s\n",name,PRAISE);
// return 0;
//}
//PS. scanf only reads the first wordds before blanks
//example 4.3
//#include <stdio.h>
//#include <string.h>
//#define PRAISE "You are an extraordinary being."
//int main(void)
//{
// char name[40];
// printf("What is your name?");
// scanf("%s",name);
// printf("Hello,%s.%s\n",name,PRAISE);
// printf("Your name of %zd letters occupies %zd memory cells.\n",
// strlen(name),sizeof name);
// printf("The phrase of praise has %zd letters.",strlen(PRAISE));
// printf("and occupies %zd memory cells.\n",sizeof PRAISE);
// return 0;
//}
//4.3
//define:to make a constant
//#define NAME value
//example4.4
//#include <stdio.h>
//#define PI 3.14159
//int main(void)
//{
// float area,circum,radius;
// printf("What is the radius of your pizza?\n");
// scanf("%f",&radius);
// area = radius * PI * radius;
// circum = 2.0 *radius*PI;
// printf("Your basic pizz parameters are as follows:\n");
// printf("circumference = %1.2f,area = %1.2f\n",circum,area);
// return 0;
//}
//'const' is used to make variables read-only
//'express constant' can be dirictly use as a certain value
//example 4.5
//#include <stdio.h>
//#include <limits.h>
//#include <float.h>
//int main(void)
//{
// printf("Some number limits for this system:\n");
// printf("Biggest int :%d\n",INT_MAX);
// printf("Smallest longlong :%lld\n",LLONG_MIN);
// printf("One byte = %d bits on this system.\n", CHAR_BIT);
// printf("Largest double: %e\n", DBL_MAX);
// printf("Smallest normal float: %e\n", FLT_MIN);
// printf("float precision = %d digits\n", FLT_DIG);
// printf("float epsilon = %e\n", FLT_EPSILON);
// return 0;
//}
//4.4.2
//example 4.6
//#include <stdio.h>
//#define PI 3.151493
//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("$%d\n",2*cost);
// /*equal to */
// //printf("%c%d\n",'$',2*cost);
// return 0;
//}
//printf(format character string,item1,item2,...)
//to print '%',use %%
//example 4.7
//#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;
//}
//example4.8
//#include <stdio.h>
//int main(void)
//{
// const double RENT=3852.99; //output:
// printf("*%f*\n", RENT); //3852.99
// printf("*%e*\n", RENT); //3.85299e3
// printf("*%4.2f*\n", RENT); //3852.99
// printf("*%3.1f*\n", RENT); //3853.0
// printf("*%10.3f*\n", RENT); //3852.990
// printf("*%10.3E*\n", RENT); //3.853e3
// printf("*%+4.2f*\n", RENT); //+3852.99
// printf("*%010.2f*\n", RENT); //0003852.99
// return 0;
//}
//why %10.nf '10' unshown
//example4.9
/* 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**
第1行输出中,1f是十六进制数,等于十进制数31。第1行printf()语句
中,根据%x打印出1f,%F打印出1F,%#x打印出0x1f。
第 2 行输出演示了如何在转换说明中用空格在输出的正值前面生成前导
空格,负值前面不产生前导空格。这样的输出结果比较美观,因为打印出来
的正值和负值在相同字段宽度下的有效数字位数相同。
第3行输出演示了如何在整型格式中使用精度(%5.3d)生成足够的前
导0以满足最小位数的要求(本例是3)。然而,使用0标记会使得编译器用
前导0填充满整个字段宽度。最后,如果0标记和精度一起出现,0标记会被
忽略
*/
//example 4.10
//#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;
//}
/* printf("[%字符串目标长度.取字符数s] ,item"); */
//学以致用
//#include <stdio.h>
//#define NAME "Halery"
//int main(void)
//{
// float income;
// income = 3000.00;
// printf("The %s family just may be $%.2f dollars richer!",NAME,income);
// return 0;
// }
//P208转换不匹配???
// the returned value of printf is the length of character strings
//example 4.13
//#include <stdio.h>
//int main(void)
//{
// int bph2o = 212;
// int rv;
// rv = printf("%dF is water 's boiling point.\n",bph2o);
// printf("The printf() function printed %d characters.\n",rv);
// return 0;
//}
//divide the character string in printf()
//example4.14
//#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");
//
// return 0;
//}
//4.15
//#include <stdio.h>
//int main(void)
//{
// int age;
// float assets;
// char pet[30];
// printf("Enter your age,assets,and favourite pet.\n");
// scanf("%d%f",&age,&assets);
// scanf("%s",pet);
// printf("%d$%.2f%s\n",age,assets,pet);
// return 0;
//}
//notice whether to use '&'
//a newline is available except %c
/*对于float类型和double类型,printf()都使用%f、%e、%E、%g和%G转换说
明。而scanf()只把它们用于float类型,对于double类型时要使用l修饰符 */
/*scanf
1.从第一个非空白开始,到结尾或第一个非空白或非类型结束
2.空白字符是克星。读取的字符串也不能含空白。
3. scanf("%d,%d", &n, &m);
用户必须像下面这样进行输入两个整数:88,121
4.%c的特殊性。
用空格来使空白available .
scanf("%c", &ch)从输入中的第1个字符开始读取,
而scanf(" %c", &ch)则从第1个非空白字符开始读取。 */
// *修饰符
//#include <stdio.h>
//int main(void)
//{
// unsigned width,precision;
// int number=256;
// double weight = 243.5;
// printf("Enter a field width:\n");
// scanf("%d",&width);
// printf("The number is :%*d:\n",width,number); // *指示了width
// printf("Now enter a width and a precision:\n");
// scanf("%d%d",&width,&precision);
// printf("Weight = %*.*f\n",width,precision,weight);
// printf("Done!\n");
// return 0;
//}
//#include <stdio.h>
//int main(void)
//{
// int n;
// printf("Please enter three integers:\n");
// scanf("%*d%*d%d",&n);
// printf("The last integer was %d\n",n);
// return 0;
//}
//使用足够大的固定字段宽度可以让输出整齐美观
//#include <stdio.h>
//int main(void)
//{
// int val1=12,val2=234,val3=1222;
// printf("%9d %9d %9d\n", val1, val2, val3);
// return 0;
//}
//exercise 4.7
//1
//#include <stdio.h>
//#include <string.h> //to include strlrn()
//#define DENSITY 62.4 //to define a constant
//int main()
//{
// float weight,volume;
// int size,letters;
// char name[40]; //to declare a digit group which is used to contain character string
// printf("Hi!What is your first name?\n");
// scanf("%s",name); //for character groups,"%s",no &
// printf("%s,what is your weight in pounds?\n",name);
// scanf("%f",&weight);
// size = sizeof name; //to get the bytes
// letters = strlen(name); //length
// volume =weight / DENSITY;
// printf("Well,%s,your volume is %2.2f cubic feet.\n",name,volume);
// printf("Also,your first name has %d letters,\n",letters);
// printf("and we have %d bytes to store it.\n",size);
// return 0;
// }
//只会读取名 ,因中间的空白喊停了scanf ,weight也得不到赋值。
/*2.a.
He sold the painting for $%234.50f.
b.
c.
His Hamlet was funny without being vulgar
has characters.
d.
Is 1.20e3 the same as 1201.00?
3.
c.#define Q "\"His Hamlet was funny without being vulgar.\""
printf("%s\nhas %dcharacters.\n", Q, strlen(Q));
*/
//4
//#include <stdio.h>
//#define B "booboo"
//#define X 10
//int main(void)
//{
// int age;
// int xp;
// char name[40];
// printf("Please enter your first name.\n");
// scanf("%s",name);
// printf("\nAll right,%s,what is your age?\n",name);
// scanf("%d",&age);
// xp = age + X;
// printf("That is a %s!You must be at least %d.\n",B,xp);
// return 0;
//}
//5
//#include <stdio.h>
//#define BOOK "War and Peace"
//int main(void)
//{
// float cost =12.99;
// float percent = 80.0;
// printf("This copy of \"%s\" sells for %.2f. \n",BOOK,cost);
// printf("That is %.0f%% of list.",percent);
// return 0;
//}
/*
6
%d;%0x;%10.3f;%12.2e;%-30s
;%4X;
(一个形如8A、字段宽度为4的十六进制整数) !!!十六进制是%x
7
%15ud;%4.#0x;%-12.2E;%+10.3f;%8s
%15lu;%#4x; ;%8.8s
(字段宽度为15的unsigned long类型的整数) !!! unsigned long类型是lu
(一个形如0x8a、字段宽度为4的十六进制整数) !!!字段宽度在#后
(一个字段宽度为8的字符串的前8个字符) !!!这里用精确到第八位来打印前八个字符
8
%6d;%nx;%2s;%+.2f;%-5s;
%6.4d;%*o;%2c; ;%7.5s;
a.(一个字段宽度为6、最少有4位数字的十进制整数)!!!用精准到第4位来表示至少有四位数字
b.(一个在参数列表中给定字段宽度的八进制整数)???
c.(一个字段宽度为2的字符)!!!字符用c
e.(一个字段宽度为7、左对齐字符串中的前5个字符)!!!别忘了字符宽度
*/
//9
/*
int n;
scanf("%d",&n);
float n;
scanf("%f",&n);
scsanf("%e",&n);
char pasta[20];
scanf("%s",pasta);
d.
char name[40]; char action[20];
scanf("%s",name); int value;
scanf("%s %d", action, &value); !!!字符串和数分开输入
#include <stdio.h>
int main(void)
{
char n[40];
scanf("%s",n);
printf("%s",n);
return 0;
}
此实际可行。22被识别为字符串的一部分
#include <stdio.h>
int main(void)
{
char n[40];
int m;
scanf("%s %d",n,&m);
printf("%s%d",n,m);
return 0;
}
此也实际可行,或许更规范。
int n; int value;
scanf("%s",n); scanf("%*s %d",&value);
10.空白包括空格、制表符和换行符;
scanf()使用空白分隔连续的输入项
11.
printf("The double type is %zd bytes..\n", sizeof(double));
%z 中的 z 是修饰符,不是转换字符,所以要在修饰符后面加上一个
它修饰的转换字符。可以使用%zd打印十进制数,或用不同的说明符打印不
同进制的数,例如,%zx打印十六进制的数。
12.
可以分别把(和)替换成{和}。但是预处理器无法区分哪些圆括号应替
换成花括号,哪些圆括号不能替换成花括号。因此,
#define ( {
#define ) }
int main(void)
(
printf("Hello, O Great One!\n");
)
将变成:
int main{void}
{
printf{"Hello, O Great One!\n"};
} */
//exercise 4.8
//1
//#include <stdio.h>
//int main(void)
//{
// char f[20];
// char l[20];
// printf("Please input your first name:");
// scanf("%s",f); //scanf自带换行
// printf("Please input your last name:");
// scanf("%s",l);
// printf("%s %s\n",f,l);
// return 0;
//}
//2.!!!名和姓一起输入的程序,则会有输入时不可含空格
//#include <stdio.h>
//int main(void)
//{
// char f[20];
// char l[20];
// printf("Please input your first name:");
// scanf("%s",f);
// printf("Please input your last name:");
// scanf("%s",l);
// printf("\"%s %s\"",f,l);
// return 0;
//}
//#include <stdio.h>
//int main(void)
//{
// char f[40];
// char l[40];
// printf("Please input your first name:");
// scanf("%s",f);
// printf("Please input your last name:");
// scanf("%s",l);
// printf("\"%s %s\"\n",f,l);
// return 0;
//}
//#include <stdio.h>
//int main(void)
//{
// char f[20];
// char l[20];
// printf("Please input your first name:");
// scanf("%s",f);
// printf("Please input your last name:");
// scanf("%s",l);
// printf("\"%s %-20s\"\n",f,l);
// return 0;
//}
//#include <stdio.h>
//#include <string.h>
//int main(void)
//{
// char f[20];
// char l[20];
// int a;
// printf("Please input your first name:");
// scanf("%s",f);
// printf("Please input your last name:");
// scanf("%s",l);
// a=strlen(f)+strlen(l)+3;
// printf("\"%*s %s\"\n",a,f,l);
// return 0;
//}
//3
//#include <stdio.h>
//int main(void)
//{
// float n;
// scanf("%f",&n);
// printf("小数点记数法:%.1f\n",n);
// printf("指数记数法:%.1e\n",n);
// return 0;
// }
//
// #include <stdio.h>
//int main(void)
//{
// float n;
// scanf("%f",&n);
// printf("小数点记数法:%.3f\n",n);
// printf("指数记数法:%.3e\n",n);
// return 0;
// }
//4
//#include <stdio.h>
//int main(void)
//{
// float h;
// char name[20];
// printf("Please input your height:");
// scanf("%f",&h); //scanf自带换行
// printf("Please input your name:");
// scanf("%s",name);
// printf("%s,you are %.3f feet tall.\n",name,h);
// return 0;
//}
//5
//#include <stdio.h>
//int main(void)
//{
// float speed;
// float size;
// float time;
// printf("Please input speed:");
// scanf("%f",&speed); //scanf自带换行
// printf("Please input the size:");
// scanf("%f",&size);
// time = size*8/speed;
// printf("At %.2f megabits per second,a file of %.2f megabytes\n\
//downloads in %.2f seconds.\n",speed,size,time);
// return 0;
//}
//6
//#include <stdio.h>
//#include <string.h>
//int main(void)
//{
// char f[20];
// char l[20];
// int a,b;
// printf("Please input your first name:");
// scanf("%s",f);
// printf("Please input your last name:");
// scanf("%s",l);
// a=strlen(f);
// b=strlen(l);
// printf("%20s %20s\n",f,l);
// printf("%20d %20d",a,b);
// return 0;
//}
//#include <stdio.h>
//#include <string.h>
//int main(void)
//{
// char f[20];
// char l[20];
// int a,b;
// printf("Please input your first name:");
// scanf("%s",f);
// printf("Please input your last name:");
// scanf("%s",l);
// a=strlen(f);
// b=strlen(l);
// printf("%-20s %-20s\n",f,l);
// printf("%-20d %-20d",a,b);
// return 0;
//}
//7
//#include <stdio.h>
//int main(void)
//{
// double a;
// float b;
// a=1.0/3.0;
// b=1.0/3.0;
// printf("%.6f %.12f %.16f\n",a,a,a);
// printf("%.6f %.12f %.16f",b,b,b);
// return 0;
//}
/*result
0.333333 0.333333333333 0.3333333333333333
0.333333 0.333333343267 0.3333333432674408 */
//8
//#include <stdio.h>
//#define V 3.785
//#define D 1.609
//int main(void)
//{
// float volum;
// float distance;
// float per;
// printf("Please input the volum:");
// scanf("%f",&volum);
// printf("Please input the distance:");
// scanf("%f",&distance);
// per = V*volum/100*D*distance;
// printf("100 kilometer %.1f gas.");
// return 0;
//}