Now that we know the various data types used in C++ language, let us see how we can modify these data types to provide more versatility to our code.

We have the following four data type modifiers in C++ :

  1. signed
  2. unsigned
  3. long
  4. short

First let us discuss some imporant points about these modifiers.

  1. If we use ‘signed’ before a variable declaration, it means that the variable can hold both positive and negative values. For example : signed int num = -3;
  2. If we use ‘unsigned’ before a variable declaration, it means that the variable can hold only positive values. For example : unsigned int a = 5;
  3.  By default, all variable declarations are signed. That is why you can simply declare a variable like int a = -10 ; in your programs.
  4. These modifiers are used with only char and int data types. Their use with float data type is not recommended. However, we may apply long modifier to float data type.
  5. ‘unsigned’ is used when we want to represent only positive numbers.
  6. ‘unsigned’ doubles up the range of numbers we can represent using an int or char.

The following tables give you all information about data types, their modifiers and ranges.

1. integer data type

integers can be declared using ‘short’, ‘long’ or simply ‘int’.

For example : short int a = 3; unsigned long int b =62220;

We could also have written short a =3; or unsigned long b= 62220;

TABLE 1 : short

Type Size Range
short 2 bytes -32768 to 32767
unsigned short 2 bytes 0 to 65535
signed short 2 bytes -32768 to 32767

TABLE 2 : int

‘int’ can be  2 or 4 bytes in  size depending upon the system in use.

Type Size Range
int 2 or 4 bytes -32768 to 32767 (2 bytes)
-2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned int 2 or 4 bytes 0 to 65535 (2 bytes)
0 to 4,294,967,295 (4 bytes)
signed int 2 or 4 bytes -32768 to 32767 (2 bytes)
-2,147,483,648 to 2,147,483,647 (4 bytes)

TABLE 3 : long

Type Size Range
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
signed long 4 bytes -2,147,483,648 to 2,147,483,647

2. char data type

char data type can be signed and unsigned. Range is shown in the table below.

Type Size Range
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

3. float data type

As stated earlier, we generally don’t use these modifiers with floats. But we can have long double which is same as double on some machines.

Type Size Range Precision
float 4 bytes 3.4 x 10-38 to 3.4 x 1038 7
double 8 bytes 1.7 x 10-308 to 1.7 x 10308 15
long double 8 or 10 bytes same as double ( 8 bytes)
3.4 x 10-4392 to 3.4 x 104392(10 bytes)
15 (8 bytes)
19 (10 bytes)

So this was all about data type modifiers in C++. Hope you’re clear with the topic. Let us know your queries in comments below.




Tags: , , , , , ,

Leave A Response