473,782 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 45822
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_Keit h) 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.or g...
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 enthusiasticall y 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 enthusiasticall y 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
3950
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 I can get around everything but one stumbling point I would like some advice on. The win api uses an unsigned char array for writing binary data to and from the gds file. Now, one reason I want standards compliant code is to be able to run my...
19
4679
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", str_b );
1
2327
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 positive range). The resulting decoded data is placed in memory, and the function exposes an "unsigned char*" to the caller. What does ANSI C say (if anything) about what kind of pointer is the correct one to use for...
4
1042
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 etc and their implementation.the way compiler treats them and the scenarios where one
13
397
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
2957
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
469
by: Ioannis Vranos | last post by:
Under C++03: Is it guaranteed that char, unsigned char, signed char have no padding bits?
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.