Write a C program to find the range of data types using the c library and without it. In this programming language, all the range information, such as the minimum and maximum constants, will be within the limits and float header files.
The limits.h header file has information about integers and characters. At the same time, the float.h file has floating-point constants. In this c example, we use the limits header file and print the range of int and chars.
#include <stdio.h> #include<limits.h> int main() { printf("The Range of Signed Characters = %d to %d\n", SCHAR_MIN, SCHAR_MAX); printf("The Range of Unsigned Characters = 0 to %d\n", UCHAR_MAX); printf("The Range of Signed Integer = %d to %d\n", INT_MIN, INT_MAX); printf("The Range of Unsigned Integer = 0 to %d\n", UINT_MAX); printf("The Range of Signed Short Int = %d to %d\n", SHRT_MIN, SHRT_MAX); printf("The Range of Unsigned Short Int = 0 to %d\n", USHRT_MAX); printf("The Range of Signed Long = %ld to %ld\n", LONG_MIN, LONG_MAX); printf("The Range of Unsigned Long = 0 to %lu\n", ULONG_MAX); return 0; }
In this example, we used the float.h header to find the range of floating point data types.
#include <stdio.h> #include<float.h> int main() { printf("The Float = %e to %e\n", FLT_MIN, FLT_MAX); printf("The Double = %e to %e\n", DBL_MIN, DBL_MAX); printf("The Long Double = %Le to %Le\n", LDBL_MIN, LDBL_MAX); return 0; }
The Float = 1.175494e-38 to 3.402823e+38
The Double = 2.225074e-308 to 1.797693e+308
The Long Double = 3.362103e-4932 to 1.189731e+4932
In this program, we will find the range of all the available data types manually by creating functions.
#include <stdio.h> void sigRag(int bytes) { int bits = 8 * bytes; long long min = -(1LL << (bits - 1)); long long max = ((1LL << (bits - 1)) - 1); printf("%lld to %llu\n\n", min, max); } void unsiRa(int bytes) { int bits = 8 * bytes; unsigned long long max = (1LLU << (bits - 1)) + ((1LL << (bits - 1)) - 1); printf("0 to %llu\n\n", max); } int main() { printf("The Signed Characters = "); sigRag(sizeof(char)); printf("The Unsigned Characters = "); unsiRa(sizeof(unsigned char)); printf("The Signed Integer = "); sigRag(sizeof(int)); printf("The Unsigned Integer = "); unsiRa(sizeof(unsigned int)); printf("The Signed Short Int = "); sigRag(sizeof(short)); printf("The Unsigned Short Int = "); unsiRa(sizeof(unsigned short)); printf("The Signed Long = "); sigRag(sizeof(long)); printf("The Unsigned Long = "); unsiRa(sizeof(unsigned long)); printf("The Float = "); sigRag(sizeof(float)); printf("The Double = " ); sigRag(sizeof(double)); printf("The Long Long = "); sigRag(sizeof(long long)); printf("The Unsigned Long Long = "); unsiRa(sizeof(unsigned long long)); return 0; }
The Signed Characters = -128 to 127
The Unsigned Characters = 0 to 255
The Signed Integer = -2147483648 to 2147483647
The Unsigned Integer = 0 to 4294967295
The Signed Short Int = -32768 to 32767
The Unsigned Short Int = 0 to 65535
The Signed Long = -9223372036854775808 to 9223372036854775807
The Unsigned Long = 0 to 18446744073709551615
The Float = -2147483648 to 2147483647
The Double = -9223372036854775808 to 9223372036854775807
The Long Long = -9223372036854775808 to 9223372036854775807
The Unsigned Long Long = 0 to 18446744073709551615