C++ Type modifiers

Type modifiers are used to providing a special meaning (behaviors) of inbuilt data type variables. C++ are provides in a few keyword to do this task. Those keywords are applied (include) before the inbuilt data type they can change its properties and storage capacity (increase and decrease size). This are following types.

signed

By default all most (inbuilt) data type are signed type. there are capability to storing the value of negative and positive value. basically there is an optional because by default every inbuilt type are (signed type).

unsigned

unsigned modifier are used provide capability to store positive values. that are mostly used in c++ programming.

long

long modifier are used to increase the size of inbuilt data type. Default behaviour of (long) modifiers are as integers. see an example.

#include<iostream>
using namespace std;

int main(){
  long a=19.23,b=90;
  cout<<"long a :"<<a<<" long b :"<<b<<endl;
  return 0; 
}
output
long a :19 long b :90

short

short modifier are used to decrease the size of inbuilt data type. Note that default behaviour of (short) modifiers are as integers.

Data Type Data Size (bytes) Data Range
short, short int, signed short int 2 -32768 to 32767
unsigned int 2 0 to 4294967296 (2^(4*8))
signed int, int, long int 4 -2147483648 to 2147483647
unsigned short int 2 0 to 65536 (2^(2*8))
unsigned long long int 8 0 to 1.8446744e+19 (2^(8*8))
signed long long int, long long int 8 -9.223372e+18 (- 2^(63)) to (9.223372e+18) -1
char, signed char 1 -128 to 127
unsigned char 1 0 to 255
long double 12

Program to display data size.

//size of modifier in c++ programming
#include<iostream>
using namespace std;
int main(){
  cout<<"signed char :"<<sizeof(signed char)<<endl;
  cout<<"unsigned char :"<<sizeof(unsigned char)<<endl;
  cout<<"signed int :"<<sizeof(signed int)<<endl;
  cout<<"unsigned int :"<<sizeof(unsigned int)<<endl;
  cout<<"short int :"<<sizeof(short int)<<endl;
  cout<<"unsigned short int :"<<sizeof(unsigned short int)<<endl;
  cout<<"short :"<<sizeof(short)<<endl; //default integers (2 bytes)
  cout<<"long :"<<sizeof(long)<<endl; //default integers (4 bytes)
  cout<<"long double :"<<sizeof(long double)<<endl;
  cout<<"long int :"<<sizeof(long int)<<endl;
  cout<<"long long int :"<<sizeof(long long int)<<endl;
  cout<<"float :"<<sizeof(float)<<endl;
  cout<<"double :"<<sizeof(double)<<endl;
  return 0; 
}
Output
signed char :1
unsigned char :1
signed int :4
unsigned int :4
short int :2
unsigned short int :2
short :2
long :4
long double :12
long int :4
long long int :8
float :4
double :8

© 2019, regularcodes.com, All rights reserved