C Program to Find the Ranges of Data Types

Ranges of Data Types:
The range of a data type gives us the minimum and maximum value that can be stored inside the variable of the given data type.

Examples:
Range of int = -2147483648 to 2147483647
Range of char = -128 to 127

Problem Description

Write a C Program to find the range of basic data types.

Problem Solution

1. Convert the bytes into bits.
2. For signed data types, use formula -2(n-1) to (2(n-1)) – 1.
3. For unsigned data types, use formula 0 to (2n) – 1. Where n is the number of bits in both the cases.

There are two ways to write a C program to find the fundamentals of basic data types.

Method 1: (Without using C Library)

In this approach, we find the range of basic data types manually without using C library.

advertisement
advertisement
Program/Source Code

Here is source code of the C Program to print the range of datatypes without using C library. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print the Range of Datatypes
  3.  */
  4. #include <stdio.h>
  5. #define SIZE(x) sizeof(x)*8
  6.  
  7. void signed_one(int);
  8. void unsigned_one(int);
  9.  
  10. void main()
  11. {
  12.     printf("range of int");
  13.     signed_one(SIZE(int));
  14.  
  15.     printf("\nrange of unsigned int");
  16.     unsigned_one(SIZE(unsigned int));
  17.  
  18.     printf("\n\nrange of char");
  19.     signed_one(SIZE(char));
  20.  
  21.     printf("\nrange of unsigned char");
  22.     unsigned_one(SIZE(unsigned char));
  23.  
  24.     printf("\n\nrange of short");
  25.     signed_one(SIZE(short));
  26.  
  27.     printf("\nrange of unsigned short");
  28.     unsigned_one(SIZE(unsigned short));
  29. }
  30.  
  31. /* RETURNS THE RANGE SIGNED*/
  32. void signed_one(int count)
  33. {
  34.     int min, max, pro;
  35.     pro = 1;
  36.     while (count != 1)
  37.     {
  38.         pro = pro << 1;
  39.         count--;
  40.     }
  41.     min = ~pro;
  42.     min = min + 1;
  43.     max = pro - 1;
  44.     printf("\n%d to %d", min, max);
  45. }
  46.  
  47. /* RETURNS THE RANGE UNSIGNED */
  48. void unsigned_one(int count)
  49. {
  50.     unsigned int min, max, pro = 1;
  51.  
  52.     while (count != 0)
  53.     {
  54.         pro = pro << 1;
  55.         count--;
  56.     }
  57.     min = 0;
  58.     max = pro - 1;
  59.     printf("\n%d to %d", min, max);
  60. }
Program Explanation

1. Convert the number of bytes into bits by multiplying the bytes with 8.
2. Use two functions namely signed_one() and unsigned_one() for calculating the range of signed and unsigned data types respectively.
3. Value got at step 1 is sent as a parameter to both the functions. In both the function, it is received by variable count.
4. Initialize the variable pro to 1 in both the functions.
5. In the function signed_one() using while loop with the condition (count != 1), shift the variable pro to its left by 1 position and decrement the variable count by 1 consecutively.
6. When the loop terminates, assign the complement of pro to the variable min and increment the min by 1. Decrement the variable pro and assign it to the variable max. Print min and max as output.
7. In the function unsigned_one() using while loop with the condition (count !=0), shift the variable pro to its left by 1 position and decrement the variable count by 1 consecutively.
8. When the loop terminates, assign zero to the variable min. Decrement the variable pro and assign it to the variable max. Print min and max as output.

Example:
Consider, the range of signed char data type. For signed range, signed_one function will be called. Size of char is 1 byte = 8 bits. So, the value of count = 8. Run the while loop until count becomes 1. Initially the value of pro variable is set to 1. Enter the while loop and left shift the value of pro at each iteration. As the value of count initially is 8 and loop terminates at count = 1. So, left shift will occur 7 times, the decimal value of which is equal to 128. Store complement of pro variable in min variable and increment it’s value by 1 i.e., min = -128. In the max variable store the value of (pro-1) i.e., max = 127. Print the value of min and max as range of char data type.

