Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - whats the use of unsigned char

 
Thread Tools Search this Thread
Old 11-06-2009, 06:05 AM   #1
Default whats the use of unsigned char



#include <stdio.h>
#include <limits.h>

int main(void)
{

signed char a = 'a';
unsigned char b = 'b';

printf("a = %u\n", sizeof(a));
printf("b = %u\n", sizeof(b));


printf("CHAR_MAX: %d\n", CHAR_MAX);
printf("CHAR_MIN: %d\n", CHAR_MIN);
printf("UCHAR_MAX: %d\n", UCHAR_MAX);
printf("SCHAR_MAX: %d\n", SCHAR_MAX);


printf("LONG_MAX: %ld\n", LONG_MAX);
printf("ULONG_MAX: %lu\n", ULONG_MAX);

return 0;
}

============== OUTPUT ================
[arnuld@dune programs]$ ./a.out
a = 1
b = 1
CHAR_MAX: 127
CHAR_MIN: -128
UCHAR_MAX: 255
SCHAR_MAX: 127
LONG_MAX: 2147483647
ULONG_MAX: 4294967295
[arnuld@dune programs]$


Both signed and unsigned char can hold only 1 byte, then what exactly is
the difference ?

/signed char/ can hold values from -128 to 127 but what is the meaning of
128 or -127 here. In case of /long/ and /unsigned long/ the value
represented is double for the later and that can be stored as is but the
value of CHAR_MAX (255) can not be stored, either it will be '2' or '5',
it can never ever be '-2'. So whats the meaning of -128 to 127 or 0 to
255 ?

