3

For most integer types, the minimum will be 1 "larger" than the max, right?

Like the range for long long is -9223372036854775808 to 9223372036854775807. When I'm running my program, why is it that both -9223372036854775808 and 9223372036854775808 give me a compiler warning and cause errors, as opposed to only the intmax_t max?

Because the intmax_t min should be valid at -9223372036854775808, no?

Edit: I also tried 9223372036854775807 and -9223372036854775807, they both work fine.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256

1 Answers1

6

(allow me to use 16-bit values for shorter numbers)

Technically, when you write -32768 in C, you have written: "Negative of the value 32768"

And 32,768 is too big for a positive 16-bit value, even though -32,768 is valid.

This is why you'll often see in header files like limits.h:

#define MAX_INT 32767
#define MIN_INT (-MAX_INT - 1)
/* Translates to -32767 - 1 ==> -32768 */

This question covers essentially the same information:

Why are there differing definitions of INT64_MIN? And why do they behave differently?

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • I'm not sure if this warrants an extra thread, but how would I change the MAX_INT or MIN_INT of an external header file? Is there any way I can make the min value valid for ? Well, I guess it's just giving me extra work since I'll have to deal with the minimum once I apply that, but I think it'll be useful to know. –  Feb 13 '18 at 21:44
  • The short answer is you cannot increase `INT_MAX` (or decrease `INT_MIN`) from values other than those specified in `limits.h`. Why? Because they are already set to the max/min that is possible for the number of bits per type. Anything larger than `INT_MAX` would require 33-bits instead of 32-bits. – David C. Rankin Feb 13 '18 at 21:53