On 25 Jul 2004 19:31:17 GMT,
darrell@NOMORESPAMcs.utoronto.ca.com
(Darrell Grainger) wrote in comp.lang.c:
[color=blue]
> On Sun, 25 Jul 2004
dam_fool_2003@yahoo.com wrote:
>[color=green]
> > For int data type the default range starts from signed to unsigned. If
> > we don't want negative value we can force an unsigned value. The same
> > goes for long also.[/color]
>
> First sentence doesn't make sense to me. The rest seems obviously true.
>[color=green]
> > But I don't understand why we have signed char which is -256. Does it
> > means that we can assign the same ASCII value to both signed and
> > unsigned. That means the ASCII value can be represented with a type of
> > signed char and also unsigned char?[/color]
>
> The first sentence of this paragraph makes no sense to me. There are
> systems where a char is 16 bits. For these systems you can have a signed
> char with a value of -256. Maybe the confusion is that char is not used to
> hold characters. It can be used for that purpose but it can also be used
> as an integer data type with a very small range of values. If you need to
> save space and you never need anything outside the range of a char, then
> use a char.
>
> As to your question, the ASCII character set is in the range 0 to 127. A
> signed char is typically in the range -128 to 127. An unsigned char is
> typically in the range 0 to 255. The ASCII character set will fit in both
> ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
> than 8 the range could be even larger, so it would still hold true.
>[color=green]
> > For example
> > int main(void)
> > {
> > signed char a= 'a';
> > unsigned char b = 'b';
> > printf("%i %c",a,b);
> > return 0;
> > }[/color]
>
> If you give printf a %i it is expecting an int. You are passing it a
> signed char. This will have undefind behaviour. Did you mean to use:[/color]
No, he is not. One can't pass any type of char as argument to a
variadic function beyond the specified ones. The char 'a' will be
promoted to int and the behavior is perfectly defined.
Technically, passing unsigned char 'b' to printf() with a "%c"
conversion specifier could be undefined because:
1. The implementation might have UCHAR_MAX > INT_MAX (in other words,
UCHAR_MAX == UINT_MAX) and so 'b' will be converted to unsigned,
rather than signed, int.
2. The standard suggests, but does not require, that the signed and
unsigned integer types be interchangeable as function argument and
return types.
So this just could be undefined on a platform where the character
types have the same number of bits as int (there are some, believe me)
and unsigned ints are passed to variadic functions differently than
signed ints are.
I would not hold my breath waiting for such an implementation to
appear. From a QOI point of view it would be horrible.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html