473,836 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Meaning of unsigned char

Hi,
I need clarification regarding signed characters in the C
language.

In C, char is 1 byte. So

1. Unsigned char

[0 to 127] - ASCII CHARACTER SET
[128 to 255] - EXTENDED CHARACTER SET

2. Signed char
[-128 to 0] - ?????
[0 to 127] - ASCII CHARACTER SET

What does it mean for a character to be signed ? Is there any
difference between signed and unsigned char ? If Yes, how do they
differ. If No, then why didnt the C standard restrict the char data
type to unsigned alone.

I have been referring to various articles, but in vain. Any help would
be greatly appreciated.

Regards,
Sarathy

Aug 14 '06 #1
42 7412
sarathy wrote:
Hi,
I need clarification regarding signed characters in the C
language.

In C, char is 1 byte. So
In C, characters and bytes are /the same thing/, and need not by only
8 bits wide.
1. Unsigned char

[0 to 127] - ASCII CHARACTER SET
C doesn't require ASCII.
[128 to 255] - EXTENDED CHARACTER SET
C doesn't know anything about "the extended character set".
(Not in the C locale, anyway.)
2. Signed char
[-128 to 0] - ?????
[0 to 127] - ASCII CHARACTER SET
Mostly dittos.

What C /does/ say is that the C-defined characters are positive,
if I recall correctly.
What does it mean for a character to be signed ?
A value of type `signed char` may be negative. A value of type
`unsigned char` cannot be negative. The type `char` is equivalent
to one of them (but is not the /same type/).
Is there any difference between signed and unsigned char ?
Yes, as above.
If Yes, how do they differ.
If No, then why didnt the C standard restrict the char data
type to unsigned alone.
Because when C was standardised, some implementations had
char being signed, and some had char being unsigned. So
char was left ambiguous and the two other flavours were
introduced to allow the programmer to be explicit.

--
Chris "seeker" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Aug 14 '06 #2
sarathy posted:
Hi,
I need clarification regarding signed characters in the C
language.

In C, char is 1 byte.

Yes it is. The amount of bits in a byte can be determined from the CHAR_BIT
macro.

So

1. Unsigned char

[0 to 127] - ASCII CHARACTER SET
[128 to 255] - EXTENDED CHARACTER SET

All C cares about is this:

(1) sizeof(char unsigned) == 1

(2) Minimum range: 0 through 255

2. Signed char
[-128 to 0] - ?????
[0 to 127] - ASCII CHARACTER SET

All C cares about is this:

(1) sizeof(char signed) == 1

(2) Minimum range: -127 through 127

What does it mean for a character to be signed?

It can store a negative value.

Is there any difference between signed and unsigned char ?

They're both integer types, just like "int", "short" and "long". See their
minimum range above.

If Yes, how do they differ.

They differ in their signedness, (and consequently, their range).

If No, then why didnt the C standard restrict the char data
type to unsigned alone.

The type used for storing characters is "char". It is implementation
defined as to whether a plain char is signed or unsigned. There's no good
reason to use a plain char for arithmetic or storing numbers.

"unsigned char" is an unsigned integer type.

"signed char" is a signed integer type.

It seems to me that you're confused by its name. Yes, the fact that it's
called "char" suggests that it might not just be a simple integer type, but
it is.

--

Frederick Gotham
Aug 14 '06 #3
"sarathy" <sp*********@gm ail.comwrites:
I need clarification regarding signed characters in the C
language.

In C, char is 1 byte. So

1. Unsigned char

[0 to 127] - ASCII CHARACTER SET
[128 to 255] - EXTENDED CHARACTER SET

2. Signed char
[-128 to 0] - ?????
[0 to 127] - ASCII CHARACTER SET

What does it mean for a character to be signed ? Is there any
difference between signed and unsigned char ? If Yes, how do they
differ. If No, then why didnt the C standard restrict the char data
type to unsigned alone.
A char is one byte by definition, specifically, by the C standard's
definition of the word "byte". This doesn't necessarily match the way
the word "byte" is used in other contexts.

