Introduction to Integer Types in C++::Part-11

1

INTEGER TYPES

An integer is a whole number: 0, 1, -1, -2, 3, -3, etc. An unsigned integer is an integer that is not negative: 0, 1, 2, 3, etc. C++ has the following nine integer types:

                    char                              short int                           unsigned short int

                    signed char                  int                                     unsigned  int

                    unsigned char             long int                             unsigned long int

The differences between these nine types is the range of values that they allow. These ranges depend, to some extent, upon the computer system being used. For example on most DOS PCs, int ranges between the values -32,768 and 32,767, while on most UNIX workstations it ranges between the values  -2,147,483,648 and 2,147,483,647. The ‘’int’’ part may be omitted from the types names short int, long int, unsigned short int, unsigned int, and unsigned long int.

The program in the example below prints the ranges of all the integer types on your machine. These limits, named SCHAR_MIN, LONG_MAX, etc are constants stored in the header file <limits.h>, so the following preprocessor directive

                    #include <limits.h>

is needed to read them.

EXAMPLE: Integer Types Ranges

This program prints the limits to the ranges of the various integer types:

        #include <iostream.h>

        #include <limits.h>

        // Prints the constants stored in limits.h:

        main()

        {

                 cout <<  ‘’minimum char  =  ‘’  <<  CHAR_MIN  <<  end1;

                 cout <<  ‘’maximum char  =  ‘’  <<  CHAR_MAX  <<  end1;

                 cout <<  ‘’minimum short = ‘’  <<   SHRT_MIN  <<  end1;

                 cout <<  ‘’maximum short = ‘’  <<   SHRT_MAX  <<  end1;

                 cout <<  ‘’minimum int =  ‘’  <<  INT_MIN  <<  end1;

                 cout <<  ‘’maximum int =  ‘’  <<  INT_MAX  <<  end1;

                 cout <<  ‘’minimum long =  ‘’  <<  LONG_MIN  <<  end1;

                 cout <<  ‘’maximum long =  ‘’  <<  LONG_MAX  <<  end1;

                 cout <<  ‘’minimum signed char =  ‘’  <<  SCHAR_MIN  <<  end1;

                 cout <<  ‘’maximum signed char =  ‘’  <<  SCHAR_MAX  <<  end1;

                 cout <<  ‘’maximum unsigned char =  ‘’  <<  UNCHAR_MAX  <<  end1;

                 cout <<  ‘’maximum unsigned short =  ‘’  <<  UNSHRT_MAX  <<  end1;

                 cout <<  ‘’maximum unsigned  =  ‘’  <<  UINT_MAX  <<  end1;

                 cout <<  ‘’maximum unsigned long =  ‘’  <<  UNLONG_MAX  <<  end1;

                 return 0;

        }

                  OUTPUT:

                  minimum char = 128

                  maximum char = 127

                  minimum short = 32768

                  maximum short = 32767

                  minimum int = -2147483648

                  maximum int = 2147483647

                  minimum long = -2147483648

                  maximum long = 2147483647

                  minimum signed char = 128

                  maximum unsigned char = 127

                  maximum unsigned char = 255

                  maximum unsigned short = 65535

                  maximum unsigned  = 4294967295

                  maximum unsigned long =  42949672295

This output is from a UNIX workstation. It shows that, on this system, there are really only six distinct integer types:

                     char                                      range – 128 to 127 ( 1 byte )

                     short                                    range-32,768 to 32,767 ( 2 bytes )

                     int                                          range-2,147,483,648 to 2,147,483,647 ( 4 bytes )

                     unsigned char                   range 0 to 255 ( 1 byte )

                     unsigned short                 range 0 to 65,535 ( 2 bytes )

                     unsigned                                        range 0 to 4,294,967,295 ( 4 bytes )

You can tell, for example, that short integers occupy  2 bytes (16 bits) on this machine, because the range 32,768 to 32,767 covers 65,536 = 216 possible values. (Recall that a byte is 8 bits, the standard storage unit for characters.)

On a PC running Borland C++, this program produces the same ranges except for int and unsigned which have

                     int                                                             range -32,768 to 32,767 (2 bytes)

                unsigned                                                range 0 to 65,535 (2 bytes) 

Ref. By: JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science, University of Richmond

——————— Thanks

Share.

About Author

TN365

1 Comment

  1. Avatar
    sorfuddin ahmad on

    actually i don’t about programing but i think when any guys get a good programmer then your post is effective for their……….

Leave A Reply