How to print range of basic data types without any library function and constant in C?

How to write C code to print range of basic data types like int, char, short int, unsigned int, unsigned char etc?
It is assumed that signed numbers are stored in 2’s complement form.

We strongly recommend to minimize the browser and try this yourself first.

Following are the steps to be followed for unsigned data types.
1) Find number of bytes for a given data type using sizeof operator.
2) Find number of bits by multiplying result of sizeof with 8.
3) The minimum value for an unsigned type is always 0 irrespective of data type.
4) The maximum value of an unsigned type is (1 << n) - 1 where n is number of bits needed in data type. For example for char which typically requires 8 bits, the maximum value is 255. Following are the steps to be followed for signed data type. 1) Find number of bytes for a given data type using sizeof operator. 2) Find number of bits by multiplying result of sizeof with 8. 3) The minimum value for a signed type is -(1 << (n-1)). For example for char which typically requires 8 bits, the minimum value is -128. 4) The maximum value of a data type is (1 << (n-1)) - 1 where n is number of bits needed in data type. For example for char which typically requires 8 bits, the maximum value is 127. Following is C code to demonstrate above idea.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to print range of basic data types
#include <stdio.h>
  
// Prints min and max value for a signed type
void printUnsignedRange(size_t bytes)
{
    int bits = 8*bytes;
  
    // Note that the value of 'to' is "(1 << bits) - 1"
    // Writing it in following way doesn't cause overflow
    unsigned int to = ((1 << (bits-1)) - 1) + (1 << (bits-1)) ;
  
    printf(" range is from %u to %u \n", 0, to);
}
  
// Prints min and max value for an unsigned type
void printSignedRange(size_t bytes)
{
   int bits = 8*bytes;
   int from = -(1 << (bits-1));
   int to =  (1 << (bits-1)) - 1;
   printf(" range is from %d to %d\n", from, to);
}
  
int main()
{
    printf("signed char: ");
    printSignedRange(sizeof(char));
  
    printf("unsigned char: ");
    printUnsignedRange(sizeof(unsigned char));
  
    printf("signed int: ");
    printSignedRange(sizeof(int));
  
    printf("unsigned int: ");
    printUnsignedRange(sizeof(unsigned int));
  
    printf("signed short int: ");
    printSignedRange(sizeof(short int));
  
    printf("unsigned short int: ");
    printUnsignedRange(sizeof(unsigned short int));
  
    return 0;
}

chevron_right


Output:



signed char:  range is from -128 to 127
unsigned char:  range is from 0 to 255
signed int:  range is from -2147483648 to 2147483647
unsigned int:  range is from 0 to 4294967295
signed short int:  range is from -32768 to 32767
unsigned short int:  range is from 0 to 65535

Note that the above functions cannot be used for float. Also, the above program may not work for data types bigger that int, like ‘long long int’. We can make it work for bigger types by changing data type of ‘to’ and ‘from’ to long long int.

This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.