Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   signed char and unsigned char difference (http://www.velocityreviews.com/forums/t434655-signed-char-and-unsigned-char-difference.html)

 dam_fool_2003@yahoo.com 07-25-2004 02:44 PM

signed char and unsigned char difference

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?

 Jens.Toerring@physik.fu-berlin.de 07-25-2004 03:39 PM

Re: signed char and unsigned char difference

dam_fool_2003@yahoo.com 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 ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de

 Tim Prince 07-25-2004 04:06 PM

Re: signed char and unsigned char difference

<Jens.Toerring@physik.fu-berlin.de> wrote in message
news:2mi2i6Fn791cU1@uni-berlin.de...
> dam_fool_2003@yahoo.com 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.

 Darrell Grainger 07-25-2004 07:31 PM

Re: signed char and unsigned char difference

On Sun, 25 Jul 2004 dam_fool_2003@yahoo.com 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 vice.president@whitehouse.gov

 Keith Thompson 07-25-2004 09:24 PM

Re: signed char and unsigned char difference

Jens.Toerring@physik.fu-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) kst-u@mib.org <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.

 Jack Klein 07-25-2004 10:28 PM

Re: signed char and unsigned char difference

On Sun, 25 Jul 2004 16:06:37 GMT, "Tim Prince"
<tprince@nospamcomputer.org> wrote in comp.lang.c:

>
> <Jens.Toerring@physik.fu-berlin.de> wrote in message
> news:2mi2i6Fn791cU1@uni-berlin.de...
> > dam_fool_2003@yahoo.com 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

 Jack Klein 07-25-2004 10:35 PM

Re: signed char and unsigned char difference

On 25 Jul 2004 19:31:17 GMT, darrell@NOMORESPAMcs.utoronto.ca.com
(Darrell Grainger) wrote in comp.lang.c:

> On Sun, 25 Jul 2004 dam_fool_2003@yahoo.com 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

 Giorgos Keramidas 07-25-2004 11:12 PM

Re: signed char and unsigned char difference

dam_fool_2003@yahoo.com 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

 Old Wolf 07-25-2004 11:54 PM

Re: signed char and unsigned char difference

Jens.Toerring@physik.fu-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.

 Peter Nilsson 07-26-2004 01:59 AM

Re: signed char and unsigned char difference

"Jack Klein" <jackklein@spamcop.net> wrote...
> (Darrell Grainger) wrote in comp.lang.c:
> > On Sun, 25 Jul 2004 dam_fool_2003@yahoo.com 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

 All times are GMT. The time now is 09:25 AM.