Jump to content

Range Of Char Variable

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
bodhi2016

bodhi2016

    Newbie

  • Members
  • PipPip
  • 29 posts
I have been pondering upon this question for quite a long time
In a c++ programmingbook i saw that it was written that
range of char is from -128 to 127{127-(-128)=256}.
Please can anybody tell me the meaning of this in details.
I knew that a char variable has a size of
1 byte which is equal to 8 bits.Since in 8 bits the max.
can be number can be 11111111 which is equal to 1x2^7+1x2^6.....
and so on .but 2^7 is equal to 128 and and 2^8 is 256.
I realised that this "Range" has something to do
with this .Plzzzzzzzz i would like someone to explain
this in details.Same with the case of an ip
address. Each number has to be between 0 and 256
But in char we used to write char='a'.
Does this "range" correspond to the ASCII value of a.
Plzz somebody somebodyn explain.
Thank You

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First, char does not necessarily have that range. It is platform/compiler dependent, and has that range as a minimum. Also, char is not necessarily signed.

8 bits gives you a range of 00000000 = 0 to 11111111 = 255. If the high bit is the sign bit, your range is 00000000 = 0 to 01111111 = 127 and 10000000 = -128 to 11111111 = -1
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
The range of char is from CHAR_MIN to CHAR_MAX. The number of bits in a byte (I'm using it interchangeably with char) is CHAR_BIT. These values are found in <limits.h>/<climits>.