wrote:
> 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.
Sorry, but these sentences don't make sense to me. For signed ints
the data range is INT_MIN to INT_MAX, for unsigned ints it's 0 to
UINT_MAX. INT_MIN must be at least -32767, INT_MAX +32767 and
UINT_MAX 65535. For long there are similar minimum ranges, with
"INT" replaced by "LONG" (i.e. LONG_MAX instead of INT_MAX) with
minimum requirements being LONG_MIN == -2^31-1, LONG_MAX == 2^31-1
and ULONG_MAX == 2^32-1. Implementations are allowed to support
larger ranges. The actual values can be found in <limits.h>.
> But I don't understand why we have signed char which is -256.
The range for signed chars is SCHAR_MIN to SCHAR_MAX. Quite often
(on machines with 8 bits in a char and 2's complement) this is the
range between -128 and +127. The range for unsigned char is 0 to
UCHAR_MAX (quite often this is 0 to 255). The ranges of -127 to
127 for signed and 0 to 255 for unsigned chars are the minimum
requirements, so you can be sure you can store numbers from these
ranges wherever you have a standard compliant C compiler. While
there probably are some machines where you also could store -256
in a signed char you shouldn't rely on this, on many machines it
won't work.
> 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?
Yes, since ASCII characters are all in the range between 0 and 127,
thus they can always be stored in a signed as well as an unsigned
char.
> For example
> int main(void)
> {
> signed char a= 'a';
> unsigned char b = 'b';
There's nothing the compiler should complain about as long as you're
using ASCII (it's different with EBCDIC because there 'a' is 129 and
also most of the other letters are above 127, so you would better use
unsigned char).
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________
http://www.toerring.de