I
Ioannis Vranos
Ioannis said:OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?
With "value ranges" above I mean the total values of each type.
Ioannis said:OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?
Displaying the limits using the macros is easy: the C89 Standard mandates
that the macros in <limits.h> must have the same type as an expression
that is an object of the corresponding type converted according to the
integral promotions (this excludes CHAR_BIT and MB_LEN_MAX). So this is
perfectly reasonable:
printf("char signed min = %d\n", SCHAR_MIN);
printf("signed int min = %d\n", INT_MIN);
printf("signed int max = %d\n", INT_MAX);
/* etc. */
For calculating them, Tondo & Gimpel suggest this in "The C Answer Book":
printf("signed char min = %d\n", ~(char)((unsigned char) ~0 >> 1));
printf("signed char max = %d\n", (char)((unsigned char) ~0 >> 1));
printf("unsigned short min = %d\n", ~(short)((unsigned short) ~0 >> 1));
printf("unsigned short max = %d\n", (short)((unsigned short) ~0 >> 1));
and so on.
Obviously, I don't know to what peer review T&G's Answer Book was subject,
but if their solutions are not correct, I would like to know.
user923005 said:It works for two's complement. Maybe that's just the trick:
You have to write a different solution for each possible number
format.
Harald said:Yes, it can.
Of course. And any of the permissible implementation-defined
values of i will result in the intended behaviour.
Ioannis said:.... snip ...
OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?
OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?
The question doesn't mention the word "portable" at all anywhere, so I
guess that a machine specific answer will count as a solution too.
I think you have a misinterpretation of 'implementaion-defined'. This
means that the action, on a particular implementation, is defined.
However that may well be "*** ABORT in line nnn, integer overflow ***".
user923005 said:
Maybe that's just the trick:
You have to write a different solution for each possible number
format.
Are you sure? What happens when both signed and unsigned short (or
indeed, int in the "and so on" case) have the same number of value
bits? I think this is permitted and causes the wrong answer to be
printed, but my brain is already hurting from thinking about this
exercise.
I hoped I could find a portable solution that way, but I drew a blank.
Hi santoshHello all,
In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.
Maybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel
And,there is a way about signed char on that book
printf("signed char min = %d\n", -(char)((unsigned char) ~0 >> 1));
but i think it should be that
printf("signed char min = %d\n", -1 -(char)((unsigned char) ~0 >> 1));
maybe it's a little mistake of press![]()
I think you've misread the symbol ~ as -. In another message in this
thread I posted this, an extract of T&G's solution:
printf("signed char min = %d\n", ~(char)((unsigned char) ~0 >> 1));
printf("signed char max = %d\n", (char)((unsigned char) ~0 >> 1));
printf("unsigned short min = %d\n", ~(short)((unsigned short) ~0 >> 1));
printf("unsigned short max = %d\n", (short)((unsigned short) ~0 >> 1));
yalong said:Hi santosh
Maybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel
And,there is a way about signed char on that book
printf("signed char min = %d\n",
-(char)((unsigned char) ~0 >> 1));
but i think it should be that
printf("signed char min = %d\n",
-1 -(char)((unsigned char) ~0 >> 1));
maybe it's a little mistake of press![]()
Hi santoshMaybe you could take this book for reference:
the c answer book 2Edtion by Clovis L. Tondo & Scott E. Gimpel
Or maybe you shouldn't.
And,there is a way about signed char on that book
printf("signed char min = %d\n",
-(char)((unsigned char) ~0 >> 1));
~0 may be a trap representation on sign-magnitude machines.
The (unsigned char) ~0 is much better stated as (unsigned
char) -1, which yields UCHAR_MAX portably. But the single
right shifting of that need not yield SCHAR_MAX.[*] The
further negation of that need not yield SCHAR_MIN.
[*] The standard doesn't preclude UCHAR_MAX == 65535 and
SCHAR_MAX == 127.
but i think it should be that
printf("signed char min = %d\n",
-1 -(char)((unsigned char) ~0 >> 1));
Excellent. All the flaws above and one more.
maybe it's a little mistake of press![]()
The cast conversion to _plain_ char is certainly a mistake,
even if inconsequencial in context. But I doubt it's a
printing error.
This seems rather simple to me, but perhaps it is just my
inexperience. Here is a solution that works on all of the platforms
I've worked with, but I haven't worked with ALL computers:
unsigned long long ULL_MAX = (unsigned long long)(-1LL);
unsigned long long ULL_MIN = 0ULL; //automatically known because the
smallest unsigned value is 0
signed long long SLL_MAX = (signed long long)(ULL_MAX >> 1);
signed long long SLL_MIN = SLL_MAX + 1;
CBFalconer said:I think you have a misinterpretation of 'implementaion-defined'.
This means that the action, on a particular implementation, is
defined. However that may well be "*** ABORT in line nnn, integer
overflow ***".
CBFalconer said:Yes. Consider a 16 bit system. INT_MAX is 32767. INT_MIN is
-32768 (or -32767). UINT_MAX is 65536 (UINT_MIN is naturally 0).
It is allowable to reserve the value that would represent -32768 to
indicate a special condition, such as uninitialized. Or, for a 1's
complement machine, the bit pattern for -0 is forbidden.
Peter Nilsson said:
Did you mean ones' complement?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.