Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > signed char range how...??

Reply
Thread Tools

signed char range how...??

 
 
naunetr
Guest
Posts: n/a
 
      11-19-2007
hi all,

i'm reading the "tomsweb" tutorial here
http://cprog.tomsweb.net/cintro.html and it says that range of signed
char is -127...+127. but this prog prints -128...127. also my textbook
says -128...127 too. what is correct? thanks.

#include <stdio.h>
main()
{
signed char SignedCharVar;
SignedCharVar = 0;

while(1)
{
printf("%d, ", SignedCharVar);
SignedCharVar = SignedCharVar+1;
}
}
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      11-19-2007
naunetr <(E-Mail Removed)> wrote:
> hi all,
>
> i'm reading the "tomsweb" tutorial here
> http://cprog.tomsweb.net/cintro.html and it says that range
> of signed char is -127...+127.


"C supports the following types: (note that the ranges
indicated are minimum ranges, they may (and will!) be
larger so you shouldn't rely on them having a certain
size)

"Integer types: (non-fractional numbers)

"signed char minimum range: -127..+127"

> but this prog prints -128...127.


Your program is flawed. Just do

printf("%d..%d\n", SCHAR_MIN, SCHAR_MAX);

> also my textbook says -128...127 too.


That is probably based on the assumption of a vanilla two's
complement 8-bit char machine.

> what is correct?


The standard. Tom's discussion of limits.h is just missing
the preamble given earlier.

--
Peter
 
Reply With Quote
 
 
 
 
J. J. Farrell
Guest
Posts: n/a
 
      11-19-2007
naunetr wrote:
>
> i'm reading the "tomsweb" tutorial here
> http://cprog.tomsweb.net/cintro.html and it says that range of signed
> char is -127...+127. but this prog prints -128...127. also my textbook
> says -128...127 too. what is correct? thanks.
>
> #include <stdio.h>
> main()
> {
> signed char SignedCharVar;
> SignedCharVar = 0;
>
> while(1)
> {
> printf("%d, ", SignedCharVar);
> SignedCharVar = SignedCharVar+1;
> }
> }


A signed char must be able to hold values in the range -127 to 127. The
actual range varies from compiler to compiler, and can be larger than
this by any amount.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-19-2007
In article <fhqsa6$9j9$(E-Mail Removed)>, naunetr
<(E-Mail Removed)> wrote on Monday 19 Nov 2007 7:57 am:

> hi all,
>
> i'm reading the "tomsweb" tutorial here
> http://cprog.tomsweb.net/cintro.html and it says that range of signed
> char is -127...+127. but this prog prints -128...127. also my textbook
> says -128...127 too. what is correct? thanks.


The Standard has to take into account non-twos-complement architectures
too. Your book probably assumes a twos-complement representation.

> #include <stdio.h>
> main()


Implicit return of int is disallowed. Have main return an int and
declare appropriately.

> {
> signed char SignedCharVar;
> SignedCharVar = 0;
>
> while(1)
> {
> printf("%d, ", SignedCharVar);
> SignedCharVar = SignedCharVar+1;


In Standard C overflow invokes undefined behaviour, so any result is
possible for your program. Just use the macros defined in limits.h.
That's what they are for.

> }
> }


 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-19-2007
santosh wrote:
> In article <fhqsa6$9j9$(E-Mail Removed)>, naunetr
> <(E-Mail Removed)> wrote on Monday 19 Nov 2007 7:57 am:
>
>> hi all,
>>
>> i'm reading the "tomsweb" tutorial here
>> http://cprog.tomsweb.net/cintro.html and it says that range of signed
>> char is -127...+127. but this prog prints -128...127. also my textbook
>> says -128...127 too. what is correct? thanks.

>
> The Standard has to take into account non-twos-complement architectures
> too. Your book probably assumes a twos-complement representation.


Even twos-complement is allowed to use all-bits-one as a trap
representation, and thus have range -127..127.

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-19-2007
Philip Potter wrote:
....
> Even twos-complement is allowed to use all-bits-one as a trap
> representation, and thus have range -127..127.


That is debatable, and has been debated on comp.std.c. Not that I
disagree; I was arguing your side in that debate. However, the other
side interpreted the wording of the standard as allowing the
representation that would otherwise represent negative zero to be a trap
representation, but permitting no other trap representations.

I was arguing that it is permitted for an implementation to declare a 32
bit int type to have INT_MIN = -32768 and INT_MAX=2147483647, by
treating all bit patterns that would otherwise represent values from
-2147483648 to -32769 to be trap representations. This would break a lot
of existing code, and I doubt that this was the committee's intent, but
I don't see it as violating any actual requirements in the standard.
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-19-2007
James Kuyper wrote:
> Philip Potter wrote:
> ...
>> Even twos-complement is allowed to use all-bits-one as a trap
>> representation, and thus have range -127..127.

