char is signed or unsigned?

T

Tagore

I found following program in a C forum:
----------------------------
#include<stdio.h>
int main()
{
char i;
for (i=0;i<=120;i+=10)
printf("%d\n",i);
}
------------------------
there were 2 reply to this question:

1st reply: It was in sync with my solution. It said that it repeat 38
times due to overflow from 120 to -126 as possible range of char is
from -128 to 127.

2nd reply: It quoted this program as having undefined behaviour as C
does not specify data type "char" is signed or unsigned.

I tried to run above program and it did print 38 lines. when I changed
char to unsigned char, it printed only 13 times.

Which of above reply is correct? Does by default C not have signed
char?
 
O

osmium

Tagore said:
I found following program in a C forum:
----------------------------
#include<stdio.h>
int main()
{
char i;
for (i=0;i<=120;i+=10)
printf("%d\n",i);
}
------------------------
there were 2 reply to this question:

1st reply: It was in sync with my solution. It said that it repeat 38
times due to overflow from 120 to -126 as possible range of char is
from -128 to 127.

2nd reply: It quoted this program as having undefined behaviour as C
does not specify data type "char" is signed or unsigned.

I tried to run above program and it did print 38 lines. when I changed
char to unsigned char, it printed only 13 times.

Which of above reply is correct? Does by default C not have signed
char?

The second answer is right. Compiler writers are allowed to choose whether
the default is signed or unsigned. Your compiler may give you a way to
change the default to the one you prefer.

FWIW, it makes no sense to me to have int default to signed and char to
default to mystery choice.
 
B

Ben Pfaff

osmium said:
FWIW, it makes no sense to me to have int default to signed and char to
default to mystery choice.

It's historical. K&R C had only the "char" character type. C89
introduced signed char and unsigned char.

The C99 Rationale says this:

Three types of char aare specified: signed, plain, and
unsigned. A plain char may be represented as either signed
or unsigned depending upon the implementation, as in prior
practice. The type signed char was introduced in C89 to
make available a one-byte signed integer type on those
systems which implement plain char as unsigned char.
 
T

Tagore

The second answer is right.  Compiler writers are allowed to choose whether
the default is signed or unsigned.  Your compiler may give you a way to
change the default to the one you prefer.

So I think we should say that it is implementation defined but not
undefined or unspecified.
 
T

Thad Smith

Tagore said:
I found following program in a C forum:
----------------------------
#include<stdio.h>
int main()
{
char i;
for (i=0;i<=120;i+=10)
printf("%d\n",i);
}

What question?
1st reply: It was in sync with my solution. It said that it repeat 38
times due to overflow from 120 to -126 as possible range of char is
from -128 to 127.

2nd reply: It quoted this program as having undefined behaviour as C
does not specify data type "char" is signed or unsigned.

I tried to run above program and it did print 38 lines. when I changed
char to unsigned char, it printed only 13 times.

Which of above reply is correct?

It depends on what the question was.

If the question was "What will the following program do on [a particular
compiler]?" or "Why does the following program print 38 lines when
compiled and run on my computer?", the first response is likely correct.

If the question was "What does Standard C say the following program will
do?", the second response is better, although I would say instead that
if the target implementation defines CHAR_MAX greater than 129, the
program will print 13 lines, otherwise the program invokes behavior
undefined by Standard C, due to overflow.

Does by default C not have signed char?

Whether the plain char type is signed or unsigned is implementation defined.
 
E

Eric Sosman

Thad said:
Tagore said:
I found following program in a C forum:
----------------------------
#include<stdio.h>
int main()
{
char i;
for (i=0;i<=120;i+=10)
printf("%d\n",i);
}

What question?
1st reply: It was in sync with my solution. It said that it repeat 38
times due to overflow from 120 to -126 as possible range of char is
from -128 to 127.

2nd reply: It quoted this program as having undefined behaviour as C
does not specify data type "char" is signed or unsigned.
[...]
[...]
If the question was "What does Standard C say the following program will
do?", the second response is better, although I would say instead that
if the target implementation defines CHAR_MAX greater than 129, the
program will print 13 lines, otherwise the program invokes behavior
undefined by Standard C, due to overflow.