ASCII table has values from 0 to 127 (all are single characters, then I
wonder what values you can store before zero and after 127 ?




--
www.lispmachine.wordpress.com
my email is @ the above blog.



arnuld
  Reply With Quote
Old 11-06-2009, 06:43 AM   #2
Seebs
 
Posts: n/a
Default Re: whats the use of unsigned char
> Both signed and unsigned char can hold only 1 byte, then what exactly is
> the difference ?


One is signed, and one is unsigned.

> /signed char/ can hold values from -128 to 127


Yes.

> but what is the meaning of
> 128 or -127 here.


Huh?

-127 is a negative number, of the same magnitude as 127. 128 isn't valid in
signed char on your system.

> In case of /long/ and /unsigned long/ the value
> represented is double for the later and that can be stored as is


I have no idea what you think you are saying.

> but the
> value of CHAR_MAX (255) can not be stored, either it will be '2' or '5',
> it can never ever be '-2'.


I have no idea what you think you're saying here. Seriously, I can't
even begin to make sense of this.

First off, CHAR_MAX is, according to your test, 127 on your system. If you
meant UCHAR_MAX, then... What on earth do you mean about it being '2' or '5'?

> So whats the meaning of -128 to 127 or 0 to
> 255 ?


They're both ranges. One is the range for signed char, one is the range
for unsigned char.

> ASCII table has values from 0 to 127 (all are single characters, then I
> wonder what values you can store before zero and after 127 ?


Depends on your data type. For signed char, you can store -1 through -128.
For unsigned, 128 through 255. (On your system. Others may differ.)

I don't really understand at all what you think is confusing. Does it
help if you notice that 128*2 = 256? It's just like signed long and unsigned
long. One represents all non-negative values, one represents both positive
and negative values.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-06-2009, 07:02 AM   #3
arnuld
 
Posts: n/a
Default Re: whats the use of unsigned char
> On Fri, 06 Nov 2009 06:43:39 +0000, Seebs wrote:

> ..SNIP...


> First off, CHAR_MAX is, according to your test, 127 on your system. If
> you meant UCHAR_MAX, then... What on earth do you mean about it being
> '2' or '5'?


> ...SNIP....


char a = 'A';
char b = -110;
char c = 10;
char d = '10';
unsigned char e = '255';

All are wrong except first one. Let me guess, /char/ is actually an int
and hence /char a/ will actually be stored as number 65, not as 'A'. Buut
ASCII table is limited to the range of values from 0 to 128, so I don't
get what -65 will represent.







--
www.lispmachine.wordpress.com
my email is @ the above blog.



arnuld
  Reply With Quote
Old 11-06-2009, 07:24 AM   #4
Joachim Schmitz
 
Posts: n/a
Default Re: whats the use of unsigned char
arnuld wrote:
>> On Fri, 06 Nov 2009 06:43:39 +0000, Seebs wrote:

>
>> ..SNIP...

>
>> First off, CHAR_MAX is, according to your test, 127 on your system.
>> If you meant UCHAR_MAX, then... What on earth do you mean about it
>> being '2' or '5'?

>
>> ...SNIP....

>
> char a = 'A';
> char b = -110;
> char c = 10;
> char d = '10';
> unsigned char e = '255';
>
> All are wrong except first one. Let me guess, /char/ is actually an
> int and hence /char a/ will actually be stored as number 65, not as
> 'A'. Buut ASCII table is limited to the range of values from 0 to
> 128, so I don't get what -65 will represent.


char is not an int, but an integer, just like int is one too, only (usually)
smaller in size and range

C is not limited to ASCII, there is at least also EBCEDIC. -65 simply
represets -65. +65 represents +65 and if, but only if, interpreted as ASCII,
it also represents 'A'

Bye, Jojo



Joachim Schmitz
  Reply With Quote
Old 11-06-2009, 07:26 AM   #5
Seebs
 
Posts: n/a
Default Re: whats the use of unsigned char
On 2009-11-06, arnuld <> wrote:
>> First off, CHAR_MAX is, according to your test, 127 on your system. If
>> you meant UCHAR_MAX, then... What on earth do you mean about it being
>> '2' or '5'?

>
>> ...SNIP....


> char a = 'A';
> char b = -110;
> char c = 10;
> char d = '10';
> unsigned char e = '255';


This is wrong. '' is for character constants, not for values of characters.

> All are wrong except first one.


No.

If char is signed, b is fine. c is fine regardless. d and e are abusing
a historical feature and don't really make sense.

> Let me guess, /char/ is actually an int


No. It's an integer type, but it's not an int. 'int' is at least 16 bits.

> and hence /char a/ will actually be stored as number 65, not as 'A'.


Right.

> Buut
> ASCII table is limited to the range of values from 0 to 128, so I don't
> get what -65 will represent.


It doesn't have to represent anything. It's just a value that can be stored
in an 8-bit signed type.

While some variety of 'char' is very often used to represent character
values, it doesn't have to be used to represent character values, nor do the
character values it represents have to be ASCII. There are C implementations
which use EBCDIC.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-06-2009, 08:05 AM   #6
Nick Keighley
 
Posts: n/a
Default Re: whats the use of unsigned char
Subject: "whats the use of unsigned char"

My first reaction was unsigned char is one of the more useful types!
I've never found much use for shorts (signed or unsigned) nor signed
char. But an unsigned char will hold a byte and all the bit banging
operators (|&^~>><<) have well defined behaviour. Maybe it's the
application area I'm in (comms) but unsigned chars are an everyday
occurance (sometimes hiding behind Byte or Octet typedefs).


On 6 Nov, 06:05, arnuld <sunr...@invalid.address> wrote:

<snip>

> * signed char a = 'a';
> * unsigned char b = 'b';
> * printf("a = %u\n", sizeof(a));
> * printf("b = %u\n", sizeof(b));
> * printf("CHAR_MAX: %d\n", CHAR_MAX);
> * printf("CHAR_MIN: %d\n", CHAR_MIN);
> * printf("UCHAR_MAX: %d\n", UCHAR_MAX);
> * printf("SCHAR_MAX: %d\n", SCHAR_MAX);
> * printf("LONG_MAX: %ld\n", LONG_MAX);
> * printf("ULONG_MAX: %lu\n", ULONG_MAX);


> a = 1
> b = 1
> CHAR_MAX: 127
> CHAR_MIN: -128
> UCHAR_MAX: 255
> SCHAR_MAX: 127
> LONG_MAX: 2147483647
> ULONG_MAX: 4294967295



> Both signed and unsigned char can hold only 1 byte, then what exactly is
> the difference ?


one is signed. The shift operator is better defined for unsigned char.

> /signed char/ can hold values from -128 to 127


"*your* signed char can hold..."

> but what is the meaning of 128 or -127 here.


que? What is the "meaning" of any number?

zero is the cardinality of the empty set. one is the successor to
zero. And so on. I don't understand what you mean by "meaning".

> In case of /long/ and /unsigned long/ *the value
> represented is double for the later


the largest unsigned long is double the largest long? Doesn't the same
apply to chars (plus or minus 1)

> and that can be stored as is


as what?

> but the
> value of CHAR_MAX (255) can not be stored,


what? CHAR_MAX is the largest char value of course it can be stored in
a char!

> either it will be '2' or '5',


what?

> it can never ever be '-2'.


what?

> So whats the meaning of -128 to 127 or 0 to 255 ?


If you say "what" one more time... You just aren't making any sense.

> ASCII table has values from 0 to 127


yes

> (all are single characters, then I
> wonder what values you can store before zero and after 127 ?


wha....



Nick Keighley
  Reply With Quote
Old 11-06-2009, 08:37 AM   #7
Mark
 
Posts: n/a
Default Re: whats the use of unsigned char
arnuld wrote:
[skip]
> ASCII table has values from 0 to 127 (all are single characters, then
> I wonder what values you can store before zero and after 127 ?


Perhaps this http://c-faq.com/decl/inttypes.html might help you to
understand the differences.

--
Mark



Mark
  Reply With Quote
Old 11-06-2009, 08:49 AM   #8
Mark Bluemel
 
Posts: n/a
Default Re: whats the use of unsigned char
On 6 Nov, 06:05, arnuld <sunr...@invalid.address> wrote:
[Snip]
> Both signed and unsigned char can hold only 1 byte, then what exactly is
> the difference ?
>
> /signed char/ can hold values from -128 to 127 but what is the meaning of
> 128 or -127 here. In case of /long/ and /unsigned long/ *the value
> represented is double for the later and that can be stored as is but the
> value of CHAR_MAX (255) can not be stored, either it will be '2' or '5',
> it can never ever be '-2'. So whats the meaning of -128 to 127 or 0 to
> 255 ?
>
> ASCII table has values from 0 to 127 (all are single characters, then I
> wonder what values you can store before zero and after 127 ?


1) You need to understand that not all character sets are 7-bit ASCII.
Start at http://en.wikipedia.org/wiki/Code_page and follow some links
to get some idea of the complexities involved here.

2) You need to understand that char - signed, unsigned or plain (which
may be either) - is an integer type, not restricted to representing
printable characters. char variables can be a useful holder for
smallish numbers.

3) Unsigned char can also be a useful way of manipulating raw binary
data - see the recent thread about understanding void pointers for
examples.



Mark Bluemel
  Reply With Quote
Old 11-06-2009, 10:58 AM   #9
bartc
 
Posts: n/a
Default Re: whats the use of unsigned char

"arnuld" <> wrote in message
news...

> Both signed and unsigned char can hold only 1 byte, then what exactly is
> the difference ?
>
> /signed char/ can hold values from -128 to 127 but what is the meaning of
> 128 or -127 here. In case of /long/ and /unsigned long/ the value
> represented is double for the later and that can be stored as is but the
> value of CHAR_MAX (255) can not be stored, either it will be '2' or '5',
> it can never ever be '-2'. So whats the meaning of -128 to 127 or 0 to
> 255 ?
>
> ASCII table has values from 0 to 127 (all are single characters, then I
> wonder what values you can store before zero and after 127 ?


(This was discussed on c.l.c a few weeks back (Rahul: 'can a character be
negative', 1-10-09), you might want to look at that.)

My view was, forget the 'char' part, think of it as signed and unsigned
byte. Bytes happen to be used for char data too, then unsigned byte makes
more sense. But some on this group will take the opposite view.

--
Bartc



bartc
  Reply With Quote
Old 11-06-2009, 02:58 PM   #10
Tinkertim
 
Posts: n/a
Default Re: whats the use of unsigned char
On Nov 6, 2:05*pm, arnuld <sunr...@invalid.address> wrote:
> #include <stdio.h>
> #include <limits.h>
>
> int main(void)
> {
>
> * signed char a = 'a';
> * unsigned char b = 'b';
>
> * printf("a = %u\n", sizeof(a));
> * printf("b = %u\n", sizeof(b));
>
> * printf("CHAR_MAX: %d\n", CHAR_MAX);
> * printf("CHAR_MIN: %d\n", CHAR_MIN);
> * printf("UCHAR_MAX: %d\n", UCHAR_MAX);
> * printf("SCHAR_MAX: %d\n", SCHAR_MAX);
>
> * printf("LONG_MAX: %ld\n", LONG_MAX);
> * printf("ULONG_MAX: %lu\n", ULONG_MAX);
>
> * return 0;
>
> }
>
> ============== OUTPUT ================
> [arnuld@dune programs]$ ./a.out
> a = 1
> b = 1
> CHAR_MAX: 127
> CHAR_MIN: -128
> UCHAR_MAX: 255
> SCHAR_MAX: 127
> LONG_MAX: 2147483647
> ULONG_MAX: 4294967295
> [arnuld@dune programs]$
>
> Both signed and unsigned char can hold only 1 byte, then what exactly is
> the difference ?
>
> /signed char/ can hold values from -128 to 127 but what is the meaning of
> 128 or -127 here. In case of /long/ and /unsigned long/ *the value
> represented is double for the later and that can be stored as is but the
> value of CHAR_MAX (255) can not be stored, either it will be '2' or '5',
> it can never ever be '-2'. So whats the meaning of -128 to 127 or 0 to
> 255 ?
>
> ASCII table has values from 0 to 127 (all are single characters, then I
> wonder what values you can store before zero and after 127 ?
>
> --www.lispmachine.wordpress.com
> my email is @ the above blog.


Saying "The world will just use ascii" is a lot like saying "640K
should be enough for anyone"

Both are incorrect.

Cheers,
--Tim


Tinkertim
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
unsigned code =?Utf-8?B?S2VudA==?= Windows 64bit 2 09-26-2007 03:24 PM
vista 64 - Unsigned drivers BENAGLIA Windows 64bit 8 09-10-2007 05:44 PM
signed & unsigned drivers? Kue2 Windows 64bit 2 09-01-2007 01:05 AM
How do You stop Vista from Blocking unsigned drivers Kue2 Windows 64bit 16 01-30-2007 08:20 AM
Windows Vista Unsigned Drivers mcbacon@gmail.com Windows 64bit 1 12-11-2006 11:50 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46