Wow! I never expected such a detailed analysis of a cartoon!Cute, but...
What is the size of an integer in the "C" standard?
Is a long int guaranteed to be larger than an int?
Where does a short int fit into this?
Answers:
Int is to be at least 16 bits. It is allowed to be larger and often is. Some compilers have ints of 32 and 64 bits. Even longer is allowed, and multiples of 2 are not required.
A long int is only guaranteed to be no shorter than an int. It is not required to be longer. If int is 16 bits, a 16 bit long int is allowed. In this case, using a long int would make no difference.
Short int is only required to not be longer than an int. If an int is 64 bits, even a short int would be allowed to be 64 bits also.
The moral of this story is: NEVER rely on the length of integer types! All signed and unsigned integer lengths are allowed to vary by compiler.
The usual way to deal with this is to have a define file specific to the compiler being used where types like int16_t or int32_t are defined to translate to the proper type in that compiler. When you switch compilers, all you have to do is switch to the proper header file.
Example:
//compiler #1
#define int16_t short int
#define int32_t int
#define int64_t long int
(in a separate file)
//compiler #2
#define int16_t int
#define int32_t long int
#define int64_t #error unsupported type
Then use int##_t instead of the built-in types, which should never be used directly.
From the Web page cited in the link above, with emphasis added.
I don't see that this contradicts what I said in post #4.Rule #6:
Whenever the width, in bits or bytes, of an integer value matters in the program, a fixed-width data type shall be used in place of char, short, int, long, or long long. The signed and unsigned fixed-width integer types shall be as shown in Table 1.
Reasoning: The ISO C standard allows implementation-defined widths for char, short, int, long, and long long types, which leads to portability problems. Though the 1999 standard did not change this underlying issue, it did introduce the uniform type names shown in the table, which are defined in the new header file <stdint.h>. These are the names to use even if you have to create the typdefs by hand.
But it does contradict what was in #1.I don't see that this contradicts what I said in post #4.
If you're implementing a sheep counter circuit for insomniacs, don't forget to declare sheepCount as a long int.
In the big picture, "long int" is not guaranteed to be longer than an int, and there are many compilers where it is not. Using "int" and "long int" is part of the definition of writing non-portable code.Whenever the width, in bits or bytes, of an integer value matters in the program, a fixed-width data type shall be used in place of char, short, int, long, or long long.
minimum value for an object of type int INT_MIN -32767 // −(2^15 − 1)
maximum value for an object of type int INT_MAX +32767 // 2^15 − 1
maximum value for an object of type unsigned int UINT_MAX 65535 // 2^16 − 1
Does cartooning count as twiddling or foundation?For twiddling in the basement, this may be OK, but as a foundation for a career, not so much so.