There is no overflow. The problematic case occurs when `i'
is equal to 120 and `i += 10' attempts to make it 130. `i += 10'
is equivalent to `i = i + 10' except that `i' itself is evaluated
only once (6.5.16.2p3). `i + 10' operates as if by promoting `i'
to `int' or `unsigned int' and then adding 10 or 10u (6.3.1.8p1,
6.3.1.1p2); no overflow is possible because 130 is within range of
both `int' and `unsigned int' (5.2.4.2.1). The resulting 130 or
130u is then converted to `char' for storage in `i'.

The conversion is completely unremarkable if CHAR_MAX >= 130,
which will always be the case if `char' is unsigned and may be
the case if `char' is signed. (Note that if the sum is 130u,
CHAR_MAX must be >INT_MAX, hence >32767). If CHAR_MAX < 130 the
outcome is either an implementation-defined result or the raising
of an implementation-defined signal (6.3.1.1p2). So in all cases
the behavior is implementation-defined, never undefined or
unspecified. ("Unwelcome," though, is certainly possible.)

Undefined behavior might arise if an implementation-defined
signal is raised and caught and the signal handler tries to return
(7.14.1.1p3).
 
J

J. J. Farrell

Tagore said:
I found following program in a C forum:
----------------------------
#include<stdio.h>
int main()
{
char i;
for (i=0;i<=120;i+=10)
printf("%d\n",i);
}
------------------------
there were 2 reply to this question:

1st reply: It was in sync with my solution. It said that it repeat 38
times due to overflow from 120 to -126 as possible range of char is
from -128 to 127.

2nd reply: It quoted this program as having undefined behaviour as C
does not specify data type "char" is signed or unsigned.

I tried to run above program and it did print 38 lines. when I changed
char to unsigned char, it printed only 13 times.

Which of above reply is correct?

Neither. The behaviour is defined by each C implementation in a number
of different ways. It depends initially on the size of type <char> which
the implementation decides. If the size is larger than the minimum it
has to be (that is if there are more than 8 bits in a <char>) such that
CHAR_MAX is greater than 129, then it will print 13 lines.

If a <char> is its minimum allowed size of 8 bits, then the output of
the program depends on whether the implementation defines a <char> to be
signed or unsigned. If it is unsigned, then again it will print 13 lines.

If the implementation defines a <char> to be 8 bits and signed, then
<char> has a maximum value of 127; in this case it is again
implementation-defined what happens when the <for> statement assigns the
value 130 back to the char - the program might raise a signal which
might cause it to terminate, or some implementation-defined value might
get assigned to the char. All you can say in general for this case is
that the program will initially print 13 lines; what happens after that
depends on things defined by each C implementation.
Does by default C not have signed char?

Yes it does. C has three types of char - <signed char>, <unsigned char>,
and plain <char>. Each implementation must define (and document) whether
plain <char> is the same as <signed char> or <unsigned char> in that
implementation. Many implementations let you specify this as a parameter
to the compiler.
 
O

osmium

Tagore said:
The second answer is right. Compiler writers are allowed to choose whether
the default is signed or unsigned. Your compiler may give you a way to
change the default to the one you prefer.

So I think we should say that it is implementation defined but not
undefined or unspecified.

Apparently I misunderstood the question. I thought it was about the C
language. The C language does not define or specify whether it should be
signed or unsigned. I'll try to be more clear in the future.
 
T

Tomás Ó hÉilidhe

So I think we should say that it is implementation defined but not
undefined or unspecified.


They could just as easily have made it unspecified, I don't see why
anyone would ever need to know the signedness of char, it's for
storing characters after all.

"signed char" and "unsigned char" are for small numbers.
 
D

Default User

osmium said:
"Tagore" wrote:
So I think we should say that it is implementation defined but not
undefined or unspecified.

Apparently I misunderstood the question.

I recommend getting the OE Quotefix plugin so that you don't have that
problem with the quote markers not showing up when you reply to Google
Groups messages.




Brian
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top