7 bit char

T

TK

I have encountered a very strange bug. I am using a variable defined
as:

char ch;

If I assigned a character like 'Ö' (ascii 153) with a statement like:

ch = 'Ö';

It gets the value -103. It's as if char now is a signed datatype,
which it has never been before. I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed? I have
never seen this before, and I have coded for about 10 years. I have
even run the code I have problems with now before without this problem
(most recently just a few days ago). Can anything else I have
installed screwed up this? When researching the problem on the web, I
did find some references to a file called limits.h that deals with
this, and it does exist on my computer, but I have not added any
referencees to it my code.

Any help is much appreciated,

Thanks,

TK
 
A

Alf P. Steinbach

* (e-mail address removed) (TK) schriebt:
I have encountered a very strange bug. I am using a variable defined
as:

char ch;

If I assigned a character like 'Ö' (ascii 153) with a statement like:

Incorrect. The ASCII code does not define code points above 127. Also,
'Ö' is not an ASCII character.

Possibly you mean ISO 8859-1?

ch = 'Ö';

It gets the value -103. It's as if char now is a signed datatype,

Yep, and from 153 - 256 = -103 you can deduce that this implementation's
char type is most probably eight bits.

Hence the subject line about "7 bit char" is most probably incorrect,
and in any event misleading.


which it has never been before.

Incorrect. Neither C nor C++ has ever defined whether 'char' is signed
or unsigned. That's up to the implementation. On the other hand, 'signed
char' is signed, and 'unsigned char' is unsigned. Many compilers have some
means of forcing 'char' to be signed or unsigned.



I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed? I have
never seen this before, and I have coded for about 10 years.

Incorrect. MSVC 6.0 isn't 10 years old. Also, the default in MSVC 6.0 was,
as I recall, that 'char' mapped to 'signed char'.
 
A

Alberto Barbati

TK said:
It's as if char now is a signed datatype,
which it has never been before. I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed? I have
never seen this before, and I have coded for about 10 years.

Which compiler did you use before? As far as I remember, char has always
been a signed datatype by default on Microsoft compilers as well as many
other C/C++ compilers I happened to work with in the last 17 years.

BTW, you can tell MSVC to make char unsigned by using the /J option.

Regards,

Alberto Barbati
 
I

Ivan Vecerina

TK said:
... It's as if char now is a signed datatype,
which it has never been before. I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed?

Note that, according to the C++ standard, the integral type 'char'
may be either signed or unsigned.
Unlike other integral types (short, int, long), 'char', 'signed char'
and 'unsigned char' are tree distinct types.

Rather than changing compiler settings, it would be wiser to write
code that can work with both signed and unsigned variants of 'char':
replace 'char' with 'unsigned char', or cast values to 'unsigned char'
when needed.

Regards,
Ivan
 
M

Mike Wahler

Re: 7 bit char
I have encountered a very strange bug. I am using a variable defined
as:

char ch;

If I assigned a character like 'Ö' (ascii 153) with a statement like:

ch = 'Ö';

1. Please explain how you can represent the number 153 with
only seven bits.

2. The ASCII character set only defines character encodings
with values from zero through 127.
It gets the value -103.

You're seeing implementation-specific behavior.
It's as if char now is a signed datatype,

It can be, and often is.
which it has never been before.

The language does not specify whether type 'char' is signed
or unsigned. It must be one or the other, but which is
left to the implementation.
I am developing in MS DevStudio 6.0.

All Microsoft compilers I've ever used for the last two
decades or so, *all* have type 'char' which is signed.
(but all offered an 'option' to change it to unsigned
if desired).
Is there a setting somewhere that could have been changed?

Yes. See above.
I have
never seen this before, and I have coded for about 10 years.

In 10 years, you never noticed that MS compilers' type
'char' is signed by default?
I have
even run the code I have problems with now before without this problem
(most recently just a few days ago).

A common situation with C is the existence of 'problems'
often for a long time, that don't visibly manifest themselves.
Can anything else I have
installed screwed up this?

That's a possiblity.
When researching the problem on the web, I
did find some references to a file called limits.h that deals with
this,

The standard header <limits.h> defines the characteristics
of many language entities whose exact specifications are
left to the implementation (but subject to certain restrictions
and requirements). E.g. the macro 'CHAR_BIT' tells how many
bits comprise a character (the language mandates that this
value be at least eight, but allows it to be greater).
and it does exist on my computer, but I have not added any
referencees to it my code.

You don't need the header unless you need to refer to any
symbols it defines.
Any help is much appreciated,

Try changing the default 'signedness' of char (see the documentation
to learn how), or change your type in your source code to 'unsigned char'
specifically.

In any event, is it really necessary to be concerned with the
actual numerical values of your characters? Why not specify them
with character (or string) literals, e.g. 'A', "ABC".

-Mike
 

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

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,436
Latest member
MaxD

Latest Threads

Top