C中的int类型有什么用?

What's the use of the int type in C?

我读到 C 编译器必须为 short 类型分配 2 个字节,并且必须为 long 类型分配 4 个字节。因此,根据系统的不同,类型 int 可以有 2 个字节或 4 个字节。那么,这样做的目的是什么?如果我们需要一个 2 字节的数字,我们可以使用 short,如果我们需要一个 4 字节的数字,我们可以使用 long。我觉得 int 系统是不是 16 位的就像赌博一样。 (我一般不明白为什么C不能像Python那样自己决定number需要多少内存)

在C的祖先B中,唯一的类型是int。它是“机器字”的大小,通常是指寄存器的大小——16 位系统上为 16 位,32 位系统上为 32 位,等等。 C 简单地保留了这种类型。 shortlong 是作为在需要更少或更多范围时控制存储 space 的方式引入的。这在可用内存受限时很重要:当您知道一个值永远不会超过 short 的范围时,为什么要分配 long

I generally don't understand why C can’t decide by itself how much memory is needed for number, just like Python does

Python 使用 arbitrary-precision representation. C decides this statically, and requires that it be specified by the programmer. There are statically typed languages in which type annotations are not required, due to type inference. If you want arbitrary-precision integers in C, you can use GMP 动态决定这一点,它提供 mpz_t 和许多其他类型和函数用于任意精度算术。