Size and Limits of Datatypes in C Language
Program Introduction:
In this article, We are going to write a program to calculate the size and limits of Datatypes in C programming.
The Limits of datatype mean, The Minimum allowed value and Maximum allowed value for a given datatype.
Example Size and limits of datatypes in C:
Let’s take character datatype. The size of character datatype is one ( 1 ) Byte.
And Limits of character datatype are Maximum available value is 127 and the Minimum value is -128.
So similar way we need to calculate the size and minimum and maximum limit of all datatypes of c programming language
Size and Limits of Datatypes in C Programming:
We are going to use the built-in sizeof operator to calculate the size of each datatype.
We calculate the Maximum limit and Minimum limit of a datatype using the C programming language limits.hlibrary.
The limits.hlibrary contains the Macros with all available variables Minimum limit and Maximum Limit.
For Example, To get the minimum value of the Integer variable, You first need to include the limits.h Library to your program. Then use the Macros INT_MINand INT_MAX of climit.h library. The INT_MINMacro provides the Minimum allowed integer value and The INT_MAX contains the Maximum allowed Integer value.
Similarly, We can calculate the Minimum value and Maximum value of all available datatypes of C Language.
Program: Size and Limits ( Minimum and Max ) of all datatypes in C:
We are using the climit.h library for getting the limits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include<stdio.h> #include<limits.h> #include<float.h> int main(void) { // Let's Get the size of all datatypes using the 'sizeof' operator printf("\n----- Size of all Datatypes of C Language -----------\n"); printf("sizeof(char) = %lu \n",sizeof(char)); // Sizeof expects unsigned long integer, so 'ul' Format specifier printf("sizeof(short) = %lu \n",sizeof(short)); printf("sizeof(int) = %lu \n",sizeof(int)); printf("sizeof(long) = %lu \n",sizeof(long)); printf("sizeof(float) = %lu \n",sizeof(float)); printf("sizeof(double) = %lu \n",sizeof(double)); printf("sizeof(long double) = %lu \n",sizeof(long double)); printf("\n------ Limits of All Datatypes ----------------------\n"); printf("Character Minimum value - SCHAR_MIN = %d \n", SCHAR_MIN); printf("Character Maximum value - SCHAR_MAX = %d \n", SCHAR_MAX); printf("Unsigned Character Max value - UCHAR_MAX = %d \n", UCHAR_MAX); printf("Short Int Minimum value - SHRT_MIN = %d \n", SHRT_MIN); printf("Short Int Maximum value - SHRT_MAX = %d \n", SHRT_MAX); printf("Unsigned Short Int Maximum value - USHRT_MAX = %u \n", USHRT_MAX); printf("Int Minimum value - INT_MIN = %d \n", INT_MIN); printf("Int Maximum value - INT_MAX = %d \n", INT_MAX); printf("Unsigned Int Maximum value - UINT_MAX = %u \n", UINT_MAX); printf("Long Int Minimum value - LONG_MIN = %ld \n", LONG_MIN); printf("Long Int Maximum value - LONG_MAX = %ld \n", LONG_MAX); printf("Unsigned Long Int Maximum value - ULONG_MAX = %lu \n", ULONG_MAX); // We can also use '%f' format specifier printf("Float Minimum value - FLT_MIN = %e \n", FLT_MIN); printf("Float Maximum value - FLT_MAX = %e \n", FLT_MAX); // We can also use '%lf' format specifier printf("Double Minimum value - DBL_MIN = %e \n", DBL_MIN); printf("Double Maximum value - DBL_MAX = %e \n", DBL_MAX); printf("Long Double Minimum value - LDBL_MIN = %Le \n", LDBL_MIN); printf("Long Double Maximum value - LDBL_MAX = %Le \n", LDBL_MAX); // Number of digits of precision after decimal point. printf("\n------ Precision of Floating Point data -------------\n"); printf("Float Precision - FLT_DIG = %d \n", FLT_DIG); printf("Double Precision - DBL_DIG = %d \n", DBL_DIG); printf("Long Double Precision - LDBL_DIG = %d \n", LDBL_DIG); return 0; } |
In the above program, We calculated Min and Max of all datatypes. We also displaying the precision of all floating point datatypes like float, double, and Long double.
Program Output:
We are compiling the program using the gcc compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
venkey@venkey$ gcc datatypes-Size.c venkey@venkey$ ./a.out ----- Size of all Datatypes of C Language ----------- sizeof(char) = 1 sizeof(short) = 2 sizeof(int) = 4 sizeof(long) = 8 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 16 ------ Limits of All Datatypes ---------------------- Character Minimum value - SCHAR_MIN = -128 Character Maximum value - SCHAR_MAX = 127 Unsigned Character Max value - UCHAR_MAX = 255 Short Int Minimum value - SHRT_MIN = -32768 Short Int Maximum value - SHRT_MAX = 32767 Unsigned Short Int Maximum value - USHRT_MAX = 65535 Int Minimum value - INT_MIN = -2147483648 Int Maximum value - INT_MAX = 2147483647 Unsigned Int Maximum value - UINT_MAX = 4294967295 Long Int Minimum value - LONG_MIN = -9223372036854775808 Long Int Maximum value - LONG_MAX = 9223372036854775807 Unsigned Long Int Maximum value - ULONG_MAX = 18446744073709551615 Float Minimum value - FLT_MIN = 1.175494e-38 Float Maximum value - FLT_MAX = 3.402823e+38 Double Minimum value - DBL_MIN = 2.225074e-308 Double Maximum value - DBL_MAX = 1.797693e+308 Long Double Minimum value - LDBL_MIN = 3.362103e-4932 Long Double Maximum value - LDBL_MAX = 1.189731e+4932 ------ Precision of Floating Point data ------------- Float Precision - FLT_DIG = 6 Double Precision - DBL_DIG = 15 Long Double Precision - LDBL_DIG = 18 venkey@venkey$ |
Here is my original output.
The Size and Min, Max Values of all Datatypes
Here is the summary of above output.
Datatype | Size | Minimum Value | Maximum Value |
---|---|---|---|
Character (
char) | 1 | -128 | 127 |
Unsigned Character | 1 | 1 | 255 |
Short Integer ( short) | 2 | -32768 | 32767 |
Unsigned Short Int | 2 | 1 | 65535 |
Integer ( int ) | 4 | -2147483648 | 2147483647 |
Unsigned Integer | 4 | 1 | 4294967295 |
Long Integer ( long ) | 8 | -9223372036854775808 | 9223372036854775807 |
Unsigned Long Int | 8 | 1 | 18446744073709551615 |
Float | 4 | 1.175494e-38 | 3.402823e+38 |
Double | 8 | 2.225074e-308 | 1.797693e+308 |
Long Double | 16 | 3.362103e-4932 | 1.189731e+4932 |
Conclusion:
In this article, We have discussed the sizes of all datatypes in the C Language. We also looked into the maximum allowed value and minimum limit of all C language datatypes like integer, float, and characters, etc. We also looked at the precision of floating-point datatypes.
9 Responses
[…] Size and Limits of Datatypes in C Language – SillyCodes […]
[…] Size and Ranges (Limits) of all Datatypes […]
[…] Size and Limits of Datatypes in C Language – SillyCodes […]
[…] Sizes or limits of Datatypes in C Languages […]
[…] C Program to calculate Size and Limits of all C Datatypes […]
[…] C Program to calculate Size and Limits of all C Datatypes […]
[…] Create a variable called min. Initialize the min with INT_MAX. The value of the INT_MAX is 2147483647. Learn more about the INT_MAX at Size and Limits of datatypes […]
[…] Then, create two variables, largest and second_largest, and initialize them with the INT_MIN. ( Learn more about the INT_MIN at Size and Limits of datatypes in C) […]
[…] We displayed the sizes of all variables and pointer variables using the sizeof operator in C […]