Anda di halaman 1dari 16

Data Handling

Concept of Data Types


C++ Data Types
Variables
Formatting Output
Data types are means to identify the type of data
and associated operations of handling it.

C++ DATA TYPE
DERIVED DATA TYPE
(ARRAY,
FUNCTION,POINTER,
REFERENCE)
FUNDAMENTAL
DATA TYPE
INTEGRAL TYPE
INT CHAR
VOID FLOATING TYPE
FLOAT DOUBLE
USER DEFINED TYPE
(STRUCTURE, CLASS)
int data type (for integers)
Eg:- 5, 39, -1917
char data type(for characters)
Eg:- A, 9
float data type (for floating point numbers)
Eg:- 3.1415 , 0.31415E01
Float can represent values between integers
Represents a much greater range of values
Floating point operations are usually slower than integer
operations
double data type(for double precision floating point
numbers)
It is treated as distinct data type because it occupies twice as
much memory as type float and stores number with much
larger range and precision
void data type (for empty set of values and non
returning functions)
No object of type void may be declared
Data type Modifiers
Following modifiers can be
applied to integer and character
base type
signed
unsigned
long
short
Long may also be applied to
double
Data Type Summary
Keyword

Low range High Range Digits of
precision
Bytes of
Memory
char -128 127 NA 1 BYTE
unsigned char 0 255 NA 1 BYTE
signed char SAME AS CHAR NA 1 BYTE
short int(short) -32768 32767 NA 2 BYTE
unsigned short 0 65535 NA 2 BYTE
signed short SAME AS SHORT NA 2 BYTE
int -32768 32767 NA 2 BYTE
Unsigned int 0 65535 NA 2 BYTE
Signed int SAME AS INT NA 2 BYTE
long int (long) -2147483648 2147483647 NA 4 BYTE
Unsigned long 0 4294967295 NA 4 BYTE
Signed long SAME AS LONG NA 4 BYTE
Float 3.4 X 10
-38
3.4 X10
38
-1 7 4 BYTE
Double 1.7X10
-308
1.7+10
308
-1 15 8 BYTE
Long double 3.4 X 10
-4932
1.1X10
4932
-1 19 10 BYTE
VARIABLES
Define
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.
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 statement
int x,y;
Syntax
Data type var_name ;

VARIABLES
There are two values are associated with a
variable:-
Its data value, stored at some location
referred as rvalue
Its location value, i.e. the address in
memory at which its data value is stored
referred as lvalue
Eg :- int i (2 bytes)

10
i
1051
Data value of variable
Memory Address
Variables name
Lvalue of i = 10
Rvalue of i = 1051
Initialization of variables
When we declare a variable it's default value
is undetermined(uninitialized). We can declare
a variable with some initial value.
int a = 20; or int a (20);
long val= 25L
unsigned long v = 42ul
float f= 23.4 f
double d = 214.70
Dynamic initialization
Initialization of the variable at runtime is
referred to as dynamic initialization.
Eg:- float avg= sum/count;

Integer Variables Example
#include<iostream.h>
void main ( )
{
int var1; //define var1
int var2, var3; //define var2, var3
var1 = 20; //assign value to var1
var2 = var1 + 10; //assign value to var2
cout<<Result =;
cout<<var2<< endl; //displaying the sum of var1 + 10
}

Character Variable Example
#include<iostream.h>
void main ( )
{
char charvar1 = A; //define char variable
char charvar2 = \t;
cout << charvar1; // display character
cout << charvar2;
charvar1 = B; //reassigning charvar1 to B
cout << charvar1;
}
float
#include<iostream.h>
void main ( )
{
float rad, area;
const float PI =3.14;
cout << Enter radius of circle;
cin >> rad;
area = PI * rad * rad;
cout <<Area is << area << endl;
}
CONSTANTS (Access Modifier)
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.
A constant variable must be initialized at
the time of its declaration.
e.g. const float pi = 3.1415f;
e.g. const char ch = 'A';
Formatting Output
Manipulators are operators used in C++
for formatting output. The data is
manipulated by the programmer's choice of
display.
Two of these manipulators are setw() and
setprecision().
In order to use these manipulators, include
header file iomanip.h
#include<iomanip.h>

endl Manipulator:
This manipulator has the same functionality as the
'n' newline character.
cout << "C++" << endl;
cout << "Programming";
Output:- C++
Programming

setw Manipulator:
Sets the width of the field assigned for the output. It
takes the size of the field (in numbers of
characters) as a parameter.
Syntax:- setw(x)
cout<<setw(6)<<R;
Output:- _ _ _ _ _ R
The setw()manipulator does not stick from one
cout statement to the next


setprecision Manipulator:
The setprecision() Manipulator is used with
floating point numbers. It is used to set the
total number of digits to be displayed when
floating point numbers are printed.
cout<< setprecision(5)<<123.456;
O/p=123.46
This manipulator can also be used to set the
no. of decimal places to displayed:
fixed (Display floating point numbers in normal
notation. )
scientific (display floating point numbers in scientific
(E) notation.


float x =12.345678;
cout.setf(ios::fixed);
cout << setprecision(3) << x << endl;
Cout.setf(ios::scientific);
cout << x << endl;
setprecision() is sticky, whatever
precision you set, sticks with the cout
device until such time as you change it
with additional setprecision() later in the
program.

Anda mungkin juga menyukai