#include
int main(void)
{
char gn[15], fn[15];
printf("What's ur given name and family name\?\n");
scanf_s("%s %s", gn, 15, fn, 15);
printf("Print: %s, %s", gn, fn);
getchar();
getchar();
return 0;
}
运行结果
What's ur given name and family name?
gname fname
Print: gname, fname
TIPS: 2015之后,使用scanf_s代替scanf,需要在读入字符串类型的变量后描述数组大小,本例中scanf_s的参数15可以使用sizeof函数替代。程序开始时,printf即把内容发送到了缓冲区,遇到(1)缓冲区满(2)换行字符(3)需要输入的情况才会刷新缓冲区,更新打印结果。
a.打印名字,包括双引号;
b.在宽度为20的字段右端打印名字,包括双引号;
c.在宽度为20的字段左端打印名字,包括双引号;
d.在比姓名宽度宽3的字段中打印名字
#include
#include
int main(void)
{
char name[20];
printf("Input your name:\n");
scanf_s("%s", name, 20);
printf("Print:\n");
printf("\"%s\"\n", name);
printf("\"%20s\"\n", name);
printf("\"%-20s\"\n", name);
printf("\"%*s\"\n", strlen(name) + 3, name);
getchar();
getchar();
return 0;
}
运行结果:
Input your name:
Amie
Print:
"Amie"
" Amie"
"Amie "
" Amie"
a.The input is 21.3 or 2.1e+001
b.The input is +21.290 or 2.129E+001
#include
int main(void)
{
float num1 = 21.3, num2 = 21.29;
printf("a:The input is %.1f or %.1e\n",num1,num1);
printf("b:The input is %.3f or %.3e",num2,num2);
getchar();
return 0;
}
Dabney, you are 6.208feet tall.
使用float类型,并用/作为除号。
#include
int main(void)
{
float height;
char name[10];
printf("请输入姓名和身高,以“姓名 身高(cm)”的格式输入:\n");
scanf_s("%s %f", name,10, &height);
printf("%s, you are %f metres high.",name, height/100.00);
getchar();
getchar();
return 0;
}
At 18.12 megabits per second, a file of 2.20 megabytes downloads in 0.97 seconds.
#include
int main(void)
{
float DLspeed, Fsize;
printf("请输入下载速度(Mb/s),文件大小(MB)\n");
scanf_s("%f,%f", &DLspeed, &Fsize);
printf("At %.2f megabits per second, a file of %.2f megabytes downloads in %.2f seconds.", DLspeed, Fsize, Fsize * 8 / DLspeed);
getchar();
getchar();
return 0;
}
#include
#include
int main(void)
{
char givenname[15], familyname[15];
printf("Pls input your name:\n");
scanf_s("%s%s", givenname, 15, familyname, 15);
printf("%s %s\n", givenname, familyname);
printf("%*d %*d\n", strlen(givenname), strlen(givenname), strlen(familyname), strlen(familyname));
getchar();
getchar();
return 0;
}
#include
#include
int main(void)
{
char givenname[15], familyname[15];
printf("Pls input your name:\n");
scanf_s("%s%s", givenname, 15, familyname, 15);
printf("%s %s\n", givenname, familyname);
printf("%-*d %-*d\n", strlen(givenname), strlen(givenname), strlen(familyname), strlen(familyname));
getchar();
getchar();
return 0;
}
#include
#include
int main(void)
{
printf("Input 1.0/3.0 and print it:\n");
printf("%.6lf %.6f\n", 1.0 / 3.0, 1.0 / 3.0);
printf("%.12lf %.12f\n", 1.0 / 3.0, 1.0 / 3.0);
printf("%.16lf %.16f\n", 1.0 / 3.0, 1.0 / 3.0);
printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG);
getchar();
return 0;
}
Input 1.0/3.0 and print it:
0.333333 0.333333
0.333333333333 0.333333333333
0.3333333333333333 0.3333333333333333
FLT_DIG = 6, DBL_DIG = 15
#include
#define LITER_PER_GALLON 3.785
#define KM_PER_MILE 1.609
int main(void)
{
float mileage, gasoline;
printf("请输入您的里程数(英里)和耗油量(加仑)\n");
scanf_s("%f %f", &mileage, &gasoline);
printf("欧洲燃料消耗表示(英里/加仑):%.2f\n", mileage/gasoline);
printf("美国燃料消耗表示(L/100km):%.2f", 100 * LITER_PER_GALLON * gasoline / KM_PER_MILE / mileage);
getchar();
getchar();
return 0;
}