0% found this document useful (0 votes)
30 views

PPL Lecture3

The document discusses different elementary data types including primitive, character string, user defined ordinal, array, record, union, pointer and reference types. It provides details on implementation and examples of these data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

PPL Lecture3

The document discusses different elementary data types including primitive, character string, user defined ordinal, array, record, union, pointer and reference types. It provides details on implementation and examples of these data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SPPU Principle of Programming Notes

(Lecture 3)

Elementary Data Types

1) Primitive data Types


Primitive is the most fundamental data type usable in the Programming
language. There are eight primitive data types: Boolean, byte, character,
short, int, long, float, and double. In a Programming language, these data
types serve as the foundation for data manipulation.

Primitives’ data types are-

o Boolean data type


o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type

2) Character String types

A character string type is one in which the values consist of sequences of


characters.
Strings and Their Operations

The most common string operations are assignment, concatenation,


substring reference, comparison, and pattern matching.

Character string types could be supported directly in hardware; but in most


cases, software is used to implement string storage, retrieval, and
manipulation.

Implementation

SPPU NOTES YOUTUBE CHANNEL- UR ENGINEERING FREIND


i) C and C++

C and C++ use char arrays to store character strings.

e.g. char my_string[]="Parag"

ii) Java

In Java, strings are supported by the String class, whose values are
constant strings, and the StringBuffer class, whose values are changeable
and are more like arrays of single characters.

User Defined Ordinal Types


An ordinal type is one in which the range of possible values can be easily
associated with a set of positive integers. In pascal the built in ordinal types are
integers, char, Boolean. User can define two types of ordinal types: enumeration
and subrange.

a) Enumeration types
An enumeration type is one in which the user enumerates all the possible
values, which are symbolic constants. In Ada a typical enumeration type is
shown as: type DAYS is ( M, T, W, R, F, Sa, S).
b) Subrange types
A subrange type is an ordered contiguous subsequence of an ordinal type.
These types were introduced by Pascal. For example: 12 . . . 16 is a subrange
of the integer type.

Array types
An Array is a Linear data structure which is a collection of data items having
similar data types stored in contiguous memory locations. By knowing the
address of the first item we can easily access all items/elements of an array.

Types of Arrays
The various types of arrays are as follows.

• One dimensional array


• Multi-dimensional array

SPPU NOTES YOUTUBE CHANNEL- UR ENGINEERING FREIND


One-Dimensional Array
one-dimensional array is also called a single dimensional array where the
elements will be accessed in sequential order. This type of array will be accessed
by the subscript of either a column or row index.

Multi-Dimensional Array
When the number of dimensions specified is more than one, then it is called as a
multi-dimensional array. Multidimensional arrays include 2D arrays and 3D
arrays.

Associative Arrays
An associative array is an unordered collection of data elements that are indexed
by an equal number of values called keys. In the case of non-associative arrays,
the indices never need to be stored (because of their regularity). In an associative
array, however, the user-defined keys must be stored in the structure.
So each element of an associative array is in fact a pair of entities, a key and a
value.

Example:

Record Types
The record type is a data type that you use to treat several different pieces of data
as one unit, for example, name and phone number. Each of these units is called
a variable of record type. Each piece of data is called an attribute. An attribute
can be a simple data type, another record type, or an array. A data value or a
variable for the record type is called a record.

SPPU NOTES YOUTUBE CHANNEL- UR ENGINEERING FREIND


Union types
A union is a special data type available in C that allows to store different data
types in the same memory location. You can define a union with many
members, but only one member can contain a value at any given time. Unions
provide an efficient way of using the same memory location for multiple-
purpose.

To define a union, you must use the union statement in the same way as you did
while defining a structure. The union statement defines a new data type with more
than one member for your program. The format of the union statement is as
follows –

union [union tag] {


member definition;
member definition;
...
member definition;
} [one or more union variables];

Pointer and reference Type


Pointers

A pointer is a variable that holds the memory address of another variable. A


pointer needs to be dereferenced with the * operator to access the memory
location it points to.

References

A reference variable is an alias, that is, another name for an already existing
variable. A reference, like a pointer, is also implemented by storing the
address of an object.
A reference can be thought of as a constant pointer (not to be confused with a
pointer to a constant value!) with automatic indirection, i.e., the compiler will
apply the * operator for you.

Example:
int i = 3;

SPPU NOTES YOUTUBE CHANNEL- UR ENGINEERING FREIND


// A pointer to variable i or "stores the address of i"
int *ptr = &i;

// A reference (or alias) for i.


int &ref = i;

SPPU NOTES YOUTUBE CHANNEL- UR ENGINEERING FREIND


SPPU NOTES YOUTUBE CHANNEL- UR ENGINEERING FREIND

You might also like