Modifiers in C++
Modifier in C++ are used to alter the meaning of a predefined data types. It changes the storage space of the data type i.e. it either increases or decreases the storage space. Different data types and their modifiers can be listed as follows:
Integer Type Modifier in C++
There are mainly 4 types of Integer type modifiers. These are:
- Signed: Signed modifier accepts both positive and negative values. It has the syntax as:
signed int variable_name; or signed int variable_name=value;For example:
C++12signed int x=5;signed int x=-5;
In the above example, the signed integer variable x can hold both positive and negative values.
- Unsigned: Unsigned modifier accepts only positive values. It has the syntax as:
unsigned int variable_name; or
unsigned int variable_name=value;
C++12unsigned int x=5;unsigned int x=-5;
In the above example, the unsigned integer variable x can hold positive value only. Hence, the 2nd example is wrong because it assigns negative value to x.
- Long: Long is used to increase the size of the integer data type to 2 more bytes. It has the syntax as follows:
long int variable_name; or long int variable_name=value;
12int x;long int x;
In the 1st example, x occupies 2 bytes of memory, but in the 2nd example x occupies 4 bytes of memory.
- Short: In the different operating system, integer variable takes different memory space. To make it fixed, short is used. It has the syntax as:
short int variable_name; or short int variable_name=value;
C++12int x;short int x;
In the 1st example, x may take 2 bytes or 4 bytes. But, in the 2nd example, x will always take 2 bytes only.
Floating Point Modifier in C++
Double is an another floating point data type and there is only one floating point modifier i.e. long. It has the syntax as:
long double variable_name;
long double variable_name=value;
long double x;
Character Type Modifier in C++
Character data type has 2 modifiers – signed and unsigned. Signed modifier represents the range from -128 to 127 and unsigned modifier ranges from 0 to 255. It has the syntax as follows:
signed char variable_name;
signed char variable_name=’charcter’;
unsigned char variable_name;
unsigned char variable_name=’charcter’;
Important: Modifiers can be combined as follows:
- signed can be combined with short. For example:
C++1signed short x;
- signed can be combined with long. For example:
C++1signed long x;
- unsigned can be combined with short. For example:
C++1unsigned short x;
- unsigned can be combined with long. For example:
C++1unsigned long x;
Type of qualifier in C++
Qualifiers in C++ are used to provide additional information about the data type they precede. It is of mainly 3 types:
- Volatile: when a variable is declared as volatile, this means the variable value may be changed in a way which is not explicitly specified in the program.
- Const: when a variable is declared as const, then its value cannot be changed during the execution of the program.
- Restrict: Pointer variable uses the restrict qualifier. When a pointer is declared as restrict, it means it can access only that variable which is pointed by the pointer.