Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > signed char and unsigned char difference

Reply
Thread Tools

signed char and unsigned char difference

 
 
dam_fool_2003@yahoo.com
Guest
Posts: n/a
 
      07-25-2004
For int data type the default range starts from signed to unsigned. If
we don't want negative value we can force an unsigned value. The same
goes for long also.
But I don't understand why we have signed char which is -256. Does it
means that we can assign the same ASCII value to both signed and
unsigned. That means the ASCII value can be represented with a type of
signed char and also unsigned char?
For example
int main(void)
{
signed char a= 'a';
unsigned char b = 'b';
printf("%i %c",a,b);
return 0;
}

The above code does not warn about the assignment. I went through the
faq ,
Section 8 but I don't find the answer. Can any one give any pointer
regarding
the above subject?
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      07-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> For int data type the default range starts from signed to unsigned. If
> we don't want negative value we can force an unsigned value. The same
> goes for long also.


Sorry, but these sentences don't make sense to me. For signed ints
the data range is INT_MIN to INT_MAX, for unsigned ints it's 0 to
UINT_MAX. INT_MIN must be at least -32767, INT_MAX +32767 and
UINT_MAX 65535. For long there are similar minimum ranges, with
"INT" replaced by "LONG" (i.e. LONG_MAX instead of INT_MAX) with
minimum requirements being LONG_MIN == -2^31-1, LONG_MAX == 2^31-1
and ULONG_MAX == 2^32-1. Implementations are allowed to support
larger ranges. The actual values can be found in <limits.h>.

> But I don't understand why we have signed char which is -256.


