Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
83 views12 pages

PPL-Unit 2 Part 3

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views12 pages

PPL-Unit 2 Part 3

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Principle of programming languages

Unit-II: Data types: Introduction, primitive,


character string types, user defined ordinal types
Data types: Introduction
•A data type defines a collection of data values and a
set of predefined operations on those values.

• According to the programming languages there are


different types of data types are there like primitive
type, user defined data types, associative type etc
Primitive Data Types
•Data types that are not defined in terms of other types
are called primitive data types.
•Nearly all programming languages provide a set of
primitive data types.
Numeric Types:
Integer:
•The most common primitive numeric data type is
integer.
For example,Java includes four signed integer sizes: byte,
short, int, and long.
•Some languages, for example, C++ and C#, include
unsigned integer types, which are simply types for integer
values without signs.
•A negative integer could be stored in sign-magnitude
notation, in which sign bit is set to indicate negative and
the remainder of the bit string represents the absolute
Primitive Data Types
Floating-Point:
•Floating-point data types model real numbers

•On most computers, floatingpoint numbers are stored


in binary, which exacerbates the problem.

For example, even the value 0.1 in decimal cannot be


represented by a finite number of binary digits.

•Another problem with floating-point types is the loss


of accuracy
through arithmetic operations.

•Most languages include two floating-point types, often


called float and double. Float size is 4 bytes and
double size is 8 bytes.
Primitive Data Types
Complex
•Some programming languages support a complex data
type—for example, Fortran and Python
•In Python, the imaginary part of a complex literal is
specified
by following it with a j or J—for example,
(7 + 3j)

Decimal
•Decimal data types store a fixed number of decimal
digits, with the decimal point at a fixed position in the
value.
•COBOL,C# and F# also have decimal data types.
•Decimal types have the advantage of being able to
precisely store decimal values, For example, the number
0.1 (in decimal) can be exactly represented in a decimal
Primitive Data Types
Boolean Types
•Boolean types are perhaps the simplest of all types.
Their range of values has only two elements: one for
true and one for false.
•Boolean types are often used to represent switches
or flags in programs.

Character Types
•Character data are stored in computers as numeric
codings. Traditionally, the most commonly used
coding was the 8-bit code ASCII (American Standard
Code for Information Interchange).
•In c language size of character 1 byte. In java size
is 2 bytes
Character String Types
A character string type is one in which the values consist of
sequences of characters.

Design Issues
•Should strings be simply a special kind of character array or
a primitive type?
• Should strings have static or dynamic length?

Strings and Their Operations


•The most common string operations are assignment,
catenation, substring
reference, comparison, and pattern matching.
•C and C++ use char arrays to store character strings. These
languages provide a collection of string operations through
standard libraries.
For example, consider the following declaration:
Character String Types
•Some of the most commonly used library functions
for character strings in C and C++ are
• strcpy, which moves strings;
•strcat, which catenates one given string onto
another;
•strcmp, which lexicographically compares two given
strings;
•strlen, which returns the number of characters

String Length Options


•The length can be static and set when the string is
created. Such a string is called a static length
string
•This is the choice for the strings of Python, the
immutable objects of Java’s String class ,Ruby’s
built-in String class
Character String Types
•The second option is to allow strings to have
varying length up to a declared and fixed maximum
set by the variable’s definition, as exemplified by the
strings in C and the strings of C++. These are called
limited dynamic length strings.
•The third option is to allow strings to have varying
length with no maximum,as in JavaScript, Perl, and
the standard C++ library. These are called dynamic
length strings
•Ada 95+ supports all three string length options.
User-Defined Ordinal Types
•An ordinal type is one in which the range of possible
values can be easily associated with the set of
positive integers.
•There are two user-defined ordinal types that have
been supported by programming languages:
enumeration
and subrange.

Enumeration Types
•An enumeration type is one in which all of the
possible values, which are named constants are
provided or enumerated in the definition. which are
called enumeration constants.
C# example:
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
•The enumeration constants are typically implicitly
User-Defined Ordinal Types
Design issues:
•Is an enumeration constant allowed to appear in more
than one type definition, and if so, how is the type of
an occurrence of that constant in the program
checked?
• Are enumeration values coerced to integer?
• Are any other types coerced to an enumeration
type?
Designs:
•C and Pascal were the first widely used languages to
include an enumeration data type. C++ includes C’s
enumeration types InC++
enum colors {red, blue, green, yellow, black};
colors myColor = blue, yourColor = red;
•The colors type uses the default internal values for
the enumeration constants, 0, 1, . . . ,For example, if
User-Defined Ordinal Types
•In ML, enumeration types are defined as new types
with datatype declarations.
Ex: datatype weekdays = Monday | Tuesday |
Wednesday |
Thursday | Friday
•F# has enumeration types that are similar to those of
ML, except the reserved word type is used instead of
datatype

Subrange Types
•A subrange type is a contiguous subsequence of an
ordinal type. For example,12..14 is a subrange of
integer type. Subrange types were introduced by
Pascal and are included in Ada.
Example in ADA: type Days is (Mon, Tue, Wed, Thu, Fri,
Sat, Sun);

You might also like