modifier in C++

Here we will discuss following, modifier in C++


modifier in C++

Means to modify. We use the modifier in C++ with the built in data type (except void). Using a modifier, you can increase the size of a variable or determine a variable which type of input will be taken by the end user,

 

Why we use modifier in C++?

It can be understood in such a way that the information given by the user is more than the size of the variable size In this situation, the variable can not hold that information, in that case the modifier can be used with that data type .

let’s see in other words

for example, using modifier in C++ with int data type, we can store large form of number(maybe mobile no) or define a condition where we need to store only positive number e.g. a student’s rollno or age always a positive values. so we can use modifier with them. So that the end user could give the right input.

SYNTEX:

MODIFIER-TYPE DATA-TYPE VARIABLE_NAME;

modifier in C++ are following type

signed modifier

This type of modifier stores both negative and positive values. By default an int data type is a signed type modifier which stores both types of value.

SYNTEX:-

signed int variable_name;
signed char variable_name;

Example:-

Both initialization are equal

int a = 5, b= -10;
signed int a = 5, b= -10;

we can use this type of modifier with both data type int and char.

 

unsigned modifier

By using it with any data type, we store only positive values in a variable. char data type by default is unsigned type modifier.

SYNTEX:-

unsigned int variable_name;
unsigned char variable_name;

Example:-

unsigned int a = 5; // possible
unsigned int b = -10;// not possible, garbage value printed

 

long modifier

SYNTEX:-

long int variable_name;
long int mobile;

Using this modifier you can increase the size of the variable. e.g. –

In generally, the size of an int data type is 2 byte but long type modifier is used with int then long int size will be 4 byte. long is used to store a large value.

It is used only with int data type.

 

short modifier

This is the opposite of the long modifier. Using long type modifier with int data type decreases the int size of int.

SYNTEX:-

short int variable_name;

signed, unsigned and short, used with both data type int and char while long is used with double int.

Signed, unsigned can be used with short and long too

signed short int variable_name;
signed long variable_name;

 

Store a mobile number using long with int data type

The long type modifier has been used with the int data type to store mobile no (which is 10 digit, can not do simple int store) in the given program-

#include<iostream.h>
#include<conio.h>

 void main()
  {
    long int m1=8989898989; // with modifier
    int m2 = 8989898989; //without modifier
 
     clrscr();
    cout<<"Mobile: "<<m1<<endl;
    cout<<"Mobile: "<<m2;
    getch();
  }
OUTPUT:-
Mobile: 8989898989
Mobile: 723;



previous-
variable and their types

next-enumereated data type in C++