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.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between fundamental data types and derived data types
- Print substring of a given string without using any string function and loop in C
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Print "Hello World" in C/C++ without using any header file
- C program to print a string without any quote (singe or double) in the program
- What happen when we exceed valid range of built-in data types in C++?
- Different ways to declare variable as constant in C and C++
- Interesting facts about data-types and modifiers in C/C++
- To find sum of two numbers without using any operator
- Implementing ternary operator without any conditional statement
- sizeof() for Floating Constant in C
- Can we access private data members of a class without using a member or a friend function?
- isgraph() C library function
- difftime() C library function
- wcstof function in C library
- Is there any need of "long" data type in C and C++?
- What are the data types for which it is not possible to create an array?
- Data Types in C
- C | Data Types | Question 1
- C | Data Types | Question 2