02 Data Types
02 Data Types
Course Leader:
Jishmi Jos Choondal
1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives
• At the end of this lecture, student will be able to
– Explain data representation and limitations for Integer, floating
point and Character data types
– Explain user defined data types
2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Contents
• Integer Data Type
• Floating Point Data Type
• Character Data Type
3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types
• Data types indicate the type of data that a variable can
hold
4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Primitive Data Type
Primitive Data
Type
Size Data
(of memory Organization
Allotted) (How Bits are
Arranged and What
they Represent)
5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Mathematical Numbers
• Mathematics
– Integers
• -∞, …. -2, -1, 0, +1, +2, … + ∞
– Real Numbers
• Integers, Fraction and square roots
• -∞, …. -2,…,-1,…,-1/2, 0, …,+1,…, +2, … + ∞
6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types for Numbers
• Computers - Integers
– Every data type has a fixed size based on the processor
– A processor can process a ‘word’ at a time
– The size of an integer is equal to word size of the system
– In‘64-bit’ processor, 64 bits or 8 bytes is the word size
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integer Data Type Declarations
• They’re messages to the compiler
8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integer Data Type Declarations contd.
•Declaration
int <variable list>;
• Example
• int a; /*Single variable declaration*/
• int a,b,c; /*Multiple variables*/
• int a = 5; /*Single variable with initialisation, giving initial
value to variable*/
• int a = b = c = 5; /*multiple assignment statement*/
9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integers – Variables and Constants
int a = 5;
• 5 is an Integer constant
• a is an Integer variable
#define SIDES 4
• 4 is an Integer constant
• SIDES is a Symbolic constant
10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integer Data Type
11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Signed Integers
• Assume MSB represents sign – Call it sign bit
0 means ‘+’ and 1 means ‘-’
• Remaining numbers represent the actual number in
binary form
• Results in +0 and -0
12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Unsigned Integers
• Whole numbers
0 to INT_MAX (Infinity in mathematics)
• No need for sign bit
• Equivalent binary form
• Declaration
unsigned int <variable list>;
• Example
• unsigned int a, b, c; /*Multiple variables*/
• unsigned int a; /*Single variable*/
• unsigned int a = 5; /*Single variable with initialisation*/
13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Range of data type
• Range of data type
– Signed : -2n-1 to 2n-1-1
– Unsigned : 0 to 2n-1
• Where ‘n’ is word size in bits
• If n is 8 bits (char):
– Signed : - 128 to +127
– Unsigned : 0 to + 255
• What if n is
– 16 bits (int or short int)
– 32 bits (long int, float)
– 64 bits (double)
14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Long and Short Modifiers
• Long
– When applied to int, it doubles the length in bits of the base
type that it modifies
– If an int is of 16-bits, then long int is of 32-bits
– When applied to double, it roughly doubles the precision
• Short
– Makes the size of an int half (important role by OS architecture,
2bytes for 32bits-OS, 4bytes for 64 bits-OS)
15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Floating Point Data Type
• Think of how money is represented 300.50 (Three hundred
Rupees and fifty Paisa)
• Consider
• 3.1413
• 2.0
• 12.18
• 122.1
– These are numbers where the decimal point moves or ‘floats’
18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Double
• Used to indicate a double precision floating-point number
• Used when more accuracy in required in representing a
floating-point number
• Equivalent to float
– but the number of significant digits stored after the decimal
point is double that of the float
19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Floating Point Data Type - Declaration
• Declaration
– float a;
– float b = 5.0f;
– float a = 5.1f, b = 2.2f;
– double a;
– double b = 5.0;
– double a = 5.1, b = 2.2;
• Constants
– 2.0f is a float constant
– 2.0 is a double constant
20
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Character Data Type
• Character data types store numbers representing
characters
• These numbers are given by ANSI and called ASCII codes
‘A’ has the ASCII value 65
21
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Character Constants
• A single character enclosed in a pair of apostrophes
• Signed (-128 to +127) or unsigned(0 to 255)
• Example
char c = ‘A’ ; //notice the single quotes
int x=10+’A’ //arithmetic operations can be done
printf(“character is %c and its ASCII value is %d\n”,c,c);
printf(“Result is %d\n”,x);
• Output will be
character is A and its ASCII value is 65
Result is 75
22
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Type-cast
• (type) is known as type cast or cast operator
– Its use indicates that the programmer explicitly is converting
value between incompatible types
• Beware
– Do not convert between variables of data types with greater
storage space and lesser storage space
– smaller types can be automatically casted to bigger types
– Use cast operator if you know what you are doing
–Example:
int b = 10; char a = (char) b;
– a is a char variable and b is an int variable
– 10 is an integer constant
23
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Type-cast
• Use cast operator to convert from float to integer
– int a = (int) 10.5f;
• Beware
– Integer only expressions return integer values
– An expression must have at least one floating point
variable or constant to yield a floating point result
24
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• Data types specify the size, representation and
organisation of data in the computer’s memory
• Primitive Data types are the basic data types that are
provided by the programming language
• Three primitive data types are
– Integer
– Float
– Character
25
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Further Reading
Kernighan, B. W. and Richie, D. (1992) The C Programming
Language. 2nd ed., New Delhi:PHI.
26
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences