Modifiers in C++

Modifiers in C++

Modifiers in C++ are extensions to existing data types and can change the storing capacity and behaviour of the data types and variables. These modifiers precede the data types like int, char, float etc.

Some modifiers are –

  • long
  • signed
  • unsigned
  • short
Modifiers

Modifiers Details

These modifiers also modify the range and size of of the data types that they are preceding. The below list gives you an idea on the same –
Data TypeSizeRange
bool1 byteTrue/False
char1 byte-128 to 127 or 0 to 255
signed char1 byte-128 to 127
unsigned char1 byte0 to 255
short int2 bytes-32,768 to 32,767
signed short int
2 bytes-32,768 to 32,767
unsigned short int2 bytes0 to 65,535
int2 or 4 bytes-32768 to +32768
or -2,147,483,648 to +2,147,483,648
signed int2 or 4 bytes-32768 to +32768
or -2,147,483,648 to +2,147,483,648
unsigned int2 or 4 bytes0 to +65,535
or 0 to +4,294,967,295
long int4 or 8 bytes-2,147,483,648 to +2,147,483,647
or -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
signed long int4 or 8 bytes0 to 4,294,967,295
or 0 to 18,446,744,073,709,551,615
unsigned long int4 or 8 bytes-2,147,483,648 to +2,147,483,647
or -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float4 bytes1.17549e-38 to 3.40282e+38
Double Floating Point8 bytes2.22507e-308 to 1.79769e+308
Long Double Floating Point16 bytes-3.3621e-4932 to +3.3621e-4932
No ValueNot validNot valid
Wide character2 or 41 wide character
The sizes of the variables might be different for various compilers, what we have given are general sizes that are useful for various competitive examinations.
  • int – can be preceded with long, signed, unsigned, short
  • char – can be preceded with signed and unsigned
The following code will print the sizes of various data types and their modifiers –
Run
#include <iostream>
using namespace std;

int main() {
    cout << "Size of char : " << sizeof(char) << endl;
    cout << "Size of int : " << sizeof(int) << endl;
    cout << "Size of short int : " << sizeof(short int) << endl;
    cout << "Size of long int : " << sizeof(long int) << endl;
    cout << "Size of long int : " << sizeof(unsigned long int) << endl; 
    cout << "Size of float : " << sizeof(float) << endl;
    cout << "Size of double : " << sizeof(double) << endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   
    return 0;
}
The following will be the output of the code –
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 8
Size of long int : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4
If you use just the modifier followed by the variable, in C++ the assignment is understood for int, for unsigned, short or long For example writing the following means the same thing and it implies that the variable is an unsigned int type.
unsigned x;
unsigned int y;

Type Qualifiers

Read more about type qualifiers here on this page.
C++ Program to Display Limits
This covers char, int with various modifiers
Run
#include <iostream>
#include <limits.h>
using namespace std;

int main ()
{
    // characters
    cout << "CHAR_BIT     :   " << CHAR_BIT << endl;
    cout << "CHAR_MAX     :   " << CHAR_MAX << endl;
    cout << "CHAR_MIN     :   " << CHAR_MIN << endl;
    cout << "SCHAR_MAX    :   " << SCHAR_MAX << endl;
    cout << "SCHAR_MIN    :   " << SCHAR_MIN << endl;
    cout << "UCHAR_MAX    :   " << UCHAR_MAX << "\n" << endl;

    // integers
    cout << "SHRT_MAX     :   " << SHRT_MAX << endl;
    cout << "SHRT_MIN     :   " << SHRT_MIN << endl;
    cout << "USHRT_MAX    :   " << USHRT_MAX << endl;
    
    cout << "INT_MAX      :   " << INT_MAX << endl;
    cout << "INT_MIN      :   " << INT_MIN << endl;
    cout << "UINT_MAX     :   " << UINT_MAX << "\n" << endl;
    
    cout << "LONG_MAX     :   " << LONG_MAX << endl;
    cout << "LONG_MIN     :   " << LONG_MIN << endl;
    cout << "ULONG_MAX    :   " << ULONG_MAX << "\n" << endl;
    
    cout << "LLONG_MAX    :   " << LLONG_MAX << endl;
    cout << "LLONG_MIN    :   " << LLONG_MIN << endl;
    cout << "ULLONG_MAX   :   " << ULLONG_MAX << endl;


    return 0;
}
Modifiers in C+ Part 1

Output

CHAR_BIT : 8
CHAR_MAX : 127
CHAR_MIN : -128
SCHAR_MAX : 127
SCHAR_MIN : -128
UCHAR_MAX : 255

SHRT_MAX : 32767
SHRT_MIN : -32768
USHRT_MAX : 65535
INT_MAX : 2147483647
INT_MIN : -2147483648
UINT_MAX : 4294967295

LONG_MAX : 9223372036854775807
LONG_MIN : -9223372036854775808
ULONG_MAX : 18446744073709551615

LLONG_MAX : 9223372036854775807
LLONG_MIN : -9223372036854775808
ULLONG_MAX : 18446744073709551615
C++ Program to Display Limits
This covers float, double, long double
Run
#include <iostream>
#include <limits.h>
using namespace std;