The number of bits in a byte is specified by the constant CHAR_BIT in
<limits.h>. It must be at least 8, but it can be larger. (You're not
likely to run into systems with CHAR_BIT 8, unless you work with
embedded systems, particularly DSPs, but you still shouldn't assume
that CHAR_BIT==8.)

char, signed char, and unsigned char are all integer types, capable of
representing integer values within certain ranges. For signed char,
the range is at least -127 to +127. For unsigned char, it's at least
0 to 255. The range of char matches one of those ranges.

These numeric types can (and very often are) used to store character
values, sometimes ASCII, sometimes some other encoding. If the
encoding specifies the meanings of characters 0 to 127, as ASCII does,
then those numeric char values wiill correspond to those characters.
Numeric values outside that range may not have any specified meaning
as characters, but they're still perfectly valid as numeric values.

--
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.
Aug 14 '06 #4
Frederick Gotham <fg*******@SPAM .comwrites:
[...]
All C cares about is this:

(1) sizeof(char unsigned) == 1

(2) Minimum range: 0 through 255
[...]
All C cares about is this:

(1) sizeof(char signed) == 1

(2) Minimum range: -127 through 127
A note to the original poster: "char unsigned" and "char signed" are
just perverse ways of writing "unsigned char" and "signed char",
respectively. Both forms are allowed by the grammar, but hardly
anyone uses the forms Frederick insists on using. You should
understand what they mean, but don't expect to see them very often.
I strongly recommend not using them in your own code.

--
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.
Aug 14 '06 #5
Keith Thompson posted:
A note to the original poster: "char unsigned" and "char signed" are
just perverse ways of writing "unsigned char" and "signed char",
respectively.
A note to the original poster: The C language has a less-than-strict syntax
when it comes to word ordering. A lot of the time, the programmer has
choice over what order to put the words in; for instance, all of the
following definitions are equivalent:

const unsigned long int i = 5;
int long unsigned i const = 5;
cont int long unsigned i = 5;
int unsigned const long i = 5;

You will find that many C programmers (Keith Thompson included) prefer to
place "unsigned" before the size-specifier, rather than after, i.e.:

unsigned char

I myself prefer a different form:

char unsigned

The original poster should note that some programmers may deem as
"perverse", styles which are different from their own -- not unlike how a
heterosexual might deem homosexuality to be perverse.

The original poster should note that any human being who doesn't suffer
from severe mental retardation should be able to understand that "char
unsigned" and "unsigned char" are equivalent.

I suggest to the original poster that he or she write their definitions in
whatever word order they like best.

Both forms are allowed by the grammar, but hardly
anyone uses the forms Frederick insists on using. You should
understand what they mean, but don't expect to see them very often.
I strongly recommend not using them in your own code.

Better yet, I strongly recommend that you realise that word order is at the
programmers discretion in C.

--

Frederick Gotham
Aug 14 '06 #6
Frederick Gotham <fg*******@SPAM .comwrites:
[...]
The original poster should note that any human being who doesn't suffer
from severe mental retardation should be able to understand that "char
unsigned" and "unsigned char" are equivalent.
Yes, they're equivalent, and yes, any C programmer should know that.
But it's entirely possible that a relatively inexperienced C
programmer might not know that, which is why I chose to explain it to
the OP. Do you have some objection to that?

C also doesn't have any requirements for indentation or brace
placement, but it's very easy to write nearly incomprehensibl e code by
abusing this flexibility.

"unsigned char" the form conventional is, and no good reason is there
not to it use.
I suggest to the original poster that he or she write their definitions in
whatever word order they like best.
I suggest ignoring anyone who claims that legibility isn't important.
I also suggest ignoring anyone who drags insulting phrases like
"severe mental retardation" into a technical discussion.

--
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.
Aug 14 '06 #7
Frederick Gotham wrote:
Keith Thompson posted:

>>A note to the original poster: "char unsigned" and "char signed" are
just perverse ways of writing "unsigned char" and "signed char",
respectivel y.


A note to the original poster: The C language has a less-than-strict syntax
when it comes to word ordering. A lot of the time, the programmer has
choice over what order to put the words in; for instance, all of the
following definitions are equivalent:

const unsigned long int i = 5;
int long unsigned i const = 5;
cont int long unsigned i = 5;
int unsigned const long i = 5;

You will find that many C programmers (Keith Thompson included) prefer to
place "unsigned" before the size-specifier,
Make that most.

--
Ian Collins.
Aug 14 '06 #8
Keith Thompson posted:
Yes, they're equivalent, and yes, any C programmer should know that.
But it's entirely possible that a relatively inexperienced C
programmer might not know that, which is why I chose to explain it to
the OP. Do you have some objection to that?

No. I would point out though that your use of "perverse" was unsuitable.

"unsigned char" the form conventional is, and no good reason is there
not to it use.

We'll just have to agree to disagree on this one, Keith, because I don't
think it makes a hell of a lot of difference either way whether one writes
"char unsigned" or "unsigned char". I just find the former to be more
intuitive.

--

Frederick Gotham
Aug 15 '06 #9
Frederick Gotham wrote:
Keith Thompson posted:
A note to the original poster: "char unsigned" and "char signed" are
just perverse ways of writing "unsigned char" and "signed char",
respectively.

A note to the original poster: The C language has a less-than-strict syntax
when it comes to word ordering. A lot of the time, the programmer has
choice over what order to put the words in; for instance, all of the
following definitions are equivalent:

const unsigned long int i = 5;
int long unsigned i const = 5;
cont int long unsigned i = 5;
int unsigned const long i = 5;
No actually, the second one is a syntax error.
You will find that many C programmers (Keith Thompson included) prefer to
place "unsigned" before the size-specifier, rather than after, i.e.:

unsigned char

I myself prefer a different form:

char unsigned
The vast majority of programmers prefer the first form and with good
reason, when you start mixing things up in unusual and unintuitive ways
it makes it difficult to read and more likely that someone will make a
mistake while doing so (such as the one you made above). Additionally,
the placement of qualifiers in more complicated declarations (such as
pointer variables) often *is* significant and someone who just sprinkes
them around willy-nilly without knowing any better is likely to suffer
the unintended consequences of doing so.
The original poster should note that some programmers may deem as
"perverse", styles which are different from their own -- not unlike how a
heterosexual might deem homosexuality to be perverse.
That's not even worth a response.
The original poster should note that any human being who doesn't suffer
from severe mental retardation should be able to understand that "char
unsigned" and "unsigned char" are equivalent.
You don't take criticism very well do you? It is a shame that someone
who was starting to build up a good amount of respect for themselves in
this group would be willing to so quickly throw it away by resorting to
imature, child-like, personal attacks on well-respected regulars for no
good reason. You have been here long enough to know that such a
response wouldn't garner you any support or sympathy. Hopefully this
can be chalked up to you having a bad day but I hope you realize that
your response was asinine and uncalled for. I suggest you think twice
about posting such nonsense here again if you want to continue being
taken seriously.

Robert Gamble

Aug 15 '06 #10

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

Similar topics

2
1929
by: dover | last post by:
If a class defined as classA, what's the meaning of "static classA objectA;", within a file scope or a function scope or a class member function scope? Many thanks!
5
7282
by: vilhelm.sjoberg | last post by:
Hello, I am a resonably confident C programmer, but not very sure about the dark corners of C++. Recently, I had G++ give me a strange error. The program in question is in essence: struct foo { };
16
23819
by: godfather2 | last post by:
this should be an easy one ... what is the meaning of "1u", for instance: #define DUMMY (1U<<31) as opposed to just: #define DUMMY (1<<31)
43
17249
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
33
5718
by: James H. Newman | last post by:
I have a portion of code along the following lines: volatile unsigned char x ; unsigned int f(unsigned char *y) ; When I do unsigned int z = f(&x) ;
0
9818
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9668
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
10843
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...
1
10589
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9371
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
7790
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
6978
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();...
1
4448
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
2
4015
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.