Beginner CPP I would like to ask that the memory occupied by the short type must be less than the memory occupied by the int type?

the book says, "if short is smaller than int, then using short can save memory."
do not understand "if short is smaller than int"
CPP stipulates that short at least 16 bits int is at least as long as short
so when will short be larger than int?

CPP
Apr.05,2021

means that different compilers may differ in details


short will not be larger than int


the same compilation environment, and it is true that the short type will not be greater than int


if in all systems, the width of each type is the same, it will be very convenient to use, for example, short is always 16-bit, int is always 32-bit, and so on; but life is not that simple, your CPP code may run on different terminals, such as 32-bit machines, 64-bit machines, or even run on ancient single-chip microcontrollers, etc., the word length of CPU is different in these different environments.

attached reference article: those details of CPP-- differences between 32-bit and 64-bit data types

so in order to adapt to different hardware environments as much as possible, CPP provides a flexible standard to make your code more portable:

  • short at least 16 bits
  • int is at least as long as short;
  • long is at least 32 bits and at least as long as int
  • long long at least 64 bits and at least as long as long

because it is the standard of CPP, as long as you are using a compiler that conforms to the CPP specification, it is inevitable that short saves more memory than int.

Menu