int main ()
{
    // Max possible positive value representable
    cout << "Max Float            :   " << FLT_MAX << endl;
    cout << "Max Double           :   " << DBL_MAX << endl;
    cout << "Max Long Double      :   " << LDBL_MAX << endl;
    
    cout << "\n\n";

    // Min possible positive value representable 
    // postive value nearest to zero in decimals
    cout << "Min Float            :   " << FLT_MIN << endl;
    cout << "Min Double           :   " << DBL_MIN << endl;
    cout << "Min Long Double      :   " << LDBL_MIN << endl;
    
    cout << "\n\n";

    // Max possible negative value representable
    cout << "-(Max Float)         :   " << -FLT_MAX << endl;
    cout << "-(Max Double)        :   " << -DBL_MAX << endl;
    cout << "-(Max Long Double)   :   " << -LDBL_MAX << endl;
    
    cout << "\n\n";

    // Min possible negative value representable 
    // negative value nearest to zero decimals
    cout << "-(Min Float)         :   " << -FLT_MIN << endl;
    cout << "-(Min Double)        :   " << -DBL_MIN << endl;
    cout << "-(Min Long Double)   :   " << -LDBL_MIN << endl;



    return 0;
}
Modifiers in C+ Part 2

Output

Max Float : 3.40282e+38
Max Double : 1.79769e+308
Max Long Double : 1.18973e+4932

Min Float : 1.17549e-38
Min Double : 2.22507e-308
Min Long Double : 3.3621e-4932

-(Max Float) : -3.40282e+38
-(Max Double) : -1.79769e+308
-(Max Long Double) : -1.18973e+4932

-(Min Float) : -1.17549e-38
-(Min Double) : -2.22507e-308
-(Min Long Double) : -3.3621e-4932
We can write a program to also display these limits side by side. The program for char/int in various modifiers is below –
Run
#include <iostream>
#include <limits.h>
using namespace std;

int main ()
{
    // characters
    cout << "Char                   :   " << "(" << CHAR_MIN << ", " << CHAR_MAX << ")" << endl;
    cout << "Signed Char            :   " << "(" << SCHAR_MIN << ", " << SCHAR_MAX << ")" << endl;
    cout << "UnSigned Char          :   " << "(" << "0" << ", " << UCHAR_MAX << ")" << endl;

    cout << "\n\n";
    
    // integers
    cout << "Short                  :   " << "(" << SHRT_MIN << ", " << SHRT_MAX << ")" << endl;
    cout << "Unsigned Short         :   " << "(" << "0" << ", " << USHRT_MAX << ")" << endl;
    
    cout << "Int                    :   " << "(" << INT_MIN << ", " << INT_MAX << ")" << endl;
    cout << "Unsigned Int           :   " << "(" << "0" << ", " << UINT_MAX << ")" << endl;
    
    cout << "Long Int               :   " << "(" << LONG_MIN << ", " << LONG_MAX << ")" << endl;
    cout << "Unsigned Long Int      :   " << "(" << "0" << ", " << ULONG_MAX << ")" << endl;
    
    cout << "Long Long Int          :   " << "(" << LLONG_MIN << ", " << LLONG_MAX << ")" << endl;
    cout << "Unsigned Long Long Int :   " << "(" << "0" << ", " << ULLONG_MAX << ")" << endl;

    return 0;
}

Output

Char                   :   (-128, 127)
Signed Char            :   (-128, 127)
UnSigned Char          :   (0, 255)


Short                  :   (-32768, 32767)
Unsigned Short         :   (0, 65535)
Int                    :   (-2147483648, 2147483647)
Unsigned Int           :   (0, 4294967295)
Long Int               :   (-9223372036854775808, 9223372036854775807)
Unsigned Long Int      :   (0, 18446744073709551615)
Long Long Int          :   (-9223372036854775808, 9223372036854775807)
Unsigned Long Long Int :   (0, 18446744073709551615)
We can write a program to also display these limits side by side. The program for float/double/long double in various modifiers is below –
Run
#include <iostream>
#include <limits.h>
using namespace std;

int main ()
{
    // Max/min possible positive value representable
    cout << "Float Positive Range          :   " << "(" << FLT_MIN << ", " << FLT_MAX << ")" << endl;
    cout << "Double Positive Range         :   " << "(" << DBL_MIN << ", " << DBL_MAX << ")" << endl;
    cout << "Long Double Positive Range    :   " << "(" << LDBL_MIN << ", " << LDBL_MAX << ")" << endl;
    
    cout << "\n\n";
    
    // Max/min possible negaive value representable
    cout << "Float Negative Range          :   " << "(" << -FLT_MAX << ", " << -FLT_MIN << ")" << endl;
    cout << "Double Negative Range         :   " << "(" << -DBL_MAX << ", " << -DBL_MIN << ")" << endl;
    cout << "Long Double Negative Range    :   " << "(" << -LDBL_MAX << ", " << -LDBL_MIN << ")" << endl;

    return 0;
}

Output

Float Positive Range          :   (1.17549e-38, 3.40282e+38)
Double Positive Range         :   (2.22507e-308, 1.79769e+308)
Long Double Positive Range    :   (3.3621e-4932, 1.18973e+4932)


Float Negative Range          :   (-3.40282e+38, -1.17549e-38)
Double Negative Range         :   (-1.79769e+308, -2.22507e-308)
Long Double Negative Range    :   (-1.18973e+4932, -3.3621e-4932)

One comment on “Modifiers in C++”