The range for signed chars is SCHAR_MIN to SCHAR_MAX. Quite often
(on machines with 8 bits in a char and 2's complement) this is the
range between -128 and +127. The range for unsigned char is 0 to
UCHAR_MAX (quite often this is 0 to 255). The ranges of -127 to
127 for signed and 0 to 255 for unsigned chars are the minimum
requirements, so you can be sure you can store numbers from these
ranges wherever you have a standard compliant C compiler. While
there probably are some machines where you also could store -256
in a signed char you shouldn't rely on this, on many machines it
won't work.

> Does it
> means that we can assign the same ASCII value to both signed and
> unsigned. That means the ASCII value can be represented with a type of
> signed char and also unsigned char?


Yes, since ASCII characters are all in the range between 0 and 127,
thus they can always be stored in a signed as well as an unsigned
char.

> For example
> int main(void)
> {
> signed char a= 'a';
> unsigned char b = 'b';


There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      07-25-2004

<(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) wrote:


> > Does it
> > means that we can assign the same ASCII value to both signed and
> > unsigned. That means the ASCII value can be represented with a type of
> > signed char and also unsigned char?

>
> Yes, since ASCII characters are all in the range between 0 and 127,
> thus they can always be stored in a signed as well as an unsigned
> char.
>
> > For example
> > int main(void)
> > {
> > signed char a= 'a';
> > unsigned char b = 'b';

>
> There's nothing the compiler should complain about as long as you're
> using ASCII (it's different with EBCDIC because there 'a' is 129 and
> also most of the other letters are above 127, so you would better use
> unsigned char).

It's been a while, but I once used a PRIMOS system where the default ASCII
representation had the high bit set.


 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      07-25-2004
On Sun, 25 Jul 2004 (E-Mail Removed) wrote:

> For int data type the default range starts from signed to unsigned. If
> we don't want negative value we can force an unsigned value. The same
> goes for long also.


First sentence doesn't make sense to me. The rest seems obviously true.

> But I don't understand why we have signed char which is -256. Does it
> means that we can assign the same ASCII value to both signed and
> unsigned. That means the ASCII value can be represented with a type of
> signed char and also unsigned char?


The first sentence of this paragraph makes no sense to me. There are
systems where a char is 16 bits. For these systems you can have a signed
char with a value of -256. Maybe the confusion is that char is not used to
hold characters. It can be used for that purpose but it can also be used
as an integer data type with a very small range of values. If you need to
save space and you never need anything outside the range of a char, then
use a char.

As to your question, the ASCII character set is in the range 0 to 127. A
signed char is typically in the range -128 to 127. An unsigned char is
typically in the range 0 to 255. The ASCII character set will fit in both
ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
than 8 the range could be even larger, so it would still hold true.

> For example
> int main(void)
> {
> signed char a= 'a';
> unsigned char b = 'b';
> printf("%i %c",a,b);
> return 0;
> }


If you give printf a %i it is expecting an int. You are passing it a
signed char. This will have undefind behaviour. Did you mean to use:

printf("%c %c\n", a, b);

> The above code does not warn about the assignment. I went through the
> faq , Section 8 but I don't find the answer. Can any one give any
> pointer regarding the above subject?


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to (E-Mail Removed)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-25-2004
(E-Mail Removed)-berlin.de writes:
[...]
> There's nothing the compiler should complain about as long as you're
> using ASCII (it's different with EBCDIC because there 'a' is 129 and
> also most of the other letters are above 127, so you would better use
> unsigned char).


On any system that uses EBCDIC as the default encoding, plain char
will almost certainly be unsigned.

To oversimplify slightly:

Use plain char to hold characters (the implementation will have chosen
an appropriate representation). Use unsigned char to hold bytes. Use
signed char to hold very small numeric values. (I actually haven't
seen much use for explicitly signed char.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-25-2004
On Sun, 25 Jul 2004 16:06:37 GMT, "Tim Prince"
<(E-Mail Removed)> wrote in comp.lang.c:

>
> <(E-Mail Removed)-berlin.de> wrote in message
> news:(E-Mail Removed)...
> > (E-Mail Removed) wrote:

>
> > > Does it
> > > means that we can assign the same ASCII value to both signed and
> > > unsigned. That means the ASCII value can be represented with a type of
> > > signed char and also unsigned char?

> >
> > Yes, since ASCII characters are all in the range between 0 and 127,
> > thus they can always be stored in a signed as well as an unsigned
> > char.
> >
> > > For example
> > > int main(void)
> > > {
> > > signed char a= 'a';
> > > unsigned char b = 'b';

> >
> > There's nothing the compiler should complain about as long as you're
> > using ASCII (it's different with EBCDIC because there 'a' is 129 and
> > also most of the other letters are above 127, so you would better use
> > unsigned char).

> It's been a while, but I once used a PRIMOS system where the default ASCII
> representation had the high bit set.


Then it wasn't ASCII.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-25-2004
On 25 Jul 2004 19:31:17 GMT, (E-Mail Removed)
(Darrell Grainger) wrote in comp.lang.c:

> On Sun, 25 Jul 2004 (E-Mail Removed) wrote:
>
> > For int data type the default range starts from signed to unsigned. If
> > we don't want negative value we can force an unsigned value. The same
> > goes for long also.

>
> First sentence doesn't make sense to me. The rest seems obviously true.
>
> > But I don't understand why we have signed char which is -256. Does it
> > means that we can assign the same ASCII value to both signed and
> > unsigned. That means the ASCII value can be represented with a type of
> > signed char and also unsigned char?

>
> The first sentence of this paragraph makes no sense to me. There are
> systems where a char is 16 bits. For these systems you can have a signed
> char with a value of -256. Maybe the confusion is that char is not used to
> hold characters. It can be used for that purpose but it can also be used
> as an integer data type with a very small range of values. If you need to
> save space and you never need anything outside the range of a char, then
> use a char.
>
> As to your question, the ASCII character set is in the range 0 to 127. A
> signed char is typically in the range -128 to 127. An unsigned char is
> typically in the range 0 to 255. The ASCII character set will fit in both
> ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
> than 8 the range could be even larger, so it would still hold true.
>
> > For example
> > int main(void)
> > {
> > signed char a= 'a';
> > unsigned char b = 'b';
> > printf("%i %c",a,b);
> > return 0;
> > }

>
> If you give printf a %i it is expecting an int. You are passing it a
> signed char. This will have undefind behaviour. Did you mean to use:


No, he is not. One can't pass any type of char as argument to a
variadic function beyond the specified ones. The char 'a' will be
promoted to int and the behavior is perfectly defined.

Technically, passing unsigned char 'b' to printf() with a "%c"
conversion specifier could be undefined because:

1. The implementation might have UCHAR_MAX > INT_MAX (in other words,
UCHAR_MAX == UINT_MAX) and so 'b' will be converted to unsigned,
rather than signed, int.

2. The standard suggests, but does not require, that the signed and
unsigned integer types be interchangeable as function argument and
return types.

So this just could be undefined on a platform where the character
types have the same number of bits as int (there are some, believe me)
and unsigned ints are passed to variadic functions differently than
signed ints are.

I would not hold my breath waiting for such an implementation to
appear. From a QOI point of view it would be horrible.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Giorgos Keramidas
Guest
Posts: n/a
 
      07-25-2004
(E-Mail Removed) writes:
> For int data type the default range starts from signed to unsigned. If
> we don't want negative value we can force an unsigned value. The same
> goes for long also.


True.

> But I don't understand why we have signed char which is -256.


We don't.

The smallest value that fits in 8-bits (which is the minimal size a
signed char can hold, IIRC) is not -256 but -128. But your programs
shouldn't depend on that. Use SCHAR_MIN instead of an inline "magic"
value and you'll be fine

> Does it means that we can assign the same ASCII value to both signed
> and unsigned.


An unsigned char can hold values up to UCHAR_MAX. I'm not sure if
converting this value to signed char and back to unsigned will always
work as expected.

> That means the ASCII value can be represented with a type of signed
> char and also unsigned char?


No. SCHAR_MAX is usually 127 (if char values have 8-bits), which is
smaller than some of the values that unsigned characters can store.

> For example
>
> int main(void)
> {
> signed char a= 'a';
> unsigned char b = 'b';
> printf("%i %c",a,b);
> return 0;
> }
>
> The above code does not warn about the assignment.


It depends on the warnings you have enabled. Here it doesn't even build
because printf() is called before a prototype is visible:

foo.c:5: warning: implicit declaration of function `printf'

Giorgos

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      07-25-2004
(E-Mail Removed)-berlin.de wrote:

> > For example
> > int main(void)
> > {
> > signed char a= 'a';
> > unsigned char b = 'b';

>
> There's nothing the compiler should complain about as long as you're
> using ASCII (it's different with EBCDIC because there 'a' is 129 and
> also most of the other letters are above 127, so you would better use
> unsigned char).


The type 'char' has to be able to represent all members of the basic
character set, which includes 'a'. If the machine had 8-bit chars and
'a' == 129, then it must have 'char' being unsigned.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      07-26-2004
"Jack Klein" <(E-Mail Removed)> wrote...
> (Darrell Grainger) wrote in comp.lang.c:
> > On Sun, 25 Jul 2004 (E-Mail Removed) wrote:
> > > For example
> > > int main(void)
> > > {
> > > signed char a= 'a';


Unless plain char is signed, there's no requirement that the value of 'a'
fit within the range of signed char.

> > > unsigned char b = 'b';
> > > printf("%i %c",a,b);
> > > return 0;
> > > }

> >
> > If you give printf a %i it is expecting an int. You are passing it a
> > signed char. This will have undefind behaviour. Did you mean to use:

>
> No, he is not. One can't pass any type of char as argument to a
> variadic function beyond the specified ones. The char 'a' will be
> promoted to int


This is ambiguous since a literal 'a' is already of int and no promotion
would be required. Of course, Jack is talking about the promotion of signed
and unsigned chars a and b respectively when used as arguments to printf.

> and the behavior is perfectly defined.


--
Peter


 
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
char vs. signed or unsigned char At_sea_with_C C Programming 4 03-14-2007 08:28 PM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM
What is the difference between signed and unsigned char? tinesan@gmail.com C Programming 10 02-04-2005 09:53 AM



Advertisments