DATA HANDLING
BASIC DATA TYPES
C++ supports a large number of data types. The built in or basic data types supported by C++ are integer, floating point and character. These are summarized in table along with description and memory requirementType | Byte | Range | Description |
int | 2 | -32768 to +32767 | Small whole number |
long int | 4 | -2147483648 to +2147483647 | Large whole number |
float | 4 | 3.4x10-38 to 3.4x10+38 | Small real number |
double | 8 | 1.7x10-308 to 1.7x10+308 | Large real number |
long double | 10 | 3.4x10-4932 to 3.4x10+4932 | Very Large real number |
char | 1 | 0 to 255 | A Single Character |
VARIABLES
It is a location in the computer memory which can store data and is given a symbolic name for easy reference. The variables can be used to hold different values at different times during the execution of a program.To understand more clearly we should study the following statements:
Total = 20.00;
In this statement a value 20.00 has been stored in a memory location Total.Declaration of a variable
Before a variable is used in a program, we must declare it. This activity enables the compiler to make available the appropriate type of location in the memory.
float Total;
You can declare more than one variable of same type in a single single statement
int x,y;
Initialization of variable
When we declare a variable it's default value is undetermined. We can declare a variable with some initial value.
int a = 20;
INPUT/OUTPUT (I/O)
C++ supports input/output statements which can be used to feed new data into the computer or obtain output on an output device such as: VDU, printer etc. The following C++ stream objects can be used for the input/output purpose.cin console input
cout console output
cout is used in conjuction with << operator, known as insertion or put to operator.
cin is used in conjuction with >> operator, known as extraction or get from operator.
cout << “My first computer";
Once the above statement is carried out by the computer, the message "My first computer" will appear on the screen. cin can be used to input a value entered by the user from the keyboard. However, the get from operator>> is also required to get the typed value from cin and store it in the memory location.
Let us consider the following program segment:
int marks;
cin >> marks;
In the above segment, the user has defined a variable marks of integer
type in the first statement and in the second statement he is trying to
read a value from the keyboard. TYPE CONVERSION
The process in which one pre-defined type of expression is converted into another type is called conversion. There are two types of conversion in C++.1. Implicit conversion
2. Explicit conversion
Implicit conversion
Data type can be mixed in the expression. 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) |
Explicit conversion
It is also called type casting. It temporarily changes a variable data type from its declared data type to a new one. It may be noted here that type casting can only be done on the right hand side the assignment statement.
T_Pay = double (salary) + bonus;
Initially variable salary is defined as float but for the above calculation it is first converted to double data type and then added to the variable bonus.
CONSTANTS
A number which does not change its value during execution of a program is known as a constant. Any attempt to change the value of a constant will result in an error message. A constant in C++ can be of any of the basic data types, const qualifier can be used to declare constant as shown below:const float pi = 3.1415;
The above declaration means that Pi is a constant of float types having a value 3.1415.Examples of valid constant declarations are:
const int rate = 50;
const float pi = 3.1415;
const char ch = 'A';
No comments:
Post a Comment