Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > char vs. signed or unsigned char

Reply
Thread Tools

char vs. signed or unsigned char

 
 
At_sea_with_C
Guest
Posts: n/a
 
      03-14-2007
hello all,

Is there a reason to prefer char over signed or unsigned char. From waht I
know all strings can be coded with unsigned char while a negative number
can be put in signed char. So what are the uses for plain char?

Thanks to all who ansered my last post (enum).

--
Email: The handle, (dot seperated), at gmail dot com.
 
Reply With Quote
 
 
 
 
Florian Weingarten
Guest
Posts: n/a
 
      03-14-2007
At_sea_with_C <(E-Mail Removed)> wrote:
> Is there a reason to prefer char over signed or unsigned char. From waht I
> know all strings can be coded with unsigned char while a negative number
> can be put in signed char. So what are the uses for plain char?


As far as I know, there is no "plain char". A char is either a unsigned
char or a signed char, which exactly is implementation depenedent:

ISO/IEC 9899:1999, 6.2.5.15 (p. 49)
The three types char, signed char, and unsigned char are collectively called
the character types. The implementation shall define char to have the same range,
representation, and behavior as either signed char or unsigned char.


HTH,
Flo
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      03-14-2007
Florian Weingarten wrote:

> At_sea_with_C <(E-Mail Removed)> wrote:
>> Is there a reason to prefer char over signed or unsigned char. From waht I
>> know all strings can be coded with unsigned char while a negative number
>> can be put in signed char. So what are the uses for plain char?

>
> As far as I know, there is no "plain char".


`char` is plain.

> A char is either a unsigned char or a signed char, which exactly
> is implementation depenedent:
>
> ISO/IEC 9899:1999, 6.2.5.15 (p. 49)
> The three types char, signed char, and unsigned char are collectively called
> the character types. The implementation shall define char to have the same range,
> representation, and behavior as either signed char or unsigned char.


No: it has the same range, representation, and behaviour as one of
those types, but it's a different type.

I'd expect the implementation to pick whichever one was "more
efficient" by some reasonable measure - eg when converting to
`int`, one probably wants an efficient code sequence, so one
picks whatever the machine conversion is (PDP11 MOVB, for example)
and takes the consequences.

--
Chris "electric hedgehog" Dollin
"We live for the One, you die for the One." Unsaid /Babylon 5/.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      03-14-2007
At_sea_with_C wrote:

> Is there a reason to prefer char over signed or unsigned char. From waht I
> know all strings can be coded with unsigned char while a negative number
> can be put in signed char. So what are the uses for plain char?


use char for character data (supported by the implementation)

use unsigned char for raw data, eg. data transmitted over a comms link

I've never used signed char, I suppose you'd use it for small signed
integers


--
Nick Keighley

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      03-14-2007
"Nick Keighley" <(E-Mail Removed)> wrote in message
>
> I've never used signed char, I suppose you'd use it for small signed
> integers
>

One use is for normals in runtime graphics.
Three numbers in the range -128 to 127 give you enough resolution to do
visually acceptable lighting, but don't take up too much space, and you can
use fast integer arithmetic.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm




 
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
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
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
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM
signed char and unsigned char difference dam_fool_2003@yahoo.com C Programming 9 07-26-2004 01:59 AM



Advertisments