Reputation: 39
We all know that all the data types which we used in c has fix limit like
char range from -128 to 127
integer store value from -2147483648 to 2147483647
Like this all have fix limits.
My question is very simple that if we use header file we can easily find out the range of all data types. But is there any process or logic through which we can find limits of all these data types without using Any predefined function or header files or Macros??
If any please solved out my query
and I also want to know about header file. Is there any method to vary the number of digit after decimal in float ??
Upvotes: 3
Views: 2574
Reputation: 3483
I'm pretty new to C but I noticed that if you decrement an unsigned type it will be assigned the maximum value.
I've included limits.h
just to compare the results.
#include <stdio.h>
#include <limits.h>
int main(void)
{
unsigned char c;
unsigned short s;
unsigned int i;
unsigned long l;
// char
printf("UCHAR_MAX = %u\n", UCHAR_MAX);
printf("UCHAR_MAX = %u\n", --c);
// short
printf("USHRT_MAX = %u\n", USHRT_MAX);
printf("USHRT_MAX = %u\n", --s);
// int
printf("UINT_MAX = %u\n", UINT_MAX);
printf("UINT_MAX = %u\n", --i);
// long
printf("ULONG_MAX = %lu\n", ULONG_MAX);
printf("ULONG_MAX = %lu\n", --l);
}
And the results:
UCHAR_MAX = 255
UCHAR_MAX = 255
USHRT_MAX = 65535
USHRT_MAX = 65535
UINT_MAX = 4294967295
UINT_MAX = 4294967295
ULONG_MAX = 18446744073709551615
ULONG_MAX = 140731079060271
As you can see it works for all except long, as to why.. well maybe someone more experienced than me can explain.
Upvotes: 1
Reputation: 78943
The header files are there for a reason, for signed integer types the admissible range depends on properties of the types that are otherwise not observable. (For unsigned types it is simple, BTW, the minimum is always 0
and the maximum the converted value of -1
).
A signed type depends on
CHAR_BIT
Upvotes: 1
Reputation: 6557
The range of a data type depends on how many bits it has.
For example, char is stored on 1 byte (= 8 bits), which gives the range (-2^7) - (2^7-1).
So the formula for signed data types is:
2^(((sizeof(t)-1) * 8) to 2^((sizeof(t)-1) * 8) - 1.
For unsigned data types it's:
0 to 2^((sizeof(t)) * 8) - 1.
Where t is the data type. It's multiplied by 8, cause sizeof returns size in bytes.
This formula doesn't apply to floating point types since they're stored differently. You could take a look at this question or this wiki page to have a better understanding on that. I was never in the need to work with numbers nearing the limit AND make it run cross-platform, so the limit was always a constant value for me.
Upvotes: -1
Reputation: 6003
Your question is warranted.
Consider the second assertion it the question:
integer store value from -2147483648 to 2147483647
This is true only on a 32-bit system. On a 16-bit system an integer may store a value in the -32768 to 32767 range.
As you indicate in the question, most programmers rely on header files such as limit.h to determine ranges of data types.
One way to calculate integer limits is to use 'sizeof({the type}) x 8' to determine how many bits are available. Then, of course, subtract one bit for 'signed' integer types allowing for the 2's complement sign bit.
While such a method may work well integers; it is marginal with non-integer types, such as float/double, etc.
Upvotes: 0