You are on page 1of 24

C H A P T E R 6

DATA TYPES
C S 1 5 0 P R O G R A M M I N G L A N G U A G E S
OVERVIEW
Primitive Data Types
Character / String Data Types
User-defined Ordinal Types
Arrays, Associative Arrays, Records
DESCRIPTOR
collection of attributes of a variable
built by the compiler, usually as a part of the
symbol table
used for type checking and building the code
for the allocation and deallocation operations
Primitive Types
NOT DEFINED BY OTHER TYPES

1.Boolean
2.Numeric
a. Integer
b. Floating Point
c. Complex
d. Decimal

3.Character
Boolean
TRUE OR FALSE

can be represented by a single bit, but often stored in the smallest


efficiently addressable cell of memory- usu. a byte
Integer
FOR DISCRETE ENTITIES

Java C/C++/C# Python 2


a. byte a. short a. int
b. short b. int b. long
c. int c. long
d. unsigned
d. long
e. signed Python3
int (unlimited)
Ruby
a. Fixnum
b. Bignum
C Integer Types

REF: https://www.tutorialspoint.com/cprogramming/c_data_types.htm
Floating Point Types
fractions and exponents

Newer machines use the IEEE Floating-Point Standard 754 format


Precision, and range - define the collection of values that can be represented by a floating-type

Precision
accuracy of the fractional part of a value
Range
combination of the range of fractions, and range of exponents

IEEE F-P Standard 754 Format Represenations


single double
Character Type
STORED AS NUMERIC CODINGS

Common Encodings:
ASCII (8-bit) - 128 characters

Unicode - every character in the supported languages has a unique number (called
code point), regardless of the platform
- currently has1,114,112 code points; 17 planes
UTF-8 -variable width; 8-bit representation up to 4 bytes
UTF-16 - variable width; 16-bit representation up to 4 bytes
UTF-32- fixed width; requires 32 bits (4 bytes) to encode any character
Character String Types
SEQUENCE OF CHARACTERS

Design Issues: String Length Options:


special character array or primitive? static
static or dynamic length? limited dynamic
dynamic

Common Operations: PLs supporting this type:


assignment C - char array
catenation C++ - char array or string class (preferred)
substring reference Java - String class
comparison Python - string (primitive)
pattern matching Ruby - String class
Static
COMPILE TIME DESCRIPTOR
Limited Dynamic
COMPILE TIME DESCRIPTOR

1. Numeric
Enumeration Type
ALL POSSIBLE VALUES, WHICH ARE NAMED CONSTANTS, ARE DEFINED

Design Issues:
is an enum constant allowed to appear in more
than one type definition?
are enum values coerced to int?
are any other types coerced to an enum type?

PLs & implementations supporting this type:


C (gcc) - constants are of unsigned int type
(if no neg value in enum); otherwise int
C ++ - similar to C but coercion to enum is
not allowed (see example ->)
C# - similar to C++ but constants are not
coerced to int
Java - Enum class; constants are not
coerced to int; may have instance data
fields, constructor, methods
Enumeration Type
ALL POSSIBLE VALUES, WHICH ARE NAMED CONSTANTS, ARE DEFINED
Array Types
HOMOGENEOUS AGGREGATE OF DATA ELEMENTS
AN ELEMENT IS IDENTIFIED BY ITS POSITION IN THE AGGREGATE
RELATIVE TO THE FIRST ELEMENT (called an index/subscript)
a[0] a[1] a[2] a[3]

1 2 3 4

1238788544 1238788548 1238788552 1238788556


&a[0] &a[1] &a[2] &a[3]

&a = ?
b[0] b[1] b[2] b[3]

a h o y

6299664 6299665 6299666 6299667


&b[0] &b[1] &b[2] &b[3]

&b= ?
Array Type
FINITE MAPPING OF (ARRAY NAME & SUBSCRIPT) -> ELEMENT

array type -> subscript type & element type


Rectangular
rows have the same number of elements &
Jagged
lengths of the rows may not be the same;
columns have the same number of elements; arrays of arrays;
1 array declaration creates 1 multidimensional array; 1 array declaration creates row+1 arrays
more efficient but less flexible;

1. Numeric C#:

C#

Python Numpy (ndarray)


C:

PYTHON:
Access Function for Arrays

A= B + (S * P)
A -> address of current elem
B - > base address
S -> size of element
P -> no. of elements preceeding the current
a[0] a[1] a[2] a[3]

1 2 3 4

1238788544 1238788548 1238788552 x


&a[0] &a[1] &a[2] &a[3]

address_of_elem= base address + (size_of_elem * no_of_elements_preceeding_the_current_elem)

x =1238788544 + (4*3)
x =1238788556
Access Function forMD arrays

&a[i,j] = &a[0][0] + (elem_size * ((i*n)+j)

& -> address


a -> multidimensional array
i - > current row
j -> current column
n -> no of elements per row
Access Function forMD arrays (Generalization)

&a[i,j] = &a[row_lb,col_lb] + (elem_size * (((i-row_lb)*n)+(j-col_lb))


& -> address
a -> multidimensional array
i - > current row
j -> current column
n -> no of elements per row
row_lb -> lower bound of rows
col_lb -> lower bound of cols
Associative Arrays
key -> value
keys must be stored unlike non-associative arrays (due to their regularity)
1D array
COMPILE TIME DESCRIPTOR
MD array
COMPILE TIME DESCRIPTOR

1. Numeric