Thursday, 6 March 2014

Constant Declaration and Memory representation of Data Types.

Constant Declaration and Memory representation of Data Types


Constant and its Declaration:


The value given to the variable is called constant. A constant value does not change during the program execution. A variable that remains hold its value during the program execution is called constant variable. In many programming numerical constants, and scientific formula constants are used, e.g. the value of Ï€ (pi) is always equal to 3.14, the value of gravitational acceleration "g" is always equal to 9.81, and the total number of days in a non leap year is always 365 etc. They are called constant values. A variable that holds a constant value is called constant variable. Constant variables can be declared by using a keyboard Const precede the data type of the variables and they should be initialised with values. Without initialisation a constant variable cannot be declared Following is the general syntax of constant variable declaration:


               const Data-type-name <Variable-name = value>;

In the above syntax Data-type-name is the data type of the variable you want to declare, Variable-name is the name of Variable. and the value is an expression involving only constant quantities that gives to the variable. Following are some examples of constant variables:

                                     const float Pi = 3.14;
                                     const int days_in_year = 365;
                                     const int month_in_year = 12;
                                     const char ch = 'c';
                                     const float  = 10.5;

The above variables are constant variables because they have initialised with constant values.







Limitation of variables ( data type ranges ) in C++:



Each variable has a maximum and minimum range for value. When a variable is declared then memory space is allocated to it in the form of bits. A bit is a memory unit representing a single binary digit 0 or 1. The variable minimum and maximum ranges are shown in the following table.

Data Type Name
Bytes
Minimum Value
Maximum Value
Char
1
-128
127
Unsigned char
1
0
255
Int
2
-32768
32767
Unsigned int
2
0
65535
Unsigned short int
2
0
65535
Long int
4
-2147483648
2147483647
Unsigned long int
4
0
2147483647
Float
4
-3.4E+38
3.4E+38
Double
8
-1.7E+308
1.7E+308
Long double
10
-1.7E+308
1.7E+308
Bool
1
False 0
True 1
Wchar_t
2

255 or greater

Memory Representation of Data Types:


The general formula that represents the maximum range of variable is 2"-1 while the general formula that represents the range of variable is -2"-1. Here n represent the total number of bits allocated in memory for  a declared variable.


  • Memory Representation of Integer Variables:

When you declared a variable as an integer then it takes 2 bytes ( 16 bits ) space in memory. The following table shows memory representation of integer variable.

       1    2   3    4     5     6    7    8    9    10   11  12  13  14   15  16 
Sign
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
Since the value may either positive or negative. The first bit holds the information about the entered value either the value is negative or positive. If the entered value is positive then this bit stores positive sign (+) and when the entered value is negative then this bit stores negative sign (-). The remaining 15 bits from 2 to 16 represent the magnitude of the data. the maximum and minimum ranges of an integer variable can be calculated by using the above two formula as.

Total Number of bits for Data
Maximum range of integer
Minimum range of integer
N = 15
215-1 = 32767
-215-1 = -32768
This memory representation shows that an integer variable can hold maximum value from 0 to 32767 and minimum value -1 to -32767. If you want to store data out of these ranges then in such case int data type will fail. For this purpose you will declare long int variable.

  • Memory Representation of Long Integer Variables:


The long integer variable takes 4 bytes ( 32 its ) space in memory. The following table shows the memory representation of long integer variable.
        
   1      2 3 4 5 6 7  8 9 10 --------------------------------26 27 28 29 30 31
Sign
/




















/
/
/
/
/
/
/
/
Here the first bits represent sign while the remaining 31 bits from 2  to 31 represent the magnitude of the data. The maximum and minimum ranges of long integer variable can be calculated by using the following formula.

Total number of bits for data
Maximum range of integer
Minimum range of integer
N = 31
231 -1 = 2147483647
-231 -1 =- 2147483648
The above memory representation shows that a long integer variable can hold the maximum value from 0 to 2147483647 and minimum value from -1 to -2147483648.

  • Memory Representation of Character Variables:


Character variable takes 1 byte ( 8 bytes ) space in memory. The following table shows the memory representation of a character variable.

           1 2  3 4  5 6  7 8 
Sign
/
/
/
/
/
/
/
/

The first bit represent sign while the remaining 7 bits from 2 to 8 represent the magnitude of the data. The maximum and minimum ranges of a character variable can be calculated by using the following formula.

Number of
bits
Maximum range of
 unsigned int
Minimum range of
unsigned int

    N = 1
   27 -1 = 127
     -27 -1 = -128

The above memory representation shows that a character variable can hold the maximum range of value up to 127 and minimum range value up to -128.

  • Memory Representation of Unsigned Integer Variables:


Unsigned means no sign for value. When you declared a variable as an unsigned then that variable will always hold a positive value. When you give the negative value to an unsigned variable then unsigned variable ignores the negative value and hold another default value. The entire bits of unsigned variable represent the magnitude of the data i.e. no bit will be allocated for sign purpose.
 1   2   3    4  5    6   7    8   9  10  11 12 13141516
/
/
/
/
/
/
/
/
/
/
/

/
/
/
/

Since in unsigned integer variable the entire 16 bits represent the magnitude of the data then the maximum and minimum ranges of unsigned integer can be calculated as:

Number of bits
Maximum range of
unsigned int
Minimum range of
unsigned int
     N = 16
       216 -1 = 65535
     0








Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

0 comments:

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com