#include<stdio.h> int main() { char name[3]; char family[3]; printf("Please input your name and family:\n"); scanf("%s %s",name,family); printf("%s,%s\n",name,family); return 0; }
a.打印名和姓,包括雙引號; b.在寬度爲20的字段右端打印名和姓,包括雙引號; c.在寬度爲20的字段左端打印名和姓,包括雙引號; d.在比姓名寬度寬3的字段中打印名和姓。code
#include<stdio.h> #include <string.h> int main() { char name[3]; printf("Please input your name:\n"); scanf("%s",name); printf("%s\n",name); printf("\"%s\"\n",name); printf("\"%20s\"\n",name); printf("\"%-20s\"\n",name); int len = (int)strlen(name) + 3; printf("%*s\n",len,name); return 0; }
a.輸入21.3或2.1e+001;ip
b.輸入+21.290或2.129E+001;input
#include<stdio.h> int main() { float num; printf("Please input the numbei:\n"); scanf("%f",&num); printf("%.3f\n",num); printf("%.2e\n",num); return 0; }
Dabney, you are 6.208 feet tall 使用float類型,並用/做爲除號。若是你願意,能夠要求用戶以釐米爲單位輸入身高,並以米爲單位顯示出來。string
#include<stdio.h> int main() { char name[3]; float aHeight; printf("Please input your name:"); scanf("%s",name); printf("Please input your aHeight:"); scanf("%f",&aHeight); printf("%s,you are %.3f feet tall",name,aHeight); return 0; }
At 18.12 megabits per second, a file of 2.20 megabytes downloads in 0.97 seconds.it
#include<stdio.h> int main(void) { float speed,big,time; printf("Please input the big:\n"); scanf("%f",&big); printf("Please input the speed:\n"); scanf("%f",&speed); time=big*8/speed; printf("At %.2f megabits per second, a file of %.2f megabytes downloads in %.2f seconds.",speed,big,time); return 0; }
Melissa Honeybeeio
7 8ast
接下來,再打印相同的信息,可是字母個數與相應名和姓的開頭對齊,以下所示:變量
Melissa Honeybeefile
7 8下載
#include <stdio.h> #include <string.h> int main() { char firstname[3]; char lastname[3]; printf("Please enter your first name:"); scanf("%s",firstname); printf("Please enter your last name:"); scanf("%s",lastname); printf("%s %s\n",firstname ,lastname); printf("%*d %*d\n",strlen(firstname),strlen(firstname) ,strlen(lastname),strlen(lastname)); printf("%s %s\n",firstname ,lastname); printf("%*d %*d\n",-strlen(firstname),strlen(firstname) ,-strlen(lastname),strlen(lastname)); return 0; }
#include <stdio.h> #include <float.h> int main() { double a = 1.0/3.0; float b = 1.0/3.0; printf("a = %.6f \nb = %.6f\n",a ,b); printf("a = %.12f \nb = %.12f\n",a ,b); printf("a = %.16f \nb = %.16f\n",a ,b); printf("double:%d\nfloat:%d\n\n",DBL_DIG ,FLT_DIG); return 0; }
#include <stdio.h> #include <string.h> #define GALLON 3.785 #define MILE 1.609 int main() { float mile; float gallon; printf("Please enter the mileage of the trip:\n"); printf("km:"); scanf("%f",&mile); printf("Please enter the amount of petrol you need to consume:\n"); printf("L:"); scanf("%f",&gallon); printf("The oil consumption is %.1f mile /gallon\n",mile / gallon); printf("Litre per 100km:%.1fL\n",gallon*GALLON/(mile*MILE)*100); return 0; }