Open In App
Related Articles

C++ Type Modifiers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Modifiers are used in C++ to change or give extra meaning to already existing data types. It’s added to primitive data types as a prefix to change their meaning. A modifier is used to change the meaning of a basic type so that it better matches the requirements of different circumstances.

Following are the C++ data type modifiers:

  • signed
  • unsigned
  • short
  • long

Modifiers in C++

These modifiers can be used with the following Built-in Data Types.

1. signed Modifier

Signed variables can store positive, negative integers, and zero.

Example:

signed int a = 45;
signed int b = -67;
signed int c = 0;

Here,

  • ‘a’ is a  positive valued integer.
  • ‘b’ is a  negative valued integer.
  • ‘c’ is a zero-valued integer.

Example:

C++

// C++ program to demonstrate
// the signed modifier
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    cout << "Size of signed int : " <<
             sizeof(signed int) <<
             " bytes" << endl;
    cout << "Size of signed char : " <<
             sizeof(signed char) <<
             " bytes" << endl;
 
    return 0;
}

                    

Output
Size of signed int : 4 bytes
Size of signed char : 1 bytes

Note: The int datatype is signed by default. So, int can be directly be used instead of signed int.

2. unsigned Modifier

Unsigned variables can store only non-negative integer values.

Example:

unsigned int a = 9;
unsigned int b = 0;

Here,

  • ‘a’ is a  positive valued integer.
  • ‘b’ is a zero-valued integer.

Example:

C++

// C++ program to demonstrate
// the unsigned modifier
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    cout << "Size of unsigned int : " <<
             sizeof(unsigned int) <<
             " bytes" << endl;
    cout << "Size of unsigned char : " <<
             sizeof(unsigned char) <<
             " bytes" << endl;
    return 0;
}

                    

Output
Size of unsigned int : 4 bytes
Size of unsigned char : 1 bytes

Difference Between Signed and Unsigned Modifiers

  • The signed value of the signed variable is stored in the allocated memory. However, the signed value is not stored by the unsigned variables.
  • The sign takes 1 bit extra. So, if the unsigned value is being used then one-bit extra space is used to save the value of a variable.
  • The range of values for unsigned types starts from 0. For example, for unsigned int, the value range is from 0 to 4,294,967,295. However, for signed int, the value range is from -2,147,483,648 to 2,147,483,647.

Note: signed and unsigned modifiers can only be used with int and char datatypes.

3. short Modifier

The short keyword modifies the minimum values that a data type can hold. It is used for small integers that lie in the range of −32,767 to +32,767.

Example:

short int x = 4590;

Example:

C++

// C++ program to demonstrate
// the short modifier
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    cout << "Size of short int : " <<
             sizeof(short int) <<
             " bytes" << endl;
    return 0;
}

                    

Output
Size of short int : 2 bytes

Note: The short int can be written as short also. They are equivalent.

4. long Modifier

The long keyword modifies the maximum values that a data type can hold. It is used for large integers that are in the range of -2147483647 to 2147483647

Example:

long int y = 26936;

Example:

C++

// C++ program to demonstrate
// the long modifier
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    cout << "Size of long int : " <<
             sizeof(long int) <<
             " bytes" << endl;
    cout << "Size of long double : " <<
             sizeof(long double) <<
             " bytes" << endl;
    return 0;
}

                    

Output
Size of long int : 8 bytes
Size of long double : 16 bytes

Note: The long int can be written as long also. They are equivalent.

Important Points about Data Type Modifiers

1. The modifiers for a data type can be combined.

For example, signed/unsigned can be used with long/short modifiers. Below is the C++ program to demonstrate the above concept.

Example:

C++

// C++ program to demonstrate
// that modifiers can be combined
// together
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    cout << "Size of signed long int : " <<
             sizeof(signed long int) <<
             " bytes" << endl;
    cout << "Size of unsigned short int : " <<
             sizeof(unsigned short int) <<
             " bytes" << endl;
    return 0;
}

                    

Output
Size of signed long int : 8 bytes
Size of unsigned short int : 2 bytes

2. The long modifier can be used as twice as long long.

It is used for even larger numbers than long. However, long long-type modifiers can only be used with int. Below is the C++ program to demonstrate the above concept.

Example:

C++

// C++ program to demonstrate
// the long long modifier
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    cout << "Size of long long int : " <<
             sizeof(long long int) <<
             " bytes" << endl;
    return 0;
}

                    

Output
Size of long long int : 8 bytes

Various Data Types with Modifiers and Their Size in Bytes

1. Character Data Type (char)

Character datatype only allows signed and unsigned modifiers. When only char is used instead of signed char or unsigned char, this type is known as plain char. When performing numerical computations, it is preferred to utilize signed char or unsigned char instead of plain char. Character values should only be stored as plain char.

S No.

Modifier

Size(in Bytes)

1.

char

1

2.

signed char

1

3.

unsigned char

1

2. Integer Data Type (int)

S No.

Modifier

Size(in Bytes)

1.

int

4

2.

signed int

4

3.

unsigned int

4

4.

short

2

5.

signed short

2

6.

unsigned short

2

7.

long

8

8.

signed long 

8

9.

unsigned long

8

3. Floating Point (float) and Double Precision Floating Point (double)

Double type can be used with the long modifier.

S No.

Modifier

Size(in Bytes)

1.

float

4

2.

double

8

3.

long double

16

Type Qualifiers in C++:

Type qualifiers are used to provide more information about a value while also guaranteeing that the data is used correctly.

  1. const: Objects of type const cannot be altered during execution. Const objects cannot be modified by your program while it is running.
  2. volatile: The modifier volatile tells the compiler that a variable’s value can be changed in ways that are not explicitly defined by the program. The compiler is informed by the modifier volatile that a variable’s value might change in ways that aren’t clearly stated in the program.
  3. restrict: A pointer qualified by restricting is initially the only means by which the object it points to can be accessed. The object restricts links that can only initially be accessed via a pointer qualified by it. Restrict is a new type of qualifier that is only added in C99.


Last Updated : 18 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads