473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

char vs. unsigned char

Can anybody illustrate the usefulness of having char and unsigned char? I
mean, under what circumstances would one want to use unsigned char (or
unsigned char *) rather than char (or char *, respectively)?
Mar 29 '06 #1
6 45722
Steven Jones <sj****@sjones.org> writes:
Can anybody illustrate the usefulness of having char and
unsigned char? I mean, under what circumstances would one want to
use unsigned char (or unsigned char *) rather than char (or char *,
respectively)?


Use plain char (which may be either signed or unsigned) when you want
to represent characters, or when you only care about representing
integer values in the range 0 to 127.

Use signed char when you want a signed integer type that covers values
from -127 to +127 (or more; that's the minimum guaranteed range).

Use unsigned char when you want an unsigned integer type that covers
values from 0 to 255 (or more; that's the minimum guaranteed range),
or when you want to examine the representation of any object. (The
standard guarantees that any object of any type can be treated as an
array of unsigned char.)

--
Keith Thompson (The_Other_Keith) ks***@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.
Mar 29 '06 #2

"Steven Jones" <sj****@sjones.org> wrote in message
news:pa****************************@sjones.org...
Can anybody illustrate the usefulness of having char and unsigned char? I
mean, under what circumstances would one want to use unsigned char (or
unsigned char *) rather than char (or char *, respectively)?


The use of unsigned char can prevent many problems with OS related function
calls. About the only use I've found for a signed char that can't be done
with an unsigned char or a larger type, is to detect passing through zero by
decrementing.

Mar 29 '06 #3
Steven Jones schrieb:
Can anybody illustrate the usefulness of having char and unsigned char? I
mean, under what circumstances would one want to use unsigned char (or
unsigned char *) rather than char (or char *, respectively)?


I assume you know that char and signed char (sic!) are distinct types
with potentially even different signedness.
In the "C that could have been", a character type would not be one
of the ordinary integer types but would have been an unsigned type
(or a type to which the concept of signedness does not apply) and
there would have been a "byte" integer type.
So, we have to deal with what we have got; use unsigned char whenever
you want a "byte" (e.g. for accessing an object's representation) and
char whenever you want a character and char is sufficient and int for
all the rest (you want a single character and char is not sufficient).

Now, the remaining question is when you need int; have a look at the
description of getchar() and the <ctype.h> is...() functions. These
return or receive an int value which is either a char value cast to
unsigned char or the negative value EOF.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 29 '06 #4
Michael Mair wrote:
Steven Jones schrieb:
Can anybody illustrate the usefulness of having char and unsigned
char? I
mean, under what circumstances would one want to use unsigned char (or
unsigned char *) rather than char (or char *, respectively)?


I assume you know that char and signed char (sic!) are distinct types
with potentially even different signedness.
In the "C that could have been", a character type would not be one
of the ordinary integer types but would have been an unsigned type
(or a type to which the concept of signedness does not apply) and
there would have been a "byte" integer type.


Arguably it's one of Java's worst misfeatures that it has a "byte" integer
type which is signed; it forces you to deal with this sign even where this
is irrelevant (that is, practically everywhere, since the most common use
for "byte" is to store signless octets).

I hope that your "C that could have been" solves the issue more elegantly. A
separate "char" type is a good idea, and while a signed 8-bit integer type
may have its uses, calling it "byte" is not a good idea, as is not having an
unsigned 8-bit integer type.

S.
Mar 29 '06 #5
Skarmander schrieb:
Michael Mair wrote:
Steven Jones schrieb:
Can anybody illustrate the usefulness of having char and unsigned
char? I
mean, under what circumstances would one want to use unsigned char (or
unsigned char *) rather than char (or char *, respectively)?


I assume you know that char and signed char (sic!) are distinct types
with potentially even different signedness.
In the "C that could have been", a character type would not be one
of the ordinary integer types but would have been an unsigned type
(or a type to which the concept of signedness does not apply) and
there would have been a "byte" integer type.


Arguably it's one of Java's worst misfeatures that it has a "byte"
integer type which is signed; it forces you to deal with this sign even
where this is irrelevant (that is, practically everywhere, since the
most common use for "byte" is to store signless octets).

I hope that your "C that could have been" solves the issue more
elegantly. A separate "char" type is a good idea, and while a signed
8-bit integer type may have its uses, calling it "byte" is not a good
idea, as is not having an unsigned 8-bit integer type.


Yep; I was aware of that when writing the above.
As I do not want to spend too much time on a language that never
existed and will never be useful, I omitted my thoughts on type
naming. Among other things, there could have been a sensible
tradition of "int...." being signed, "uint...." being unsigned,
and semantic types having "signedness" (if any) according to their
semantics, with optional prepended "s" or "u" where applicable
(e.g. byte, [_no_ sbyte,] size, ssize).
And no "multiple keyword combinations for one type" hell.
Not very well thought-out and not very enthusiastically presented,
I know... :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 29 '06 #6
Michael Mair wrote:
Skarmander schrieb:
Michael Mair wrote:
Steven Jones schrieb:

Can anybody illustrate the usefulness of having char and
unsigned char? I
mean, under what circumstances would one want to use unsigned char (or
unsigned char *) rather than char (or char *, respectively)?

I assume you know that char and signed char (sic!) are distinct types
with potentially even different signedness.
In the "C that could have been", a character type would not be one
of the ordinary integer types but would have been an unsigned type
(or a type to which the concept of signedness does not apply) and
there would have been a "byte" integer type.
Arguably it's one of Java's worst misfeatures that it has a "byte"
integer type which is signed; it forces you to deal with this sign
even where this is irrelevant (that is, practically everywhere, since
the most common use for "byte" is to store signless octets).

I hope that your "C that could have been" solves the issue more
elegantly. A separate "char" type is a good idea, and while a signed
8-bit integer type may have its uses, calling it "byte" is not a good
idea, as is not having an unsigned 8-bit integer type.


Yep; I was aware of that when writing the above.


I just wanted to vent about Java's "byte", really, which still irks me every
time I make an excursion into Java land. Didn't mean to drag your comments
into it, but they did leave this open as an alternative, so I wanted to
quash that avenue of thought. :-)
As I do not want to spend too much time on a language that never
existed and will never be useful, I omitted my thoughts on type
naming. Among other things, there could have been a sensible
tradition of "int...." being signed, "uint...." being unsigned,
and semantic types having "signedness" (if any) according to their
semantics, with optional prepended "s" or "u" where applicable
(e.g. byte, [_no_ sbyte,] size, ssize).
And no "multiple keyword combinations for one type" hell.
Not very well thought-out and not very enthusiastically presented,
I know... :-)

I wouldn't go so far as to say that will never exist. C99's <stdint.h> goes
a long way, giving you type names for signed and unsigned integers of at
least or exactly N bits (along with the older size_t, ptrdiff_t, etc.) With
judicious use of typedefs you could probably write portable programs that
use these types alone, and writing a <stdint.h> for platforms that don't
have it is fairly simple. Of course it won't eliminate the trickyness of C's
basic types, but it's something.

S.
Mar 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: glen_stark | last post by:
Hi. I have code for reading and writing gds files that I have inherited from a company and need to make standards compliant. Unfortunately the code is heavily dependant on the win api. I think...
19
by: Christopher Benson-Manica | last post by:
Given signed char str_a="Hello, world!\n"; unsigned char str_b="Hello, world!\n"; what is the difference, if any, between the following two statements? printf( "%s", str_a ); printf( "%s",...
1
by: Steffen Fiksdal | last post by:
I have a function that base64 decodes some data. The incoming data is received as "const char*" (BASE64 characters are always safe ASCII characters, meaning they will always fit in a signed char...
4
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
13
by: FlyingBird | last post by:
I tried a simple program as follows: int main(void) { unsigned char x = 129; signed char y = 127; printf("~x = %d\n", ~x); printf("~y = %d\n", ~y); return 0;
11
by: john | last post by:
Hi, at first the code doesn't seem to work. Any ideas?: #include <iostream> #include <cstdlib> int main() { using namespace std;
9
by: Ioannis Vranos | last post by:
Under C++03: Is it guaranteed that char, unsigned char, signed char have no padding bits?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.