1 // 4.8.1
2 #include<stdio.h>
3 int main(void)
4 {
5 char x[5];
6 char m[10];
7
8 printf("请输入您的姓式:");
9 scanf("%s", x);
10 printf("请输入名:");
11 scanf("%s", m);
12 printf("%s,%s", x, m);
13
14 return 0;
15 }
这里主要注意转义说明;
1 #include<stdio.h>
2 #include<string.h>
3 int main(void)
4 {
5 char name[10];
6 int width;
7
8 printf("请输入姓名:");
9 scanf("%s", name);
10 printf("\n\"%s\"", name);
11 printf("\n\"%20s\"", name);
12 printf("\n\"%-20s\"", name);
13 printf("\n%lu", strlen(name));
14 width = strlen(name) + 3;
15 printf("\n\"%*s\"", width, name);
16
17 return 0;
18 }
strlen() 函数的应用和" * "通配符的应用。
1 #include<stdio.h>
2 int main(void)
3 {
4 double num;
5
6 printf("Please input a float: ");
7 scanf("%lf", &num);
8 printf("a. The input is %.1f or %.1e\n", num, num);
9 printf("b. The input is %+.3f or %.3e", num, num);
10
11 return 0;
12 }
scanf()函数:1·读取数字时,不要忘了指针&;
2·读取字符串的时候,不需要指针。
1 #include<stdio.h>
2 int main(void)
3 {
4 float hight;
5
6 printf("请输入身高(cm):");
7 scanf("%f", &hight);
8 printf("Dabney, you are %.3f metres tall", hight/100);
9
10 return 0;
11 }
1 #include<stdio.h>
2 int main(void)
3 {
4 float v,r;
5
6 printf("下载速度(Mb/s): ");
7 scanf("%f", &v);
8 printf("文件大小(MB): ");
9 scanf("%f", &r);
10 printf("At %.2f megabits per second, a file of %.2f megabytes",
11 v, r);
12 printf("\ndownloads in %.2f seconds.", r*8/v);
13
14 return 0;
15
16 }
2 #include<stdio.h>
3 #include<string.h>
4 int main(void)
5 {
6 char x[10], m[10];
7 int lenth1,lenth2;
8
9 printf("请输入姓式:");
10 scanf("%s", x);
11 printf("请输入名字:");
12 scanf("%s", m);
13 printf("%s %s", x, m);
14 lenth1 = strlen(x);
15 lenth2 = strlen(m);
16 printf("\n%*d %*d", lenth1, lenth1, lenth2, lenth2);
17 printf("\n%-*d %-*d", lenth1, lenth1, lenth2, lenth2);
18
19 return 0;
20 }
1 #include<stdio.h>
2 #include<float.h>
3 int main(void)
4 {
5 double num1 = 1.0/3.0;
6 float num2 = 1.0/3.0;
7
8 printf("Double: %.6f %.12f %.16f\n", num1, num1,num1);
9 printf("Float: %.6f %.12f %.16f\n", num2, num2, num2);
10 printf("FLT_DIG is %d\n", FLT_DIG);
11 printf("DBL_DIG is %d", DBL_DIG);
12
13 return 0;
14 }
1 #include<stdio.h>
2 #define km 1.609
3 int main(void)
4 {
5 double metres, gal, per, change;
6 const float L = 3.785;
7
8 printf("请输入旅行的里程:");
9 scanf("%lf", &metres);
10 printf("请输入消耗的汽油量:");
11 scanf("%lf", &gal);
12 per = metres/gal;
13 printf("Metres per galon: %.1f", per);
14 change = (metres*km)/(gal*L);
15 printf("\n(m/g->km/L): %.1f", change);
16
17 return 0;
18 }
最后是限定符const和定义**#define**的使用了。