L-20 MCS 260 Fri 5 Oct 2001

In this lecture we discussed the binary number system and the different integer types, signed and unsigned, long and short. Here is how you can check the limits on your computer :
/* L-20 MCS 260 Fri 5 Oct 2001 : check the limits on your machine 

The program below prints the limits of the char and int types
to illustrate how a programmer gets access to these limits.        */

#include<stdio.h>
#include<limits.h>

int main(void)
{

   int create_overflow = INT_MAX;
   unsigned int avoid_overflow = INT_MAX;
   long long int a = ULONG_LONG_MAX;

   printf("\nSee appendix A on page 528 of the book:\n");
   printf("  number of bits in byte : %d\n", CHAR_BIT);
   printf("  maximal signed character code : %d\n", CHAR_MAX);
   printf("  minimal signed character code : %d\n", CHAR_MIN);
   printf("  maximal signed short integer : %d\n", SHRT_MAX);
   printf("  minimal signed short integer : %d\n", SHRT_MIN);
   printf("  maximal signed long integer  : %d\n", INT_MAX);
   printf("  minimal signed long integer  : %d\n", INT_MIN);
   printf("\nSome additional ones :\n");
   printf("  maximal unsigned character code : %d\n", UCHAR_MAX);
   printf("  maximal unsigned short integer : %d\n", USHRT_MAX);
   printf("  maximal unsigned long integer  : %u\n", UINT_MAX);
   printf("Notice the %cu in the last printf statement!!\n", 37);
   printf("... observe that also the last printf statement is special...\n");

   create_overflow++;
   avoid_overflow++;

   printf("\nOverflow gives a negative number : %d\n", create_overflow);
   printf("  maximal signed integer + 1 = minimal signed integer\n");
   printf("No overflow with unsigned int : %u\n\n", avoid_overflow);

   printf("This is the largest integer number : %llu\n\n", a);

   return 0;
}
The output of the above program follows :
See appendix A on page 528 of the book:
  number of bits in byte : 8
  maximal signed character code : 127
  minimal signed character code : -128
  maximal signed short integer : 32767
  minimal signed short integer : -32768
  maximal signed long integer  : 2147483647
  minimal signed long integer  : -2147483648

Some additional ones :
  maximal unsigned character code : 255
  maximal unsigned short integer : 65535
  maximal unsigned long integer  : 4294967295
Notice the %u in the last printf statement!!
... observe that also the last printf statement is special...

Overflow gives a negative number : -2147483648
  maximal signed integer + 1 = minimal signed integer
No overflow with unsigned int : 2147483648

This is the largest integer number : 18446744073709551615