![]() |
|
|
|||||||
![]() |
C Programming - whats the use of unsigned char |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
#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 |
|
|
|
|
#2 |
|
Posts: n/a
|
> 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 |
|
|
|
#3 |
|
Posts: n/a
|
> 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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
"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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |