Wednesday, 8 July 2015

Data Type Modifiers : C++


Data Type Modifiers



Since we are already familiar with the basics of data types, let us see what data type modifiers are.

As the name suggests, data type modifiers change the behavior of data types, to suit some specific needs. These are used before the name of data type at the time of declaration. C++ provides the following data type modifiers:

signed:It means that a variable of given data type can hold both positive and negative values. If you don't specify it, the variables can store both type of values by default. Ex:

signed int a;

Assuming integer to consume four bytes, it can store values ranging from -2,147,483,647 to +2,147,483,647.

unsigned:It means that a variable can store only positive values. This is useful when you know that a variable will not have non-negative values, such as roll numbers or marks of students. This doubles the range of positive values that a variable can have. Ex:

unsigned int b;

Assuming integer to be consume four bytes, it can store values ranging from 0 to +4,294,967,295.

Note that, since characters are stored in memory in the form of their ASCII values, a one-byte signed character can have values from -127 to +127, whereas an unsigned character can have values from 0 to +255.

To see the ASCII value of a character, you can use the following statements:
char c='a';
int num=c;
cout<<"The ASCII value of "<<c<<" is "<<num;

short:A short integer can be declared as:
short int a;
or
short a;

A short integer is atleast two bytes and at most as big as int. It's size varies from compiler to compiler.

More than one data type modifier can be used in succession. So, a two byte, signed short can have values from -32767 to +32767.

long:Just because there is something called short, it is logical to have something called long. ;)
A long integer can be declared as:
long int xyz;
or
long xyz;

A long is atleast four bytes and atleast as big as int. There is also a data type called long long int, which occupies atleast eight bytes.

Here also, signed and unsigned modifiers can be used.

The general rule for short and long is:
sizeof( short integer ) <= sizeof( integer ) <= sizeof( long integer )

 

These modifiers can be used with other data types (float and double) too. Note that, C++ eliminates the use of long float because it is nothing but double data type.

All these data types discussed so far (int,char,float,double) are called FUNDAMENTAL DATA TYPES. They are very basic in nature and are not composed of any other data type. There is also another data type called void. It is used for those functions that do not return a value. No variable of type void can be declared.

There are DERIVED DATA TYPES too, that are composed of these fundamental data types. Examples include Arrays, Pointers, Union, Classes etc. We will not go into their details right now, as they themselves are big topics.

Tip: To find out the range of any data type if you know the amount of memory it takes, follow these steps:

Let us assume that integer occupies four bytes. Since one byte=8bits and each bit can have one of the two values (either 1 or 0), the total number of different values represented by 8*4=32 bits=2^32 (two raised to the power 32)= 4,294,967,296. Each number is represented by a unique binary representation. If it is unsigned, it can store values from 0 to (4,294,967,296-1). If it is signed, it can store values from -(4,294,967,296/2-1) to +(4,294,967,296/2-1).

To know the size of a given data type or variable in your compiler, use the following statements:

int a;

cout<<sizeof(a);

cout<<sizeof(int);

Also, the sign ^ is not used as raised to the power in C++. It is a bitwise XOR operator. Let's not go deep into it now.

That's it for now. Keep reading, and do comment and ask your queries!

No comments:

Post a Comment