【4.8.1】
# include <stdio.h>
int main()
{
char last_name[20];
char first_name[20];
printf("Please enter your first_name:\n");
scanf("%s",first_name);
printf("Please enter your last_name:\n");
scanf("%s",last_name);
printf("Your whole name is %s,%s.\n",first_name,last_name);
return 0;
}
【4.8.2】
# include <stdio.h>
# include <string.h>
int main()
{
int width;
char name[40];
printf("Please enter your name:\n");
scanf("%s", name);
printf("\"%s\"\n", name);
printf("\"%20s\"\n", name);
printf("\"%-20s\"\n", name);
width = strlen(name) + 3;
printf("%*s\n", width, name);
return 0;
}
【4.8.3】
# include <stdio.h>
int main()
{
float a;
printf("Please enter a floating number:\n");
scanf("%f",&a);
printf("The input is %f or %.1e\n",a,a);
printf("The input is +%5.3f or %5.3E\n",a,a);
return 0;
}
【4.8.4】
# include <stdio.h>
int main()
{
char name[40];
float height;
printf("Hello,what's your name?\n");
scanf("%s",name);
printf("How tall are you?\n");
scanf("%f",&height);
printf("%s,you are %1.3f m tall\n",name,height/100);
return 0;
}
【4.8.5】
# include <stdio.h>
int main()
{
float speed,size,time;
printf("Please enter the speed of download:\n ");
scanf("%f",&speed);
printf("Please enter the file size:\n");
scanf("%f",&size);
time=(size*8)/speed;
printf("At %2.2f magabits per second ,a file of %2.2f magabytes download in %2.2f seconed.\n",speed,size,time);
return 0;
}
【4.8.6】
# include <stdio.h>
# include <string.h>
int main()
{
char first_name[20],family_name[20];
printf("Please enter your first_name:\n");
scanf("%s",first_name);
printf("Please enter your family_name:\n");
scanf("%s",family_name);
printf("%s %s\n",first_name,family_name);
printf("%*d %*d\n",strlen(first_name),strlen(first_name),strlen(family_name),strlen(family_name));
printf("%s %s\n",first_name,family_name);
printf("%-*d %-*d\n",strlen(first_name),strlen(first_name),strlen(family_name),strlen(family_name));
return 0;
}
【4.8.7】
# include <stdio.h>
# include <float.h>
int main()
{
double m=1.0/3.0;
float n=1.0/3.0;
printf("%.6f %.12f %.16f\n",m,m,m);
printf("%.6f %.12f %.16f\n",n,n,n);
printf("double类型的最少有效数字位数:%d,\nfloat类型的最少有效数字位数:%d\n",DBL_DIG,FLT_DIG);
return 0;
}
【4.8.8】
# include <stdio.h>
# define GALLON 3.785
# define MILE 1.609
int main()
{
float gallon,mile;
printf("Please enter the miles of the trip:\n");
scanf("%f",&mile);
printf("Please enter the amount of the gas:\n");
scanf("%f",&gallon);
printf("The oil consuption is %.1f mile/gallon.\n",mile/gallon);
printf("Litre per 100Km:%.1f L.\n",gallon*GALLON/(mile*MILE)*100);
return 0;
}