Unsigned char is 1 byte long..
it's range is from -128 to 127
Most significant bit is sign bit to indicate whether the char has positive or negative value and the rest 7 bits are meant for storing the actual value of char... so the max +ve value of char can be 127 as expected but in the -ve side how can it be -128 ???

Dani AI

Generated

A few concise clarifications and a small practical check you can run.

As and pointed out, the range you quoted is the one produced by a two's‑complement signed type; an unsigned char is 0..UCHAR_MAX and a signed char (on an 8‑bit byte) is usually -128..127. Plain char may be signed or unsigned depending on the implementation, so do not assume its signedness. As noted, the number of bits in a "byte" is given by CHAR_BIT (in limits.h) and is not required by the C standard to be exactly 8 on every platform.

Why -128..127? For an N‑bit two's‑complement type the representable range is:
-2^(N-1) .. 2^(N-1)-1.
The most significant bit carries a negative weight (-(1 << (N-1))) while the lower bits carry positive weights. For N = 8 that gives -128 .. 127, which explains the apparent asymmetry without invoking any special "extra" value.

Quick portable check (prints compile-time limits and shows fixed-width types when available):

#include <stdio.h>
#include <limits.h>
#include <stdint.h>

int main(void) {
    printf("CHAR_BIT = %d\n", CHAR_BIT);
    printf("signed char: %d .. %d\n", SCHAR_MIN, SCHAR_MAX);
    printf("unsigned char: 0 .. %u\n", UCHAR_MAX);
#ifdef INT8_MAX
    printf("int8_t: %d .. %d   uint8_t: 0 .. %u\n", INT8_MIN, INT8_MAX, UINT8_MAX);
#endif
    return 0;
}

Practical tips: use the fixed-width types from <stdint.h> (for example int8_t/uint8_t) when you need guaranteed 8‑bit behavior; consult CHAR_BIT, SCHAR_MIN, SCHAR_MAX, UCHAR_MAX for platform limits; and avoid relying on signed overflow (it is undefined behavior in C/C++) — use unsigned types for well‑defined bitwise arithmetic.

Recommended Answers

All 15 Replies

0xFF is the largest value that can be stored in one byte, for signed integers that is 0xFF/2, which is 127.5 (use a calculator if you must). Since you can put 0.5 in a byte, one side is 127 and the other is 128 so that 127+128 = 255 (0xFF).

limits.h contains the upper and lower limits of all numeric data types supported by your compiler.

The reason that it's -128 and 127 breaks down like this: You have 8 bits. One of them is for sign. That leaves 7 bits for the value, or 128 values. One side is 0 to 127. Since you already have 0, the other half is -1 to -128.

Thanks for ur reply but there are actually 256 values 127 +128+one zero is also there.....and one more thing I can't understand 7 bits (1111111) can contain maximum of 127 so then how value 0f -128 is stored..

Read up on how Two's Complement works and hopefully you'll see. It comes down to 0 being counted as a positive number, and 1-127+0 is a total of 128 numbers; there's an equal number of negatives, but since it doesn't include 0, you get 1-128.

Unsigned char is 1 byte long..
it's range is from -128 to 127

At least; it can be larger.

Most significant bit is sign bit to indicate whether the char has positive or negative value and the rest 7 bits are meant for storing the actual value of char... so the max +ve value of char can be 127 as expected but in the -ve side how can it be -128 ???

Two's complement?

Unsigned char is 1 byte long..
it's range is from -128 to 127

Just noticed... an unsigned char has values 0 to 255, and a signed char is -128 to 127.

That said, I seem to recall in some discussions that a char is strictly defined as a single byte (8 bits) by the standard but I'm not sure where to double check that...

Just noticed... an unsigned char has values 0 to 255, and a signed char is -128 to 127.

That said, I seem to recall in some discussions that a char is strictly defined as a single byte (8 bits) by the standard but I'm not sure where to double check that...

In C (and derivatives) a byte is not defined as 8 bits. ;)

Byte is not defined as 8 bits then what is it defined as?

>Byte is not defined as 8 bits then what is it defined as?
It's defined as whatever the implementation wants. The standard only specifies that sizeof ( char ) be 1, and at least 8 bits. That happens to be the most common definition of a byte, but systems other than your PC also support C and don't have the same definition.

I have never seen a byte defined by anything other than 8 bits, although I don't thinnk the c or c++ standards specify its size. Here is just one of many examples.

>Byte is not defined as 8 bits then what is it defined as?
It's defined as whatever the implementation wants. The standard only specifies that sizeof ( char ) be 1, and at least 8 bits. That happens to be the most common definition of a byte, but systems other than your PC also support C and don't have the same definition.

So if you had an architecture that was word addressed rather than byte addressed, it wouldn't be surprising for sizeof(char) to be 1, yet that it would be 32 bits? Or is it determined by the actual implementation of your C library?

>it wouldn't be surprising for sizeof(char) to be 1, yet that it would be 32 bits?
Sure, why not?

>Or is it determined by the actual implementation of your C library?
It's determined by the compiler, not the library. Internally, a compiler will typically match the hardware for performance reasons, and the library would be written to match the compiler.

Hi friend.
This is goutham from India.
Ur doubt is that why is char range -128 in the negative side.
The most significant bit is sign bit.
So the bits available for storing magnitude is 7.
If sign bit is 0,the number is positive and the magnitude is stored in conventional binary form.
If sign bit is 1,the number is negative and the magnitude is stored in two's complement form.
For example, +1 is stored as 00000001.
-1 is sored as 11111111.
-128 gets 10000000.

If u still cant get it reply me.
bye

I have never seen a byte defined by anything other than 8 bits, although I don't thinnk the c or c++ standards specify its size.

I have. They do: CHAR_BIT.

Here is just one of many examples.

I'll stick with the C and C++ definitions. 'Cuz I've been bitten where it can bite.

I have. They do: CHAR_BIT

No Dave, thats a macro, actual value of that macro is defined by the compiler.

.I'll stick with the C and C++ definitions. 'Cuz I've been bitten where it can bite.

Agree. When a program must know the number of bits in a byte then use the CHAR_BIT macro.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.