>
> That is debatable, and has been debated on comp.std.c. Not that I
> disagree; I was arguing your side in that debate. However, the other
> side interpreted the wording of the standard as allowing the
> representation that would otherwise represent negative zero to be a trap
> representation, but permitting no other trap representations.
>
> I was arguing that it is permitted for an implementation to declare a 32
> bit int type to have INT_MIN = -32768 and INT_MAX=2147483647, by
> treating all bit patterns that would otherwise represent values from
> -2147483648 to -32769 to be trap representations. This would break a lot
> of existing code, and I doubt that this was the committee's intent, but
> I don't see it as violating any actual requirements in the standard.


I can't see how there could be any debate, at least as far as n1256 is
concerned. From 6.2.6.2p2:

"Which of these [sign-magnitude, 1's-comp or 2's-comp] applies is
implementation-defined, as is whether the value with sign bit 1 and all
value bits zero (for the first two), or with sign bit and all value bits
1 (for ones’ complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero."

"If the sign bit is zero, it shall not affect the resulting value. If
the sign bit is one, the value shall be modified in one of the following
ways:

— the corresponding value with sign bit 0 is negated (sign and magnitude);
— the sign bit has the value -(2N) (two’s complement);
— the sign bit has the value -(2N - 1) (ones’ complement).

Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first two), or
with sign bit and all value bits 1 (for ones’ complement), is a trap
representation or a normal value. In the case of sign and magnitude and
ones’ complement, if this representation is a normal value it is called
a negative zero."

It seems quite clear that binary 10000000 is a possible trap
representation for signed char under twos complement.

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-19-2007
Philip Potter wrote:
> James Kuyper wrote:
>> Philip Potter wrote:
>> ...
>>> Even twos-complement is allowed to use all-bits-one as a trap
>>> representation, and thus have range -127..127.

>>
>> That is debatable, and has been debated on comp.std.c. Not that I
>> disagree; I was arguing your side in that debate. However, the other
>> side interpreted the wording of the standard as allowing the
>> representation that would otherwise represent negative zero to be a
>> trap representation, but permitting no other trap representations.
>>
>> I was arguing that it is permitted for an implementation to declare a
>> 32 bit int type to have INT_MIN = -32768 and INT_MAX=2147483647, by
>> treating all bit patterns that would otherwise represent values from
>> -2147483648 to -32769 to be trap representations. This would break a
>> lot of existing code, and I doubt that this was the committee's
>> intent, but I don't see it as violating any actual requirements in the
>> standard.

>
> I can't see how there could be any debate, at least as far as n1256 is
> concerned. From 6.2.6.2p2:
>
> "

....
> Which of these applies is implementation-defined, as is whether the
> value with sign bit 1 and all value bits zero (for the first two), or
> with sign bit and all value bits 1 (for ones’ complement), is a trap
> representation or a normal value. In the case of sign and magnitude and
> ones’ complement, if this representation is a normal value it is called
> a negative zero."
>
> It seems quite clear that binary 10000000 is a possible trap
> representation for signed char under twos complement.


You're right; I incorrectly saw your statement as an extension of the
issue I was talking about. I forgot that your particular case was
covered explicitly, which makes it unrelated to my issue.
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-19-2007
Philip Potter wrote:
> santosh wrote:
>> In article <fhqsa6$9j9$(E-Mail Removed)>, naunetr
>> <(E-Mail Removed)> wrote on Monday 19 Nov 2007 7:57 am:
>>
>>> hi all,
>>>
>>> i'm reading the "tomsweb" tutorial here
>>> http://cprog.tomsweb.net/cintro.html and it says that range of signed
>>> char is -127...+127. but this prog prints -128...127. also my textbook
>>> says -128...127 too. what is correct? thanks.

>>
>> The Standard has to take into account non-twos-complement architectures
>> too. Your book probably assumes a twos-complement representation.

>
> Even twos-complement is allowed to use all-bits-one as a trap
> representation, and thus have range -127..127.


Sorry, I meant sign-bit-one, value-bits-zero.

Phil
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-19-2007
Peter Nilsson wrote, On 19/11/07 02:46:
> naunetr <(E-Mail Removed)> wrote:
>> hi all,
>>
>> i'm reading the "tomsweb" tutorial here
>> http://cprog.tomsweb.net/cintro.html and it says that range
>> of signed char is -127...+127.

>
> "C supports the following types: (note that the ranges
> indicated are minimum ranges, they may (and will!) be
> larger so you shouldn't rely on them having a certain
> size)


<snip>

>> what is correct?

>
> The standard. Tom's discussion of limits.h is just missing
> the preamble given earlier.


No, Tom's discussion is not missing the preamble, it is just the OP
missed it. Specifically it says:
| C supports the following types: (note that the ranges indicated are
| minimum ranges, they may (and will!) be larger so you shouldn't rely
| on them having a certain size)
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments