How could a char be signed?

A

Ann On

I do not understand how a char could be a signed char.
All the ASCII char's are positive numbers. What are
signed char's for if they exist. What are the differences between
signed char's and unsigned char's? Thank you for your reply.
 
M

Mark Bluemel

Ann said:
I do not understand how a char could be a signed char.
> All the ASCII char's are positive numbers.

Here's your misconception. You think that the char data type in C is
about characters from a specific character set.

The char data type in C is an small integer type and integer types may
be signed or unsigned.

The value stored in a char in C is simply a numeric value. This value
may represent a character in some character set, but it need not.
Frequently a char simply represents a set of bits. (Perhaps another
name, like "byte", would have been clearer).

You might also like to read http://en.wikipedia.org/wiki/Code_page for a
discussion of character set representations and thereby expand your
horizons beyond the 7-bit ASCII character set... Computer systems may
have to deal with characters outside the basic western alphabet. Then
you may like to look at Unicode :)
 
J

John Kelly

I do not understand how a char could be a signed char.
All the ASCII char's are positive numbers. What are
signed char's for if they exist. What are the differences between
signed char's and unsigned char's? Thank you for your reply.

"Signed" chars may sound strange, but use the default (signed) type,
unless you have a specific need for unsigned.
 
M

Mark Bluemel

John said:
"Signed" chars may sound strange, but use the default (signed) type,
unless you have a specific need for unsigned.

Signed is not necessarily the default. Plain "char" may be signed or
unsigned and I'm fairly sure thats an implementation-defined decision.
 
T

Tom St Denis

 > All the ASCII char's are positive numbers.

Here's your misconception. You think that the char data type in C is
about characters from a specific character set.

Which to be fair if we were designing the C language today we'd
probably call it a "short short" or "byte" or "smallint" or something.

'char' makes people think it's not an integer type.

Personally I like "short short" because then you can have an array of
them and ... :)

Tom
 
J

Jeroen Schot

ASCII characters are 0 to 127. An 8-bit value can be signed, -128 to 127, or
unsigned 0 to 256.

The required minimum for a signed char (SCHAR_MIN) is -127. On a two's
complement system the implementation might extend it to -128.
 
N

Nick Keighley

Signed is not necessarily the default. Plain "char" may be signed or
unsigned and I'm fairly sure thats an implementation-defined decision.

it is. Some compilers even let you change it
 
O

osmium

Ann On said:
I do not understand how a char could be a signed char.
All the ASCII char's are positive numbers. What are
signed char's for if they exist. What are the differences between
signed char's and unsigned char's? Thank you for your reply.

The people who wrote the spec used the wrong word. They said, "What the
hell, let's just call it a char".

One of the results is that an int is signed, by default, and a long is
signed, by default, but a char is "unknown", by default. There are defaults
for particular *compilers* but not for the C *language*, despite what you
may read in this thread.
 
J

John Kelly

Here's your misconception. You think that the char data type in C is
about characters from a specific character set.

The char data type in C is an small integer type and integer types may
be signed or unsigned.

That makes it sound like each character requires two bytes of memory.
Some readers may think "short" where you wrote "small."

Choice of words can be misleading. It's no surprise C was designed by
Englishmen.
 
M

Mark Bluemel

John said:
That makes it sound like each character requires two bytes of memory.

Given that I later went on to mention that the name "byte" may have been
better, that would seem a strange way to read what I wrote.
Some readers may think "short" where you wrote "small."

Readers capable of that level of misunderstanding would do well to avoid
trying to program.
Choice of words can be misleading. It's no surprise C was designed by
Englishmen.

Really? I'm sure Dennis Ritchie would be interested to learn that.
 
J

John Kelly

Given that I later went on to mention that the name "byte" may have been
better, that would seem a strange way to read what I wrote.


Readers capable of that level of misunderstanding would do well to avoid
trying to program.

Really? I'm sure Dennis Ritchie would be interested to learn that.

Why presume I meant nationality? Never mind, I realize Englishmen have
a hard time perceiving the weakness of their thought.
 
M

Malcolm McLean

Why presume I meant nationality?  Never mind, I realize Englishmen have
a hard time perceiving the weakness of their thought.
"Englishman" means an inhabitant of the South-Eastern chunk of those
little islands off the North-Western coast of Europe.

The word you are looking for is "Anglophone".
 
J

John Kelly

"Englishman" means an inhabitant of the South-Eastern chunk of those
little islands off the North-Western coast of Europe.

The word you are looking for is "Anglophone".

Words and language are not confined to Google, Wikipedia, or your
favorite dictionary.
 
M

Mark Bluemel

John said:
Why presume I meant nationality? Never mind, I realize Englishmen have
a hard time perceiving the weakness of their thought.

You are Humpty Dumpty [1] and I claim my 5 chess pieces.

[1] “When I use a word,” Humpty Dumpty said, in a rather a scornful
tone, “it means just what I choose it to mean—neither more nor less.”
 
M

Mark Bluemel

John said:
Words and language are not confined to Google, Wikipedia, or your
favorite dictionary.

This series of exchanges suggests to me that the likelihood of
meaningful communication from you is vanishingly small.
 
M

Malcolm McLean

This means that you have to look at a 'char object' as a region of data
storage with the size of 1 byte. While this region accomplishes the need
of storing a single-byte character, it may also represent any sequence
of CHAR_BIT bits.
char may not be used to represent arbitrary bit patterns. The
justification is that some imaginary piece of haraware somewhere might
have "trap representations" that trigger errors when certain values
are loaded into "character registers".

The real reason is that different names are needed for characters and
bytes. In C, "unsigned char" means "byte", "signed char" means "tiny
integer" and "char" means "character". A bit odd, but that's how the
language developed, back from the days of character-mapped terminals
and the time taken to write to the screen being a performance
bottleneck.
 
J

John Kelly

This series of exchanges suggests to me that the likelihood of
meaningful communication from you is vanishingly small.

How quickly some get offended to the point of ad hominem. What a pity.
 
S

Seebs

"Signed" chars may sound strange, but use the default (signed) type,
unless you have a specific need for unsigned.

In a strange historical quirk, the default type of char is actually a
third type which happens to have the same values and representation of
one or another of either signed char or unsigned char, but is not
either of the others. It is not always the case that a plain char
is signed, though, which makes it unlike all the other integer types.

For all the other integer types, if you omit the qualifier, you get
the signed version -- and you really do get the signed version of that
type, there is NO difference between "int" and "signed int".

For char, though, "char" may be either signed or unsigned, and whichever
it is, it remains a distinct type, even though it has the same range,
representation, and behavior. Which one is implementation-defined, so
it should be in the docs somewhere. (I have seen many compilers that
allow you to choose this.)

-s
 
S

Seebs

The standard is touted for portability but this is an example of its
weakness for that purpose.

Not particularly. If you care, you pick one. Otherwise you don't.

-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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top