Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

C




// 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;
}


C++




#include <iostream>                /*1ULL is used to tell compiler use 1 which is having data type unsigned long long*/
using namespace std;            /*if we use 1 only it will be explained as 1 of data type int which will further */
                                /*give us a wrong output by giving a warning: left shift count >= width of type [-Wshift-count-overflow]*/
int main() {
 
    cout<<"signed char: the range is from "<<(1 << ((sizeof(char)*8)))<<" to "<< ~(1 << ((sizeof(char)*8)-1));
 
    cout<<"\nunsigned char: the range is from "<<0<<" to "<< ~((1 << ((sizeof(char)*8)-1))+(1 << (sizeof(char)*8)));
 
    cout<<"\nsigned int: the range is from "<<(1ULL << ((unsigned long long)(sizeof(int)*8)))<<" to "<< ~(1ULL << ((unsigned long long)(sizeof(int)*8)-1ULL));
 
    cout<<"\nunsigned int: the range is from "<<0<<" to "<< ~((1ULL << ((unsigned long long)(sizeof(int)*8)-1ULL))+(1ULL << (unsigned long long)(sizeof(int)*8)));
    
      cout<<"\nsigned float: the range is from "<<(1ULL << ((unsigned long long)(sizeof(float)*8)))<<" to "<< ~(1ULL << ((unsigned long long)(sizeof(float)*8)-1ULL));
 
    cout<<"\nunsigned float: the range is from "<<0<<" to "<< ~((1ULL << ((unsigned long long)(sizeof(float)*8)-1ULL))+(1ULL << (unsigned long long)(sizeof(float)*8)));
 
      return 0;
}


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.

 



Last Updated : 25 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads