whats the use of unsigned char

A

arnuld

#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 ?
 
S

Seebs

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
 
A

arnuld

..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.
 
J

Joachim Schmitz

arnuld said:
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
 
S

Seebs

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
 
N

Nick Keighley

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).



  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....
 
M

Mark Bluemel

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.
 
B

bartc

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.
 
T

Tinkertim

#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
 
K

Keith Thompson

Seebs said:
char a = 'A'; [...]
and hence /char a/ will actually be stored as number 65, not as 'A'.

Right.

Only half right.

Assuming an ASCII-based system (that includes any of the numerous
character sets derived from ASCII), the value stored in a can be
written either as 65 or as 'A'. Both expressions have the same value
and type (yes, character constants in C are of type int, for
historical reasons).

The value stored in a is a number, more specifically an integer. That
value is written as 65. It can also be written as 'A'.

The value stored in any char object is a small integer. Such a value
is often *used* to represent a character value, but it needn't be.
 
S

Seebs

Only half right.

Yeah. I was trying to agree that so far as C is concerned, it's just a 65,
it doesn't matter whether or not that happens to match a particular letter.

The OP's confusion was apparently contagious, though.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top