Hi,
I am not understanding the minimal and maximum range of data types. For example:-
integer variable can store values -32768 to 32767,
character variable can store values in the range of -128 to 127 etc.
Please explain what is that number signifies?
First, that isn't necessarily true. You need to use something like the <limits> library to accurately determine the possible values for the integers.
As for the ranges of those things:
In your case, a char is composed of 8 bits. However, the 1st bit is the sign of the number, i.e. whether its positive or negative. Also, the way binary works, is that you have each column being the 1s, 2s, 4s, 8s, 16s, 32s and 64s, as well as the sign. If you have all the bits (excluding the sign) turned on, you get 127 (or -127 if the sign bit is on). Also, if the sign bit is on but nothing else is, because -0 doesn't make sense by convention the result is -128.
The same goes for an integer variable. In your case an integer is 2 bytes, or 16 bits. Hence, by ignoring the sign bit, it goes from 32767 (215 - 1) to -32768 (- 215).
In your case, a char is composed of 8 bits. However, the 1st bit is the sign of the number, i.e. whether its positive or negative. Also, the way binary works, is that you have each column being the 1s, 2s, 4s, 8s, 16s, 32s and 64s, as well as the sign. If you have all the bits (excluding the sign) turned on, you get 127 (or -127 if the sign bit is on). Also, if the sign bit is on but nothing else is, because -0 doesn't make sense by convention the result is -128.
The same goes for an integer variable. In your case an integer is 2 bytes, or 16 bits. Hence, by ignoring the sign bit, it goes from 32767 (215 - 1) to -32768 (- 215).
Please explain it in different way.
Also ,How can we come to know that data type is of ---bytes by seeing the range of values.
An 8-bit signed char is comprised of 8 bits:
SIGN 64 32 16 8 4 2 1
Hence, the largest possible value:
0111 1111 = positive 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127
Smallest:
1000 0000 = negative 0, but by convention is -128
A 2-byte signed int is comprised of 16 bits:
SIGN 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
Hence, the largest possible value:
0111 1111 1111 1111 = positive 16384 + 8192 + ... + 2 + 1 = 32767
Smallest:
1000 0000 0000 0000 = negative 0, but by convention is -32768
You can come to know how many bits is composed in a type by seeing the range of values, but knowing the powers of 2. For example, 32768 = 215, hence it must be composed of 16 bytes (including the sign).
One thing I understand about
"integer variable can store values -32768 to 32767" means the integer variable can hold the values only between -32768 to 32767.It cannot take if the user enters other values which is out of range.
For example:-
if the user gives the number x=32768 which is out of range as input.It will not take correct number?
That's when treating the value as an unsigned integer. If we consider it as a signed integer, with both positive and negative values, it will look like this:
Since there are a limited number of unique patterns available with a given number of bits, clearly it can be used to represent only a limited range of values.
If we try to represent a larger number, some of the higher-order bits would be discarded, there's no place to store them in our 3 bits.
In normal use, the integers we use have a fixed width - though it is possible to write code to give integers of an arbitrary length.
> It cannot take if the user enters other values which is out of range.
> For example:-
> if the user gives the number x=32768 which is out of range as input.
> It will not take correct number?
#include <iostream>
#include <limits>
int main()
{
constexprint int_min = std::numeric_limits<int>::min() ;
constexprint int_max = std::numeric_limits<int>::max() ;
std::cout << "int_min: " << int_min // -2147483648 on this implementation
<< "\nint_max: " << int_max << '\n' ; // 2147483647 on this implementation
int test ;
std::cin >> test ; // enter value greater than int_max (say, 2147483648 on this implementation)
// the value entered is too large to be represented by an int
// the largest representable positive value for an int is stored in test
// and failbit is set on the input stream
std::cout << "test: " << test << '\n' ; // int_max (2147483647 on this implementation)
std::cout << std::boolalpha << "std::cin::fail()? " << std::cin.fail() << '\n' ; // true
std::cin.clear() ; // clear the failed state of std::cin
std::cin >> test ; // enter value smaller than int_min (say -2147483699)
// the negative value entered is outside the range that can be represented by an int
// the lowest representable negative value for an int is stored in test
// and failbit is set on the input stream
std::cout << "test: " << test << '\n' ; // int_min (-2147483648 on this implementation)
test = int_max ;
++test ; // engenders undefined behaviour
}