Friday 8 February 2013

Data Handling

Basic Data Types



C++ supports a large number of data types. Built in data type integers which are supported by C++ are integers, floating point and characters.

These are summarised in the table given below


TypeByteRangeDescription
int2-32768 to +32767Small whole number
long int4-2147483648 to +2147483647Large whole number
float43.4x10-38 to 3.4x10+38Small real number
double81.7x10-308 to 1.7x10+308Large real number
long double103.4x10-4932 to 3.4x10+4932Very Large real number
char10 to 255A Single Character


Variables

Variables are the location in computer memory where program stores the data and it is given a symbolic name for a easy reference.The variable can be used to hold different value on different times.

For example if we write this :-  A = 55 , than the value 55 is assigned to the Variable A.. 

Now after some time we write this :- A=78 , now the value of the Variable A becomes 78 and the previous value is now erased from the Variable A.

You can use any Word to assign a Value.
Declaring a Variable

Declaring a variable is easy. Before Declaring a variable we must declare the type of Variable.

For example if you are going to store a decimal number into a variable then you must declare it's type to be " Float " this makes compiler to make an appropriate space for storing the data.


Some Examples:-

1. If you want to assign an integer. For eg. 459 than type this.

 Int x;
  x=459;

2.If you want to store a Decimal Number. For eg. 459.87

Float x;
x=459.87

This process of assigning a value to Variable is Called Initializing.


Input/Output ( I/O)


C++ support the facility to take input while running the program.These can be used to feed new data in computer.


Following Stream is used for taking input/output in C++

Cin>>                                                                         //this is used for taking input.
Cout<<                                                                      //this is used for output.


Well don't forget to put ">>" and "<<" after cin and cout respectively. These are must. "<<" & ">>" and are operators which must be used with Cin and Cout.

Many people get confused about which operator to be used after cin and cout. The Best way to learn this is :-

Cout means outputting the data.. and the operator  " << " is also looks like that it is giving out something hence use "<<" with Cout and ">>" with Cin.


Now some exercise is for you..

Question 1. what is the size of word "Computer" in computer memory??
Question 2. Which data type will you declare for this number --> 45.76 ?? hint--> int or float??
Question 3. is this statement is written correctly??
                  cin<< what the F*** ???

Type Conversion

Now you know well about C++, so it's time to learn something new.

Type conversion is a process in which a pre-defined type of expression is converted into another type is called conversion.

There are two type of conversions in c++.
  1. Implicit Conversion.
  2. Explicit Conversion.

Implicit Conversion.

In this type of conversion data type can be mixed.
For Example :-


                  double a; 
                  int b = 5; 
                  float c = 8.5; 
                  a = b * c;
When two operands of different type are encountered in the same expression, the lower type variable is converted to the higher type variable. The following table shows the order of data types.

Order of data types
Data type
long double
double
float
long
int
char
order

(highest)
To

(lowest)
The int value of b is converted to type float and stored in a temporary variable before being multiplied by the float variable c. The result is then converted to double so that it can be assigned to the double variable a.

0 comments: