Data types, along with their size and range, are crucial components in C programming. The sizeof operator provides the amount of memory needed to store a particular value. Understanding a type's range is essential in order to avoid overflow and underflow issues during programming. C mandates the precise minimum storage size for every integer variation. For instance, a short necessitates a minimum of two bytes, while a long necessitates a minimum of four bytes. The compiler establishes both the size and range of a data type, underscoring the importance of not embedding size and range values directly into the program.
Find a range of data types manually without a C library
The provided equation establishes the lower and upper bounds of a signed data type:
- -(2N-1) to 2N-1 - 1 (Here, N represents the size of the type multiplied by 8, indicating the total bit capacity of the integer type).
The provided equation establishes the lower and upper boundaries of an unsigned data type:
- 0 to (2N-1) + (2N-1 - 1)
Examples:
Let's consider an example to determine the range of integer values in C programming.
#include <stdio.h>
void printUnsignedRange(int bytes)
{
int bits = 8 * bytes;
unsigned long long to = (1LLU << (bits - 1)) + ((1LL << (bits - 1)) - 1);;
printf(" 0 to %llu\n\n", to);
}
void printSignedRange(int bytes)
{
int bits = 8 * bytes;
long long from = -(1LL << (bits - 1));
long long to = (1LL << (bits - 1)) - 1;
printf(" %lld to %lld\n\n", from, to);
}
int main()
{
/* print the range of integer type */
printf("Range of int =");
printSignedRange(sizeof(int));
printf("Range of unsigned int =");
printUnsignedRange(sizeof(unsigned int));
/* print the range of character type */
printf("Range of char =");
printSignedRange(sizeof(char));
printf("Range of unsigned char =");
printUnsignedRange(sizeof(unsigned char));
/*print the range of long type */
printf("Range of long =");
printSignedRange(sizeof(long));
printf("Range of unsigned long =");
printUnsignedRange(sizeof(unsigned long));
/* print the range of short type */
printf("Range of short =");
printSignedRange(sizeof(short));
printf("Range of unsigned short =");
printUnsignedRange(sizeof(unsigned short));
/* print the range of long long type */
printf("Range of long long =");
printSignedRange(sizeof(long long));
printf("Range of unsigned long long =");
printUnsignedRange(sizeof(unsigned long long));
return 0;
}
After running this code, the resulting output will be displayed as demonstrated below:
Range of int = -2147483648 to 2147483647
Range of unsigned int = 0 to 4294967295
Range of char = -128 to 127
Range of unsigned char = 0 to 255
Range of long = -9223372036854775808 to 9223372036854775807
Range of unsigned long = 0 to 18446744073709551615
Range of short = -32768 to 32767
Range of unsigned short = 0 to 65535
Range of long long = -9223372036854775808 to 9223372036854775807
Range of unsigned long long = 0 to 18446744073709551615
Find the range of data types using a C library
The technique outlined previously for acquiring any type of range is intriguing, however, it is not advisable for practical application. Leveraging the capabilities of a predefined C library is frequently suggested instead.
The header files limits.h and float.h in C programming define constants for minimum and maximum values. limits.h specifies constants for integer and character types, including ranges for small and large sizes, total bits, and more. On the other hand, float.h is used for defining characteristics of floating-point numbers.
Example:
Now, let's consider an example to demonstrate how we can determine the range of whole numbers using the C standard library.
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main()
{
/* print the range of signed integer type */
printf("Range of signed int %d to %d\n", INT_MIN, INT_MAX);
/* print the range of unsigned integer type */
printf("Range of unsigned int 0 to %u\n\n", UINT_MAX);
/* print the range of signed char type */
printf("Range of signed char %d to %d\n", SCHAR_MIN, SCHAR_MAX);
/* print the range of unsigned char type */
printf("Range of unsigned char 0 to %d\n\n", UCHAR_MAX);
/* print the range of signed long integer type */
printf("Range of signed long int %ld to %ld\n", LONG_MIN, LONG_MAX);
/* print the range of unsigned long integer type */
printf("Range of unsigned long int 0 to %lu\n\n", ULONG_MAX);
/* print the range of signed short integer type */
printf("Range of signed short int %d to %d\n", SHRT_MIN, SHRT_MAX);
/* print the range of unsigned short integer type */
printf("Range of unsigned short int 0 to %d\n\n", USHRT_MAX);
/* print the range of float type */
printf("Range of float %e to %e\n", FLT_MIN, FLT_MAX);
/* print the range of double type */
printf("Range of double %e to %e\n", DBL_MIN, DBL_MAX);
/* print the range of long double type */
printf("Range of long double %Le to %Le\n", LDBL_MIN, LDBL_MAX);
return 0;
}
After running the code above, the resulting output will be displayed as depicted below:
The output is:
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 long int -9223372036854775808 to 9223372036854775807
Range of unsigned long int 0 to 18446744073709551615
Range of signed short int -32768 to 32767
Range of unsigned short int 0 to 65535
Range of float 1.175494e-38 to 3.402823e+38
Range of double 2.225074e-308 to 1.797693e+308
Range of long double 3.362103e-4932 to 1.189731e+4932
Example:
Develop a script that reads an integer data type and verifies its position within a specified range.
#include <stdio.h>
int main() {
int x;
printf("\nInput an integer: ");
scanf("%d", &x);
if(x >=0 && x <= 25)
{
printf("Range [0, 25]\n");
}
else if(x >=26 && x <= 40)
{
printf("Range (26,50]\n");
}
else if(x >=41 && x <= 60)
{
printf("Range (45,75]\n");
}
else if(x >61 && x <= 80) {
printf("Range (61,80]\n");
}
else
{
printf("Outside the range\n");
}
return 0;
}
After running the code provided above, the resulting output will be displayed as demonstrated below:
The Output is:
Input an integer: 28
Range [26,50]
Example:
The sizeof operator is utilized to ascertain the size of a specific data type, such as an integer. Below is a program demonstrating how to employ the sizeof operator to determine the sizes of different integer types within the system.
#include <stdio.h>
int main()
{
printf("sizeof(short) = %d bytes\n",sizeof(short));
printf("sizeof(int) = %d bytes\n",sizeof(int));
printf("sizeof(unsigned int) = %d bytes\n",sizeof(unsigned int));
printf("sizeof(long) = %d bytes\n",sizeof(long));
return 0;
}
After running the code above, the result will be displayed as indicated below:
The output is:
sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(unsigned int) = 4 bytes
sizeof(long) = 8 bytes