我的 C 编码类简介的这个问题指出:
Write a program that will determine the biggest positive value that your machine can achieve for each of the following data types. (and thus you can figure out the number of bits used for each of the data types). In order to find the biggest value, use a simple loop structure that increments a variable of that datatype, until it overflows.
它指示我使用多种数据类型来执行此操作,例如 int
、char
、short int
等。
我不太确定问题在问什么,我是否应该创建一个无限循环,接受某种数据类型的变量,并每次增加它的值,直到它变得太大以至于程序中断?或者它问的是完全不同的东西
最佳答案
当数据太大而无法容纳数据类型时,就会发生溢出,即值溢出并丢失精度。
假设我们正在使用两位,所以我们有这个表
00
01
10
11
这些是所有适合的数字值,以两位为单位。二进制中的 11
是十进制中的 3,如果您递增它,您将得到 100
,如果您将其截断为较低的有效位,则为 00
这是 0,这就是您正在搜索的行为。
我将把实现作为你的练习,因为这是家庭作业
关于c - 递增变量直到溢出的循环结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58002093/