Time Complexity: O(n)
The above program for finding range of basic data types has a time complexity of O(n), where n is number of bits in the particular data type.

Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

Run Time Testcases

The output range for the fundamental data types of int, char, and short is shown below.

range of int
-2147483648 to 2147483647
range of unsigned int
0 to -1
 
range of char
-128 to 127
range of unsigned char
0 to 255
 
range of short
-32768 to 32767
range of unsigned short
0 to 65535

advertisement
Method 2: (Using C Library)

In this approach, we find the range of basic data types using C library.

Program/Source Code

Here is source code of the C Program to print the range of datatypes using C library. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print the Range of Datatypes using C library
  3.  */
  4. #include <stdio.h>
  5. #include <limits.h>
  6.  
  7. void main()
  8. {
  9.     printf("Range of signed int \n%d to %d", INT_MIN, INT_MAX);
  10.     printf("\nRange of unsigned int \n0 to %u\n\n", UINT_MAX);
  11.  
  12.     printf("Range of signed char \n%d to %d\n", SCHAR_MIN, SCHAR_MAX);
  13.     printf("Range of unsigned char \n0 to %d\n\n", UCHAR_MAX);
  14.  
  15.     printf("Range of signed short int \n%d to %d\n", SHRT_MIN, SHRT_MAX);
  16.     printf("Range of unsigned short int \n0 to %d\n\n", USHRT_MAX);
  17.  
  18.     printf("Range of signed long int \n%ld to %ld\n", LONG_MIN, LONG_MAX);
  19.     printf("Range of unsigned long int \n0 to %lu\n\n", ULONG_MAX);
  20. }
Program Explanation

In this approach we are directly using inbuilt C Libraries. The header file “limits.h” is used to find minimum and maximum constants of integer and character data type.

All the C library used in the above program are called macros.

advertisement
  • INT_MIN = represents minimum integer value i.e. (-2147483648)
  • INT_MAX = represents maximum integer value i.e. (2147483647)
  • UNIT_MAX = represents maximum integer value of unsigned numbers i.e. (0 to 4294967295)
  • SCHAR_MIN = represents minimum character value i.e. (-128)
  • SCHAR_MAX = represents maximum character value i.e. (127)
  • UCHAR_MAX = represents maximum character value of unsigned characters i.e. (0 to 255)
  • SHRT_MIN = represents minimum value for an object of type short int i.e (-32768)
  • SHRT_MAX = represents maximum value for an object of type short int i.e. (32767)
  • USHRT_MAX = SHRT_MIN = represents maximum value for an object of type unsigned short int i.e. (0 to 65535)
  • LONG_MIN = represents minimum value for an object of type long int i.e. (-2147483648)
  • LONG_MAX = represents maximum value for an object of type long int i.e. (2147483647)
  • ULONG_MAX = SHRT_MIN = represents maximum value for an object of type unsigned long int i.e. (0 to 4294967295)

Time Complexity: O(1)
The above program for finding range of basic data types has a time complexity of O(1). As all the lines are being executed exactly once.

Space Complexity: O(1)
In the above program, space complexity is O(1) as no variable has been taken to store the values in the memory.

Run Time Testcases

Here is the output for the basic data types of INT_MIN, INT_MAX, UINT_MAX, SCHAR_MIN, SCHAR_MAX, UCHAR_MAX, SHRT_MIN, SHRT_MAX, USHRT_MAX, LONG_MIN, LONG_MAX and ULONG_MAX.

Range of signed int
-2147483648 to 2147483647
Range of unsigned int
0 to 4294967295
 
Range of signed char
-128 to 127
Range of unsigned char
0 to 255
 
Range of signed short int
-32768 to 32767
Range of unsigned short int
0 to 65535
 
Range of signed long int
-2147483648 to 2147483647
Range of unsigned long int
0 to 4294967295

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.