Skip to content
Related Articles

Related Articles

C++ Modifier Types

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Mar, 2022

Modifiers are used in C++ to change or give extra meaning to database 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
  • long
  • short

C++ Modifier Types

These modifiers can be used with the following Built-in Data Types or User-defined data types.

Built-in Data Types: These datatypes are already predefined in the compiler.

S No.

Data Type

Size(in bytes)

1. char 1
2. int 4
3. float 4
4. double 8
5. bool 1

User-Defined Data Types:

These data types are defined by the user. They are more complex than pre-defined 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.

Below is the C++ program to implement the above approach-

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.
‘c’ is a zero-valued integer.

Below is the C++ program to implement the above approach-

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: Short modifies the minimum values that a data type can hold. It is used for small integers that lie in the range from −32,767 to +32,767

Example:

short int x = 4590;

Below is the C++ program to implement the above approach:

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: Long modifies the maximum values that a data type can hold. It is used for large integers that is in the range from -2147483647 to 2147483647

Example:

long int y = 26936;   

Below is the C++ program to implement the above approach-

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:

1. The modifiers for a datatype can be combined. For example, signed/unsigned can be used with long/short modifiers. Below is the C++ program to demonstrate the above concept-

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-

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

The following table shows various data types with modifiers and their size in bytes:

1. For char: This 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. For Integer:

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. For float and 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

C++ Type Qualifiers: 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.
  2. restrict: The modifier volatile tells the compiler that a variable’s value can be changed in ways that are not explicitly defined by the program.
  3. volatile: A pointer qualified by restricting is initially the only means by which the object it points to can be accessed. Only C99 adds a new type qualifier called restrict.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!