I read Kernighan and Ritchie ("The C programming language. SE."), There is such a task:

Exercise 2.1. Write a program to determine the ranges of variable types of char, short, int, and long (both signed and unsigned) by outputting the corresponding values ​​from the header files, as well as using direct calculation. For the second method, let's complicate the task: define also the ranges of real types. ”

Everything is clear with the values ​​from the header files, but how to determine that the value exceeded the range?

    1 answer 1

    If the integer reaches its maximum value, then after the increment it will become zero for unsigned numbers and negative for signed ones. Overflow (wiki) . A faster way is to move the bit one to the left and determine the number of bits in the number. Then the maximum value is (2 ^ n) -1.

    • Thank you very much. Understood. ) I just did this first: char ch = 0; /* max 127 */ printf( "%i", ch + 300 ); char ch = 0; /* max 127 */ printf( "%i", ch + 300 ); He gave out 300. As I understand it, the result was brought to the int. Well, now I see. Thanks again for answering such a Nubian question. )) - LoOny