2.1
编写一个程序,调用一次 printf() 函数,把你的名和姓打印在一行,在调用一次 printf() 函数,把你的名和姓打印在两行。然后,在调用两次 printf() 函数,把你的名和姓打印在一行
#include <stdio.h>
int main(void)
{
char surname[10] = "Mahler"; //姓
char name[10] = "Gustav"; //名
printf("%s %s\n", name, surname);
printf("%s\n", name);
printf("%s\n", surname);
printf("%s ", name);
printf("%s\n", surname);
return 0;
}
2.3
编写一个程序把你的年龄转换为天数
#include <stdio.h>
int main(void)
{
int age = 18;
printf("days are %d", age * 365);
return 0;
}
2.4
编写一个程序,生成:
Brazil, Russia, India, China
India, China
Brazil, Russia
除了 mian() 外,自定义两个函数 br() 和 ic(),调用一次 br() 打印一次Brazil, Russia,调用一次 ic()打印一次India, China
#include <stdio.h>
void br(void)
{
printf("Brazil, Russia");
}
void ic(void)
{
printf("India, China");
}
int main(void)
{
br();
printf(", "); //打印逗号和空格
ic();
printf("\n");
ic();
printf("\n");
br();
return 0;
}
3.2
编写一个程序,提示输入一个ASCII码值,然后打印相应的字符
#include <stdio.h>
int main(void)
{
int ascii;
printf("输入一个ASCII码值:>");
scanf_s("%d", &ascii);
printf("对应的字符是\"%c\"", ascii);
return 0;
}
3.3
发出警告,然后打印
Starled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"
#include <stdio.h>
int main(void)
{
printf("\aStarled by the sudden sound, Sally shouted,\n\"By the Great Pumpkin, what was that!\"\n");
return 0;
}
3.4
编写一个程序,读取一个浮点数,先打印小数点格式,再打印成指数形式,最后打印成十六进制计数法格式(P计数法)
#include <stdio.h>
int main(void)
{
float f;
printf("Enter a floating-point value:>");
scanf_s("%f", &f);
printf("fixed-point notation: %f\n", f);
printf("exponential notation: %e\n", f);
printf("exponential notation: %a\n", f);
return 0;
}
3.5
一年大约3.156*10^7秒,将年龄转换为秒
#include <stdio.h>
int main(void)
{
int age, second;
printf("输入你的年龄:>");
scanf_s("%d", &age);
second = age * 3.156e7;
printf("你活了 %d 秒\n", second);
return 0;
}
3.6
1分子质量为3.0*10^-23克,一夸脱水约950克,计算n夸脱水有多少个水分子
#include <stdio.h>
int main(void)
{
int water;
float molecule;
printf("输入水的夸脱数:>");
scanf_s("%d", &water);
molecule = water / 3e-23;
printf("有 %e 个水分子\n", molecule);
return 0;
}
3.8
1品脱=2杯,1杯=8盎司,1盎司=2大汤勺,1大汤勺=3茶勺,输入杯数,以其他单位显示
#include <stdio.h>
int main(void)
{
float cups;
printf("输入杯数:>");
scanf_s("%f", &cups);
printf("对应品脱数: %f\n", cups / 2);
printf("对应盎司数: %f\n", cups * 8);
printf("对应大汤勺数:%f\n", cups * 8 * 2);
printf("对应茶勺数: %f\n", cups * 8 * 2 * 3);
return 0;
}
PS.此处使用浮点类型避免杯数除以二求品脱数时的值不是整数