0% found this document useful (0 votes)
0 views

Unit C, Chapter-08 Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Unit C, Chapter-08 Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Page 1 CHAPTER- 08, DATA TYPES

CHAPTER-08
DATA TYPES
Datatype: Data type specifies the type of the value that a variable can store.

Data type are classified into three types


1. Fundamental / basic data types
2. Derived data type
3. User defined data type

1. Fundamental / basic data types


C++ supports 5 types of basic data type such as integer, float, character, void, Boolean.

a) Integer (int) data type


Numeric value without fractional part belongs to integer data type. The size of the variable of
integer data type is 2 bytes and the range of value that can be stored in it is -32768 to 32767
(-215 to 215-1).

b) Float data type: Numeric value with fractional part belongs to float data type. The keyword float is
used to declare it. The size of the variable of float data type requires 4 bytes of memory ranging
from 3.4e-38 to 3.4e38.
Ex: float a;
Modifier of float data type
i. Double data type: The size of the variable of double data type requires 8 bytes of
memory space ranging from 1.7e-308 to 1.7e308.

c) Character (char) data type: All single characters which are used in program belong to character
data type. The keyword char is used to declare the variable of character type. The size of character
variables requires 1 byte of memory space ranging from -128 to 127 (ie the ASCII value of
different characters).
Ex: char ch;

d) Void data type: Is a special data type available in C++. It has no values and doesn’t perform any
operation. It specifies the function does not return any value.
Ex: void main()

Prepared by: VIJAYAKUMAR S M.Sc,B.Ed,UGC-NET


Page 2 CHAPTER- 08, DATA TYPES

e) Boolean (bool): We can use this data type to manipulate logical (boolean) expressions.
Ex: bool legalage;
int age;
The statement:
legalage=true;

Data type Size Range


Int 2 bytes -32768 to 32767 (-215 to 215-1)
short int 2 bytes -32768 to 32767 (-215 to 215-1)
long int 4 bytes -2,147,483,648 to 2,147,483,648 (-231 to 231-1)
unsigned int 2 bytes 0 to 65535 (0 to 2 16-1)
unsigned long int 8 bytes 0 to 4, 294, 97, 295 (0 to2 32-1)
Float 4 bytes 3.4e-38 to 3.4e38
Double 8 bytes 1.7e-308 to 1.7e308
Char 1 byte 128 to 127

2. Derived data type


The data types are constructed using fundamental data type is called derived data type. I t includes
arrays, functions, pointers and reference.

3. User defined data type


The data types are constructed by the user according to their needs and requirements is called user
defined data type.
It includes structure, unions, class and enumeration.

Expressions: Expression is a combination of operators and operands which reduces to a single


value after evaluation.
Ex: int a=10, b=20, c;
c= a+b;
=10 + 20
=30
Expression are classified into three types
1) Arithmetic expression
The expression that uses an arithmetic operator is called arithmetic expression.
Three modes of arithmetic expression.

a) Integer mode expression


All the operands of an expression are integer type is called integer mode expression. The
resulting value of this expression is always integer.
Ex: int a=3, b=2, c;
c=a/b;
=3/2
=1, but not (1.33)

b) Real (floating) mode expression


All the operands of an expression are fractional type is called real mode expression. The
resulting value of this expression is always fraction.
Ex: float a=3, b=2, c;
C=a/b;
=3.0/2.0
=1.33

Prepared by: VIJAYAKUMAR S M.Sc,B.Ed,UGC-NET


Page 3 CHAPTER- 08, DATA TYPES

c) Mixed mode expression.


All the operands of an expression are integer and float type is called mixed mode expression.
The resulting value of this expression is always fraction.
Ex: int a=3;
float b=2, c;
c=a/b;
=3/2.0
=1.33

2) Relational expression
The expression that uses a relational operator is called relational expression. The resulting value
of this expression is always true(1) or false (0).
Ex: int a=20, b=10;
if(a>b) result is true (1)

3) Logical expression
The expressi on that uses a logical operator is called logical expression. The resulting value of this
expression is always true(1) or false(0). It is used to combine two or more relational expressions.
Ex:
int a=20, b=20, c=5;
if((a>b) && (a>c))
if((F) && (T)) result is false
Evaluation of expression
Expression in a C++ program are evaluated based on priority (precedence) of operator and Associative of an
operator

Precedence (priority) of an operator:


Evaluating an expression based on priority value of different operators is called precedence of operator. First
priority operator evaluated first followed by second, third and so on.

Associative of an operator
An expression that contains two or more operators of same priority value, then the compiler evaluates the
expression based on associative i.e from left to right associative and right to left associative.
Left to right associative: Evaluating a expression from L to R
Right to left associative: Evaluating a expression from R to L

Precedence and associative of arithmetic operators


Operators Priority/ Precedence value Associativity
( ) brackets First Left to Right
Unary +, -, ++ (increment) , -- Second Right to Left
(decrement)
*, /, % Third Left to Right
+, - Fourth Left to Right

Ex:
Evaluate 6 * (3+3) / 6 – 3

Method 1:
=6 * (3+3) / 6 - 3
=6 * (3+3) /3
=6 * 6 / 3
=6 * 2
=12

Prepared by: VIJAYAKUMAR S M.Sc,B.Ed,UGC-NET


Page 4 CHAPTER- 08, DATA TYPES

Method 2
=6 * (3+3) / 6 - 3
=6 * 6 / 6 – 3
=36 / 6 – 3
=6 – 3
=3

In the above two methods two possible outputs 12 and 3, but the correct output is 3, because it follows the
rules of priority and associative of arithmetic operators (ie BODMAS rule)

NOTE:
BODMAS  Brackets of Division Multiplication Addition Subtraction.

--*********--

Prepared by: VIJAYAKUMAR S M.Sc,B.Ed,UGC-NET

You might also like