Count Ranges in C
Tags: c, learning, codenewbie
Published: 2024-03-09 15:20:39

[https://www.learntosolveit.com/](https://www.learntosolveit.com/) is my project that helped me become a software engineer. I continue to work on this fundamental project to help myself and others become good programmers. The project now is a companion website to learn C programming using K&R book. It uses modern tools, and is designed to be used along with the book.

---

> Write a program to determine the ranges of char, short, int, and long variables, both signed and unsigned, by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.

```c
#include <float.h>
#include <limits.h>
#include <stdio.h>

int main() {
    /* ranges of various integer types through calculation */
    printf("Ranges of various integer types through calculation:\n");

    printf("Minimum Signed Char %d\n", -(int)((unsigned char)~0 >> 1) - 1);
    printf("Maximum Signed Char %d\n", (int)((unsigned char)~0 >> 1));

    printf("Minimum Signed Short %d\n", -(int)((unsigned short)~0 >> 1) - 1);
    printf("Maximum Signed Short %d\n", (int)((unsigned short)~0 >> 1));

    printf("Minimum Signed Int %d\n", -(int)((unsigned int)~0 >> 1) - 1);
    printf("Maximum Signed Int %d\n", (int)((unsigned int)~0 >> 1));

    printf("Minimum Signed Long %ld\n", -(long)((unsigned long)~0 >> 1) - 1);
    printf("Maximum signed Long %ld\n", (long)((unsigned long)~0 >> 1));

    /* Unsigned Maximum Values */

    printf("Maximum Unsigned Char %d\n", (unsigned char)~0);
    printf("Maximum Unsigned Short %d\n", (unsigned short)~0);
    printf("Maximum Unsigned Int %u\n", (unsigned int)~0);
    printf("Maximum Unsigned Long %lu\n\n", (unsigned long)~0UL);

    /* Calculating max values of float types can be tricky, we can use the standard headers */

    /* ranges of various floating-point types from standard headers */
    printf("Ranges of various integer and floating-point types from standard headers:\n");
    printf("Minimum Signed Char %d\n", SCHAR_MIN);
    printf("Maximum Signed Char %d\n", SCHAR_MAX);

    printf("Minimum Signed Short %d\n", SHRT_MIN);
    printf("Maximum Signed Short %d\n", SHRT_MAX);

    printf("Minimum Signed Int %d\n", INT_MIN);
    printf("Maximum Signed Int %d\n", INT_MAX);

    printf("Minimum Signed Long %ld\n", LONG_MIN);
    printf("Maximum signed Long %ld\n", LONG_MAX);

    printf("Minimum Signed Long Long %lld\n", LLONG_MIN);
    printf("Maximum Signed Long Long %lld\n", LLONG_MAX);

    printf("Minimum Float %E\n", FLT_MIN);
    printf("Maximum Float %E\n", FLT_MAX);

    printf("Minimum Double %E\n", DBL_MIN);
    printf("Maximum Double %E\n", DBL_MAX);

    printf("Minimum Long Double %LE\n", LDBL_MIN);
    printf("Maximum Long Double %LE\n", LDBL_MAX);

    /* Unsigned Maximum Values */

    printf("Maximum Unsigned Char %d\n", UCHAR_MAX);
    printf("Maximum Unsigned Short %d\n", USHRT_MAX);
    printf("Maximum Unsigned Int %u\n", UINT_MAX);
    printf("Maximum Unsigned Long %lu\n", ULONG_MAX);
    printf("Maximum Unsigned Long Long %llu\n", ULLONG_MAX);

    return 0;
}
```

See the visual explanation of the program at
[https://www.learntosolveit.com/cprogramming/chapter2/ex_2.1_cal_limits](https://www.learntosolveit.com/cprogramming/chapter2/ex_2.1_cal_limits)