Download presentation
Presentation is loading. Please wait.
1
Chapter 6: Data Types Lectures # 10
2
Topics Introduction Primitive Data Types Character String Types
Array Types Associative Arrays Record Types Union Types Pointer and Reference Types Chapter 6: Data Types 2
3
Introduction A data type defines a collection of data objects and a set of predefined operations on those objects. More data types makes programming easier but too many data types might be confusing. Chapter 6: Data Types 3
4
Primitive data types A primitive data type is one that is not defined in terms of other types. Primitive types are a reflection of the underlying hardware: Integer. Floating point. Other primitive types require a little non-hardware support for their implementation. Major categories include: Numeric. Boolean. Character. Chapter 6: Data Types 4
5
Numeric types: Integer
Almost always an exact reflection of hardware. 2s complement binary representation used to store negative integers. C has many different integer types: char, short, int, long. signed and unsigned. Ada allows: SHORT INTEGER, INTEGER, LONG. Java has 4 different integer types: byte, short, int, and long. Chapter 6: Data Types 5
6
Numeric types: Floating-point
Model real numbers, but only as approximations. Languages for scientific use support at least two floating-point types (e.g., float and double) sometimes more. Values are defined in terms of precision and range: Precision is the accuracy of the fractional part. Range is a combination of the range of fractions and exponents. Chapter 6: Data Types 6
7
Floating-point IEEE floating-point formats:
(a) Single precision, (b) Double precision Chapter 6: Data Types 7
8
Numeric types: Decimal
Used mostly for business applications. Stores a fixed number of decimal digits, with the decimal point at a fixed position. Decimal digits are coded (BCD with 2 digits/byte). Example from DCL “Digital Command Language”: PAYRATE FIXED DECIMAL(7,2); Advantage: accuracy. Disadvantages: limited range, wastes memory. Chapter 6: Data Types 8
9
Boolean Types Simplest of all types.
Range of values has only 2 elements, true and false. Introduced in ALGOL 60. Could be represented as a single bit, but most often as a byte (the smallest efficiently addressable cell of memory). C does not include a Boolean type; instead, numeric expressions are used as conditionals. Advantage: readability. Chapter 6: Data Types 9
10
Character types Character data are stored as numeric codings. ASCII:
8-bit allows 256 characters. Inadequate for global communication. Unicode: 16-bit allows 655,536 characters . Includes characters from most of the world’s natural languages. Java was the first widely-used language to use the Unicode character set. C# and Java Script also support Unicode. Chapter 6: Data Types 10
11
Character string Types
Values are a sequence of characters. Operations on character strings: Assignment, Comparison, Concatenation, Substring, Pattern matching. Chapter 6: Data Types 11
12
Character string Types: Ada
STRING is a type that is predefined as a one-dimensional array of CHARACTER elements: S : STRING(1..10); Operations include: Substring reference: S(1..3) = “Bob”; Concatenation: S = “Bob ” & “Alooga”; Comparison: = , /= , < , <= , > , >= Chapter 6: Data Types 12
13
Character string Types: C & C++
Not a primitive type. char arrays store character strings. Strings are terminated by ‘\0’ strings have limited dynamic length. The standard library <string.h> includes a collection of string operations, which include: strlen : string length. strcat : string concatenation. strcmp : string comparison. strcpy : string copy. Chapter 6: Data Types 13
14
Evaluation of character strings
Character strings are important to the writability of a language. Dealing with strings as arrays more cumbersome than dealing with a primitive string type. If implemented as a primitive type with static length, they are inexpensive. Dynamic-length strings provide additional flexibility, but are more expensive to implement. Chapter 6: Data Types 14
15
Character String Length Options
Static Length: string (length is specified at the declaration time). FORTRAN 90, Ada, COBOL, Pascal, Java’s String class. Ex in Fortran 90: CHARACTER (LEN = 15) NAME; Limited Dynamic Length: strings can store any number of characters between 0 and maximum (specified by the variables declaration). C and C++. Chapter 6: Data Types 15
16
Character String Length Options (cont…)
Dynamic (no maximum): variables have varying length with no maximum. provides maximum flexibility, but requires of overhead of dynamic storage allocation and deallocation. SNOBOL 4, Perl, JavaScript. Ada supports all three string length options. Chapter 6: Data Types 16
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.