Art of C programming

Variable ranges

Ranges of char, short, int and long variables, both signed and unsigned

From standard headers and by direct computation.

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

int main()
{
    printf("char with limits.h\n");
    printf("  char: %d to %d\n", CHAR_MIN, CHAR_MAX);
    printf("  unsigned char: 0 to %d\n\n", UCHAR_MAX);
    printf("char with direct calculation\n");
    printf("  char: %d to %d\n", -(1 << (8-1)), (1 << (8-1)) - 1);
    printf("  unsigned char: 0 to %d\n\n", (1 << 8) - 1);

    printf("short with limits.h\n");
    printf("  short: %d to %d\n", SHRT_MIN, SHRT_MAX);
    printf("  unsigned short: 0 to %d\n\n", USHRT_MAX);
    printf("short with direct calculation\n");
    printf("  short: %d to %d\n", -(1 << (16-1)), (1 << (16-1)) - 1);
    printf("  unsigned short: 0 to %d\n\n", (1 << 16) - 1);

    printf("int with limits.h\n");
    printf("  int: %d to %d\n", INT_MIN, INT_MAX);
    printf("  unsigned int: 0 to %u\n\n", UINT_MAX);
    printf("int with direct calculation\n");
    printf("  int: %ld to %ld\n", -(1L << (32-1)), (1L << (32-1)) - 1);
    printf("  unsigned int: 0 to %ld\n\n", (1L << 32) - 1);

    printf("long with limits.h\n");
    printf("  long: %ld to %ld\n", LONG_MIN, LONG_MAX);
    printf("  unsigned long: 0 to %lu\n\n", ULONG_MAX);
    printf("long with direct calculation\n");
    printf("  long: %ld to %ld\n", -(1UL << (64-1)), (1UL << (64-1)) - 1);
    printf("  unsigned long: 0 to %llu\n\n", ~0ULL);
}
                    
                

Compile

cc ranges.c -o ranges

Run

./ranges

char with limits.h
  char: -128 to 127
  unsigned char: 0 to 255

char with direct calculation
  char: -128 to 127
  unsigned char: 0 to 255

short with limits.h
  short: -32768 to 32767
  unsigned short: 0 to 65535

short with direct calculation
  short: -32768 to 32767
  unsigned short: 0 to 65535

int with limits.h
  int: -2147483648 to 2147483647
  unsigned int: 0 to 4294967295

int with direct calculation
  int: -2147483648 to 2147483647
  unsigned int: 0 to 4294967295

long with limits.h
  long: -9223372036854775808 to 9223372036854775807
  unsigned long: 0 to 18446744073709551615

long with direct calculation
  long: -9223372036854775808 to 9223372036854775807
  unsigned long: 0 to 18446744073709551615

Source code