#c语言学习
1.通过试验(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。
链接:原文链接
整型:
基本整形(int):一般占据2个或者4个字节,这都是由编译系统决定的。在计算机中整型数据一般是按补码的形式存储的;
短整型(short int):分配2个字节。存储方式与基本整形相同,一个短整型的数据范围是:-32768-32767,即 -215 ~ (215 - 1);
长整型数据(long int):分配它4个字节,范围为 -231 ~ (231 - 1);
无符号长整型数据(unsigned long int):分配它4个字节,范围为 0 ~ 4294967295,即 0 ~ (232 - 1);
双长整型(long long int):在vc中系统给它分配8个字节,这种数据类型一般比较少用。
整数溢出:当达到最大值时,将会溢出到起始点;当达到最小值时,将会从最大点开始往变小方向溢出
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
//int 类型在32位机器中通常为32位
int zhengshu1, zhengshu2;
unsigned int zhengshu3;
zhengshu1 = 2147483647;
zhengshu2 = -2147483647;
zhengshu3 = 4294967295;
//整数的上溢,直接从-2147483648开始
printf("%d\t%d\t%d\n", zhengshu1, zhengshu1 + 1, zhengshu1 + 2);
//下溢也从-2147483648开始
printf("%d\t%d\t%d\n", zhengshu2, zhengshu2 - 1, zhengshu2 - 2);
//无符号整数上溢从0开始
printf("%u\t\t%u\t\t%u\n", zhengshu3, zhengshu3 + 1, zhengshu3 + 2);
return 0;
}
运行结果:
2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印输入的字符。
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int number;
printf("Please enter a number:");
scanf_s("%d",&number);
printf("\nThe character corresponding to this number is %c\n", number);
return 0;
}
运行结果:
3.编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
“By the Great Pumpkin, what was that!”
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
printf("\aStartled by the sudden sound, Sally shouted,\n");
printf("\"By the Great Pumpkin, what was that!\"\n");
return 0;
}
运行结果:
4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。按以下格式输出(实际显示的指数位数因系统而异):
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
float a;
printf("Enter a floating-point value:");
scanf_s("%f", &a);
printf("fixed-point notation:%f\n", a);
printf("exponential notation:%e\n", a);
printf("p notation:%a\n", a);
return 0;
}
运行结果:
5.一年大约有3.156*10^7秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int age;
int second= 31560000;
printf("Please input your age:");
scanf_s("%d", &age);
printf("\nyou have lived in the earth for %d seconds!",age*second);
return 0;
}
运行结果:
6.1个水分子的质量约为3.0*10^(-23)克。一夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int quart;
printf("Enter the number of water in quart:");
scanf_s("%d", &quart);
printf("The number of water molecules is:%e\n", quart * 950 / 3.0e-23);
}
运行结果:
7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
float h;
printf("please enter your height :");
scanf("%f", &h);
printf("Your height in centimeter is:%.2f\n", h * 2.54);
return 0;
}
运行结果:
8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用浮点类型比整数类型更合适?
#define _CRT_SECURE_NO_WARNING 1
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
float i;
printf("Enter the number of cup:");
scanf("%f",&i);
printf("%.2f cup is equal to %.2f pint\n",i,i/2);
printf("%.2f cup is equal to %.2f ounce\n",i,i*8);
printf("%.2f cup is equal to %.2f soup ladle\n",i,i*8*2);
printf("%.2f cup is equal to %.2f tea ladle\n",i,i*8*2*3);
return 0;
}
运行结果: