Saturday 4 April 2020

Type Modifiers in C++


Type Modifiers
The Basic Data Types may have modifiers preceding them to indicate special properties of the objects being declared. These modifiers change the meaning of the Basic data types to suit the specific needs.
These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in combination, e.g., unsigned log int.

1. Modifiers for char  Data Type
char data type can be qualified as either signed or unsigned, both occupying one byte each, but having different ranges. A signed char is same as an ordinary char and has a range from -128 to +127; whereas an unsigned char has a range from 0 to 255. By default char is unsigned.
e.g.:         main ( )
                {
                            char ch = 291;
                            cin>>ch;
                }
    output:             35        #
Here ch has been defined as a char, and a char cannot take a value bigger than +128. That is why assigned value of ch, 291 is considered to be 35 (291-128).

2. Modifiers for int  Data Type
Integer quantities can be defined as short int, long int or unsigned int. short int occupies two bytes of space, whereas long int occupies 4 bytes of space. A signed int has the same memory requirements as an unsigned int (or a short int or a long int), the leftmost bit is reserved for the sign. With an unsigned int, all the bits are used to represent the numerical value. The unsigned qualifier can also be applied to other qualified int. For example, unsigned short int or unsigned long int. By default, modifier assumed with integers is signed.

3. Modifiers for double and float  Data Type
Modifier long is used with double data type but not with float. Long double occupies 10 bytes of memory space (usually, but actual size depends on implementation and hardware platform).

Data Type               Range                                           Bytes             
signed char              -128 to + 127                               1                    
unsigned char          0 to 255                                        1                    
short signed int       -32768 to 32767                           2                    
short unsigned int   0 to 65535                                    2                    
long signed int        -2147483648 to +2147483647    4                    
long unsigned int    0 to 4294967295                          4                    
float                         -3.4e38 to 3.4e38                         4                    
double                     -1.7e308 to +1.7e308                   8                    
long double             -1.7e4932 to 1.7e4932                 10                  

No comments:

Post a Comment