:. Basics
-Home
-History of C++
-Data Types in C++
-Arrays
-Basics
-Classes & Objects
-Files
-Inheritance
-Strings
-Structures
-Operator Overloading

:. Xtraas

-Links
-Sign Guestbook


:: Data Types in C++::


Variables are program entites whose values may vary as the program is being executed (simple meaning right?).
Think of your computer's memory as a collection of tiny boxes. All the data on your computer is stored in these boxes in the form of Bytes. What happens if the data is too big to fit into one single box?Then we can split the data over more than just a single box,we can fill the data in subsequent boxes or in boxes that are not located one after the other as shown in the following figure.





What kind of data are we talking about? Well, computers talk 1s and 0s,so most of the data will be numbers, there are two types of numbers, namely integers (eg.1,2,3,4 and so on) and those with decimal points (i.e. fractal parts eg. 1.3, 2.5 and so on ). Data can also be in the form of characters i.e a,d,g etc. we can also represent numbers as characters by enclosing them in single quotes eg. �1�, �2.5� and so on.

In C++ we use three different types of data types. Data type is a term that tells us about the kind of data that a variable contains. For instance, if the data type of a variable is �int� we know that this variable contains a whole number. These data types can be organized as shown below.



Here is more on data types:

Keyword

Numerical Range

Low                     High

Digits of precision Bytes Occupied
char -128                    127 n.a. 1
short -128                    127 n.a. 1
int -32,768               32,767 n.a. 2
long / long int -2,147,483,648   2,147,483,647 n.a. 4
float 3.4 * 10-38       3.4 * 1038 7 4
double 1.7 * 10-308     1.7 * 10308 15 8
long double 3.4 * 10-4392    3.4 * 104392 19 10

The table above shows the following:
  1. Keyword - it is the word that is used to declare a variable of a particular data type, for instance �int a;� declares the variable a, which is of type �int�.
  2. Numerical Range - it tells us the range of values which the variable of a particular data type can accommodate, for instance a (from keyword example) can only hold a value between �32,768 and 32,767. Above and below this range we get junks values, i.e. if we try to assign 32,768 to a, a will end up with a junk value ( any value other than the intended one) . We can double the given high range of short,int and long by adding the keyword �unsigned� before them. By doing this we prevent them from accepting negetive values, for instance unsigned int will have a range between 0 to 65355.
  3. Digits of Precision - it tells us the maximum possible precison for a variable. Precision is the number of digits that a variable can accommodate to the right of the decimal point. If a had been of type float,then we could have assigned values like .123456 (precision of 6), .54321 (precision of 5).
  4. Bytes of memory - it tells us how many bytes of memory each data type will occupy. This may vary on different hardware architectures (eg. The number of bits allocated to data type int on an IBM PC clone running Microsoft Windows may be different from one running UNIX).


Declaring Variables Declaring a variable is the process by which we create a variable.C++ gives programmers great freedom when it comes to declaring variables, variables can be declared anywhere in the program as long as they are declared before actually being used.

Storing Values in Variables
Alright , now that you have declared your variable, what use will they be if you cannot store anything in them! The following C++ program demonstrates the various ways of assigning values to variables.

#include"iostream.h"
#include"conio.h"
//iostream.h, conio.h and such files are normally //enclosed in angular braces '<' & '>' void main()
{
clrscr();
//this is to clear the screen of all
//previous output so you get a clear screen
int a;
//this is one way of delaring a variable
int b=9;
//this how you can declare a variable and
//assign it a value in the same line

a=1;
//you can assign a value to a variable
//after it has been declared
int sum=a+b;
//you can also assign the values of
//an arithemetic expression to a variable

int fillme;
cout<<"\nEnter a number :" ;
//you can also get the user
//to give a value to the
//variable
cin>>fillme;

cout<<"\nYou Entered: "<< fillme;
//the values of the
//variable can be displayed
//in this way

getch(); }


Please Note:
The actual size of the data types is depends on the compiler and the platform you are using, use the sizeof to determine the exact size of the data types.

Terms Used Byte - it�s a lot less painful than a Bite, a Byte is made up of 8 Bits,a bit is the smallest unit of memory . Most computers today have Hard Disk Space in Giga Bytes (GB), 1GB=1024MB, MB is short for Mega Byte, 1 MB = 1024 Bytes.

IBM PC Clone - many computers used in homes and offices today follow the architecture of the IBM PC. Unlike other manufacturers like Apple, IBM allowed its architecture to be copied (cloned) by other manufacturers.


 

© 2006 Austinium Webworx