DEV Community

Cover image for Count Ranges in C
Senthil Kumaran
Senthil Kumaran

Posted on • Updated on

Count Ranges in C

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.

#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;
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (4)

Collapse
 
orsenthil profile image
Senthil Kumaran • Edited

Lastly, since the macros from the standard headers exists, it really is pointless to do it yourself.

It is an exercise to understand how the limits can be determined.

Updated the program with rest of your feedback. Thank you. I have changed the titles.

Determining the min and max value of floats using calculation can expand the scope. I have used macros for FLT_MIN, FLT_MAX etc. For rest, the both the calculated values and the macro value should match. That was my test case. The linked document should show the output.

Thanks for the feedback. I really appreciate it.

Collapse
 
pauljlucas profile image
Paul J. Lucas

(FYI: comments have a Reply button so your reply will appear attached to and nested under my comment — plus I would have gotten a notification that you replied.)

Your casting is still wrong. The left-most cast must not be either char or short. For example, it should be this:

(int)((unsigned char)~0 >> 1)
Enter fullscreen mode Exit fullscreen mode

The left-most cast must be int for %d.

Collapse
 
orsenthil profile image
Senthil Kumaran

(int)((unsigned char)~0 >> 1)

I see what you meant with %d when using wrong cast. Fixed that. Thank you.

Collapse
 
pauljlucas profile image
Paul J. Lucas

You say "floating point types" but you're printing the ranges of every type except floating point types (float and double).

And casting to anything other than int then using %d is also wrong. Also, even for variadic functions, things like char or short are promoted to int anyway, so those casts are also pointless for that reason.

You're also forgot long long and unsigned long long.

Lastly, since the macros from the standard headers exists, it really is pointless to do it yourself.