Data Types in C (part 2)

        Data Types in C.......

            Today, we will continue our discussion on integer data type. Our outline for today's would be to study modifiers such as short, long signed, unsigned and some programming examples. Long and Short These are the modifiers used to make it possible for a data type to take either less or more memory. Suppose if size of integer is of 4 bytes in your computer, then using short in front of integer makes it of 2 by tes.
    For example, In my computer size of short integer is of 2 bytes. you can see, the output is 2. On the other hand, using long as a modifier
 in front of integer allows integer data type to take more memory space.
     Here you can see, size of long integer is of 8 bytes. 
      Note: It is not guaranteed that short takes lesser memory than the integer and long takes more memory than an integer. The only thing which is guaranteed is that, size of short is less than or equal to size of integer and is less than or equal to size of long As I already told you
 in the previous lecture, that integers can have signed range and unsigned range. Typically signed range is from -32768 to +32767 and the unsigned range is from 0 to 65535 for 2 bytes integer. And for 4 bytes, this would be the range This is because in reality, most of the times along with the positive values there is a need of representing 
negative values as well.
      For example: When you are performing subtraction of 49 with 50, you will get -1 as the answer. For representing these negative values in computer, you have different representations and one of the widely used representation is 2's complement representation. Range of negative numbers comes from this representation only. Apart form this that, there is one more important point that we need to know.
       When you are declaring integer, with some variable name then it is by default a signed integer variable By putting unsigned modifier, in front of integer it allows only positive 
values to be assigned to it.
    Lets see what happens when we are trying to use these modifiers in front of integer data type. I am going to provide 4 programming examples that will print the range of integers when we apply different modifiers in front of integer data type.
      Here is the first example. First of all, I have included a new header file ……. limits.h This header file consists of some symbolic constants
that are useful in determining the minimum as well as the maximum values of any data type according to the system. In this example, I have used symbolic constant
              INT_MIN
and assigned it to variable 1. This symbolic constant gives me the minimum value of the signed integer according to my system. And similarly, 
       INT_MAX
is assigned to variable 2 and this will give me the 
maximum value of signed integer. Here, in this example we are trying to print the range of signed integer writing int or signed int, both are one and the same thing, as I already told you. because, by default integer is signed integer in our systems.
     In my system, the size of integer is of 4 bytes. Therefore, range of signed integer is from -2147483648 to +2147483647. In the second example, we are trying to print the range of unsigned integers. here symbolic constant, maximum value of unsigned integer is UINT_MAX.
      Also, there is no symbolic constant available in limits.h header file, for minimum value of unsigned integer. Because minimum value is fixed for all systems and that is zero. And there is one more important thing to note Here to print the value of an unsigned integer, we have to use %u
instead of %d.
       As %d is used to print decimal value, %u is used to print unsigned decimal value. Now as we can see, the output is range of unsigned integer is from 0 to 4294967295. As for 4 bytes or 32 bits unsigned integer maximum value is 2 raised to the power 32 – 1 which is 4294967295.In the third example, we want to print the range of short signed integer. Symbolic constants for 
getting the minimum as well as the maximum values of short signed integer is SHRT_MIN and SHRT_MAX respectively. Because of this "short" key word, integer data type takes 2 bytes
instead of 4 bytes.
       Therefore range would be from -32768 to +32767. And finally, in example no. 4 we are using USHRT_MAX for getting the maximum value
of unsigned short integer. And as expected,
the range is from 0 to 65535.
        Note: You can also write unsigned short instead of short unsigned here. The order doesn't matter.
       You can also check the range of long integers
by replacing the keyword short with long.
And in printf instead of using %d, you can use %ld for long signed integer and instead of %u
you have to use %lu. There is one more thing called long long integer. It simply means,
Suppose if long integer is of 4 bytes, then long long integer will be of 8 bytes else if long integer is of 8 bytes, then long long integer would be of 8 bytes only. Because the maximum limit is upto 8 bytes you can say.
         Let's try to summarize the information that whatever we had learnt till now. sizeof(short) is less than or equal to sizeof(int) and is less than 
or equal to sizeof(long) Writing signed int some_variable_name; is equivalent to writing
int some_variable_name;
%d is used to print "signed integer"
%u is used to print "unsigned integer"
%ld is used to print "long integer"
which is actually equivalent to signed long integer.
%lu is used to print "unsigned long integer"
%lld is used to print "long long integer"
%llu is used to print  "unsigned long long integer"
OK friends, this is it for now…….

कोई टिप्पणी नहीं:

एक टिप्पणी भेजें

Data Types in C (part 2)

        Data Types in C.......             Today, we will continue our discussion on integer data type. Our outline for today...