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.

modifiers in c++

Some modifiers are –

  • long
  • signed
  • unsigned
  • short

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 – [table id=551 /] 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;
C++ Program to Display Limits
This covers char, int with various modifiers
Modifiers in C+ Part 1
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;
}

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)

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

One comment on “Modifiers in C++”