Unit3 A
Unit3 A
Unit3 A
Primitive Data
Types:
Numbers
Strings
Ordinal Types
Pointers
Definitions
• data type
– collection of data objects
– a set of predefined operations
• descriptor : collection of attributes for a
variable
• object : instance of a user-defined
(abstract data) type
• Ones complement
• Boolean
– Range of values: two elements, one for “true”
and one for “false”
– Could be implemented as bits, but often as
bytes
• Character
– Stored as numeric codings
– Most commonly used coding: ASCII
– An alternative, 16-bit coding: Unicode
• Complex (Fortran, Scheme, Python)
• Rational (Scheme)
Copyright © 2007 Addison-Wesley. All rights reserved.
1–14
Character Strings
• Values are sequences of characters
• Operations:
– Assignment and copying
– Comparison (=, >, etc.)
– Catenation
– Substring reference
– Pattern matching
• Design issues:
– Is it a primitive type or just a special kind
of array?
Copyright © 2007 Addison-Wesley. All rights reserved.
1–15
Character String Implementations
• C and C++
– Not primitive
– Use char arrays and a library of functions
that provide operations
• SNOBOL4 (a string manipulation
language)
– Primitive
– Many operations, including elaborate pattern
matching
• Java
– String class
Day1: Days;
Day2: Weekday;
Copyright © 2007 Addison-Wesley. All rights reserved.
1–24
Implementation of User-Defined
Ordinal Types
• Enumeration types are implemented
as integers
• Subrange types are implemented
like the parent types
– code inserted (by the compiler) to
restrict assignments to subrange
variables
allocation
ptr = (int*)malloc(
sizeof( int))