0% found this document useful (0 votes)
3 views14 pages

Unit 2 - Principles of Programming Languages - Www.rgpvnotes.in

The document provides an overview of data types in programming, including primitive types, character types, user-defined types, arrays, records, unions, pointers, and variables. It discusses design issues, operations, and implementation details for each data type, highlighting their characteristics and usage in programming languages. Additionally, it covers concepts such as binding and static type binding, emphasizing the importance of understanding data types for effective programming.

Uploaded by

Isha Bharti
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)
3 views14 pages

Unit 2 - Principles of Programming Languages - Www.rgpvnotes.in

The document provides an overview of data types in programming, including primitive types, character types, user-defined types, arrays, records, unions, pointers, and variables. It discusses design issues, operations, and implementation details for each data type, highlighting their characteristics and usage in programming languages. Additionally, it covers concepts such as binding and static type binding, emphasizing the importance of understanding data types for effective programming.

Uploaded by

Isha Bharti
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/ 14

Subject Name: Advanced Computer Architecture

Subject Code: CS-6001


Semester: 6th
Downloaded from be.rgpvnotes.in

Subject Notes
Department of Computer Science and Engineering
Subject Name: PPL Subject Code:CS6002
Unit-2
Data type:
A data type defines a collection of data values and a set of predefined operations on those values.
Design issues for all data types
i. What operations are defined and how are they specified.
ii. It is convenient, both logically and concurentely, to think of variables in terms of descriptors.
Descriptor:
A descriptor is the collection of the attributes of a variable
i. A descriptor is used for type checking, allocation and, de-allocation.
ii. Static attributes need only be available at compile-time; dynamic attributes need to be available at
run-time.

Primitive data types


Primitive data types are those that are not defined in terms of other data types
Common primitive types:
1. Numeric types
Early PLs had only numeric primitive types, and still play a central role among the collections of types
supported by contemporary languages.
A. Integers
i. For example, C, Ada, java .. allows these: short integer, integer and long integer.
ii. An integer is represented by a string of bits, with the leftmost representing the sign bit.
B. Floating point numbers
i. Model real numbers but only as approximations.
ii. Languages for scientific use support at least two floating-point types; sometimes more.
iii. usually exactly like the hardware, but not always; some languages allow accuracy specs in code e.g.
(Ada).
IEEE (The Institute of Electrical and Electronics Engineers) floating-point formats: (a) Single precision, (b)
Double precision
8 bits 23 bits

Exponent Fraction

Sign bit
Figure 2.1 (a) Single Precision floating point format

11 bits 52 bits

Exponent Fraction

Sign bit

Figure 2.1 (b) Double Precision floating point format

Page no: 1 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

C. Decimal
i. for business applications.
ii. store a fixed number of decimal digits (coded)
Advantage: accuracy
Disadvantages: limited range, wastes memory

D. Boolean types
i. The range of values has only two elements TRUE or FALSE.
ii. Booleans types are often used to represent switches or flags in programs

Character- Character is a symbol in programming language that has meaning. A character can be any letter,
number, punctuation marks, symbols or whitespace. For example, the word "character" consists of eight
characters and the phrase "Hello World!" consists of 12 characters including the whitespace and exclamation
mark. In programming character is a datatype. We can declare a variable as of a character type and store
characters in the variable. For example, in C and Java, we write,
Characters when storing in a variable must be inserted between single quotes. String is another datatype in
programming, which is a modified version of character datatype. Strings are used to store more than one
character
Character String Types
 Character string type is one in which the values consist of sequences of characters
Design issues with the string types
i. Should strings be simply a special kind of character array or a primitive type?
ii. Should strings have static or dynamic length?

String Operations
i. Assignment ( Java: str1 = str2;) (C: strcpy(pstr1, pstr2);
ii. Comparison (=, >, etc.) BASIC: str1 < str2
iii. Concatenation, C: strcat (str1,str2), (Java : str2 + str3;)
iv. Substring reference
v. Pattern matching, C: strcmp(str1,str2);

Implementation
i. Static length - compile-time descriptor
ii. Limited dynamic length - may need a run-time descriptor for length (but not in C and C++ because the
end of a string is marked with the null character)
iii. Dynamic length - need run-time descriptor; allocation/deallocation is the biggest implementation
problem
Limited dynamic string

Static string Maximum Length

Length
Current Length
Address Address

(a) Compile – time (b) Run-time descriptor for


descriptor for static strings limited dynamic strings

Page no: 2 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Figure 2.2 Compile and Run time Descriptor for static and dynamic string

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
Design issue:
Should a symbolic constant be allowed to be in more than one type definition?
Examples
i. Java does not include an enumeration type, but provides the Enumeration interface
C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
ii. Evaluation of enumeration types
iii. aid to readability e.g. no need to code a color as number.
iv. aid to reliability e.g. compiler can check

Array
Array is a data structure, which provides the facility to store a collection of data of same type under single
variable name. Just like the ordinary variable, the array should also be declared properly. The declaration of
array includes the type of array that is the type of value we are going to store in it, the array name and
maximum number of elements.
Design Issues
i. What types are legal for subscripts?
ii. Are subscripting expressions in element references range checked?
iii. When are subscript ranges bound?
iv. When does allocation take place?
v. Are Jagged or rectangular multidimensioned arrays allowed, or both?
vi. Can array objects be initialized?
vii. Are any kind of slices allowed?

1. Indexing is a mapping from indices to elements map(array_name, index_value_list)  an


element
2. Index Syntax
i. FORTRAN, PL/I, Ada use parentheses
ii. Most other languages use brackets
3. Subscript Types:
i. FORTRAN, C - integer only
ii. Java - integer types only
4. Array Initialization
Some languages allow initialization at the time of storage allocation
i. C, C++, Java, C# example
int list [] = {4, 5, 7, 83} ;
ii. Character strings in C and C++
char name [] = freddie ;
iii. Arrays of strings in C and C++
har * a es [] = { Bo , Jake , Joe };
iv. Java initialization of String objects
“tri g[] a es = { Bo , Jake , Joe };

Page no: 3 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Associative
i. An associative array is an unordered collection of data elements that are indexed by an equal number
of values called keys.
ii. Also known as Hash tables
a. Index by key (part of data) rather than value.
b. Store both key and value (take more space).
c. Best when access is by data rather than index.
iii. Examples:
Lisp alist: ((key1 . data1) (key2 . data2) (key3 . data3)
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Associative arrays have an index that is not necessarily an integer, and can be sparsely populated. The index
for an associative array is called the key, and its type is called the Key Type.
Design Issues
i. What is the form of references to elements?
ii. Is the size static or dynamic?

Record
A record is a possibly heterogeneous aggregate of data elements in which the individual elements are
identified by names
Design Issues
i. What is the form of references?
ii. What unit operations are defined? (Assignment, equality, assign corresponding filed)

Some Important Points


1. Implementation method
i. Simple and efficient, because field name references are literals bound at compile-time.
ii. Use offsets to determine address.

2. Record Definition Syntax


COBOL uses level numbers to show nested records; others use recursive definitions.

3. Record Field References


i. COBOL
field_name OF record_name_1 OF ... OF record_name_n
ii. Others (dot notation)
record_name_1.record_name_2. ... .record_name_n.field_name

4. Record Operations
i. Assignment
a. Pascal, Ada, and C allow it if the types are identical
b. In Ada, the RHS can be an aggregate constant
ii. Initialization
Allowed in Ada, using an aggregate constant
iii. Comparison
In Ada, = and /=; one operand can be an aggregate constant
iv. Move Corresponding
a. In COBOL - it moves all fields in the source record to fields with the same names in the destination
record

Page no: 4 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

b. Useful operation in data processing application, where input records are moved to output files after
same modification.
Comparing records and arrays
a. Access to array elements is much slower than access to record fields, because subscripts are dynamic
(field names are static)
b. Dynamic subscripts could be used with record field access, but it would disallow type checking and it
would be much slower.
Record

Name
Field 1
Type

Offset

.
:
.
:
Name

Type
Field n Offset

Address

Figure 2.3 Record

Union
Union is a data type with two or more member similar to structure but in this case all the members share a
common memory location. The size of the union corresponds to the length of the largest member. Since the
member share a common location they have the same starting address.The real purpose of unions is to
prevent memory fragmentation by arranging for a standard size for data in the memory. Java has neither
records nor unions.
The syntax of union declaration is
union union_name
{
type element 1;
type element 2;
……………..
type element n;
};
This declares a type template. Variables are then declared as:
union union_name x,y,z;
For example, the following code declares a union data type called Student and a union variable called stud:
union student
{
int rollno;
float totalmark;
};

Page no: 5 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Design Issues for unions


i. Should type checking be required? Note that any such type checking must be dynamic.
ii. Should unions be integrated with records?

Pointer
Pointer is a variable that represents the location of a data item, such as variable or an array element. Within
the o puter’s e or , e er stored data ite o upies o e or ore o tiguous e or ells. The u er
of memory cells required to store a data item depends on the type of the data item. For example, a single
character will typically be stored in one byte of memory; an integer usually requires two contiguous bytes, a
floating-point number usually requires four contiguous bytes, and a double precision usually requires eight
contiguous bytes.
For example, a C program contains the following declarations.
int i,*ptri;
float f,*ptrf;
The first line declares i to be an integer type variable and ptri to be a pointer variable whose object is an
integer quantity. The second line declares f to be a floating-point type variable and ptrf to be a pointer
variable whose object is a floating point quantity.

Uses
i. Addressing flexibility (support indirect addressing)
ii. Dynamic storage management (scoping)
Design Issues
What is the scope and lifetime of pointer variables?
i. What is the lifetime of heap-dynamic variables?
ii. Are pointers restricted to pointing at a particular type?
iii. Are pointers used for dynamic storage management, indirect addressing, or both?
iv. Should a language support pointer types, reference types, or both?

Variables
A variable is an abstraction of a memory cell. They have the following characteristics:
i. Name The identifier that refers to the variable. Variables created dynamically with new or malloc can be
anonymous.
ii. Address The location in memory where the variable is stored. A single variable can have multiple different
addresses as a program runs. It is also possible that multiple variables can refer to the same address. An
address is so eti es alled a L-Value si e it is eeded for the left-hand side of an assignment.
iii. Value The o te ts of the aria le. A alue is so eti es alled a R-Value si e it is eeded for the right-
hand side of an assignment.
iv. Type Determines what possible values can be stored in the variable and what operations are permitted on
it. We will discuss types more in the future.
v. Lifetime The period of time when a variable exists.
vi Scope The portion of code which can access the variable.
int main() {
int a;
int b;
}
The above program creates two variables to reserve two memory locations with names a and b. We created
these variables using int keyword to specify variable data type which means we want to store integer values in
these two variables. Similarly, you can create variables to store long, float, char or any other data type

Page no: 6 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Binding
A binding is an association between an entity and an attribute, such as between a variable and its type or
value, or between a function and its code.
Binding time is the point at which a binding takes place. There are different times a binding can happen:
i. Design Time Some binding decisions are made when a language is designed such as the binding of + to
addition in C, the operations of the String class in Java and so on.
ii. Compile Time Bindings can also be done while the program is compiled such as binding variables to types in
C++ or Java.
iii. Link Time aaasLink time is when compiled code is combined into a full program for C and C++. At this time,
global and static variables are bound to addresses.
iv. Run Time Many bindings happen at run time including most values being bound to variables. In dynamically
typed languages like Python, types are bound at run time as well.
Any binding that happens at run time is called dynamic, and any that happens before run time is called static.
Things that are statically bound can not change as a program runs, and those that are dynamically bound can
change.
What bindings take place in the following C code?
count = count + 5;
 The type of count is bound at compile time
 The set of possible values of count is bound at design time
 The meaning of the operator symbol + is bound at compile time, when the types of its operands have
been determined.
 The internal representation of the literal 5 is bound at design time.
 The value of count is bound at execution time with this statement

Static Type Binding


Static type binding can be done either implicitly or explicitly. Explicit declaration means that the programmer
must specify the types of variables. This is done in languages like C, C++, and Java.
The code below has a few type annotations:
int fact(int x) {
if(x == 0) {
return 1;
} else {
return x * fact(x - 1);
}
}
Implicit static typing means that the programmer does not need to declare types. In languages with type
inferencing, the types are still statically bound, but it is done by the compiler. These languages also normally
provide the ability to declare types if necessary. Type inferencing can lead to difficult compiler errors if the
programmer makes a type error.
Below is the factorial function in Haskell which uses type inference:
fact x =
if x == 0 then
1
else
x * fact (x - 1)
What are some benefits and drawbacks of type inference?
Dynamic Type Binding
Dynamic type binding is when the type of a variable is not decided until the program runs. Dynamically-bound
types are always implicit.

Page no: 7 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Below is the factorial function in Python which uses dynamic binding:


def fact(x):
if x == 0:
return 1
else:
return x * fact(x - 1)
Dynamic typing can lead to run time errors that a compiler would have caught. Consider the following Python
code that attempts to add the numbers from from 1 to 100:
total = 0
number = 1

while number <= 100:


toal = total + number
number = number + 1

print(total)
This ode pri ts 0 i stead of 5050". This t pe of error a e diffi ult to de ug.

TYPE CHECKING
Type checking is the process of verifying and enforcing the constraints of types, and it can occur either at
compile time (i.e. statically) or at runtime (i.e. dynamically). Type checking is all about ensuring that the
program is type-safe, meaning that the possibility of type errors is kept to a minimum. A type error is an
erroneous program behavior in which an operation occurs (or trys to occur) on a particular data type that it’s
not meant to occur on. This could be a situation where an operation is performed on an integer with the
intent that it is a float, or even something such as adding a string and an integer together:

Static Type Checking


A language is statically-typed if the type of a variable is known at compile time instead of at runtime. Common
examples of statically-typed languages include Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML, Pascal etc. The
big benefit of static type checking is that it allows many type errors to be caught early in the development
cycle. Static typing usually results in compiled code that executes more quickly because when the compiler
knows the exact data types that are in use, it can produce optimized machine code (i.e. faster and/or
using less memory). Static type checkers evaluate only the type information that can be determined at
compile time, but are able to verify that the checked conditions hold for all possible executions of the
program, which eliminates the need to repeat type checks every time the program is executed.
For example consider a statement in c++
int a=10;
here compiler needs to know the datatype of variable "a" before using it.

Dynamic Type Checking


Dynamic type checking is the process of verifying the type safety of a program at runtime.
In Dynamic type checking our need not specify or declare the type of variable instead compiler itself figures
out what type a variable is when you first assign it a value.
Now consider some statements in python:
str="Python"
str2=10
Here you need not declare the data type. The compiler itself will know which type the variable belongs to
when you first assign it a value (str1 is of "String" data type and str2 is of type "int"). Common dynamically-
typed languages include JavaScript, Lisp, PHP, Prolog, Python, Ruby, Smalltalk.

Page no: 8 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Strong Typing
A strongly typed language is one in which each name in a program in the language has a single type associated
with it, and that type is known as compile time. The essence of this definition is that all types are statically
bound. The weakness of this definition is that it ignores the possibility that the storage location to which it is
bound may store values of different types at different times. We define a programming language to
be strongly typed if type errors are always detected.

Type Compatibility
There are two types of compatibility methods: name compatibility and structure compatibility.
Name type compatibility means that 2 variables have compatible types only if they are in either the same
declaration or in declarations that use the same type name.
Example: int x; int y; // x and y are name type compatible (if the variables are declared using the same
declaration or the same type)

Structure type compatibility means that 2 variables have compatible types if either type has identical
structures.
Example struct foo { int x; float y; };
struct bar { int x; float y;};
foo a; bar b; // a and b are structure type compatible (if the variables have the same structure even
though they are of differently named type)

1. C uses compatibility by structure while C++ uses name compatibility


2. Ada uses name compatibility except for anonymous arrays which use structure compatibility

Named constants- a constant is a value that cannot be altered by the program during normal execution, i.e.,
the value is constant. When associated with an identifier, a constant is said to be "named," although the terms
"constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is
an identifier with a value that can be changed during normal execution, i.e., the value is variable. Constants
are useful for both programmers and compilers: for programmers they are a form of self-documenting code
and allow reasoning about correctness; while for compilers they allow compile-time and run-time checks that
constancy assumptions are not violated, and allow or simplify some compiler optimizations.

Variable initialization- A variable provides us with named storage that our programs can manipulate. Each
variable in Java has a specific type, which determines the size and layout of the variable's memory; the range
of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Initialization is often done on the declaration statement, e.g., in Java
int sum = 0;
There are three ki ds of aria les i Ja a −
(a) Local variables
(b) Instance variables
(c) Class/Static variables
Local Variables
1. Local variables are declared in methods, constructors, or blocks.
2. Local variables are created when the method, constructor or block is entered and the variable will be
destroyed once it exits the method, constructor, or block.
3. Access modifiers cannot be used for local variables.
4. Local variables are visible only within the declared method, constructor, or block.
5. Local variables are implemented at stack level internally.

Page no: 9 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Instance Variables
1. Instance variables are declared in a class, but outside a method, constructor or any block.
2. When a space is allocated for an object in the heap, a slot for each instance variable value is created.
3. Instance variables are created when an object is created with the use of the keyword 'new' and
destroyed when the object is destroyed.
4. Instance variables hold values that must be referenced by more than one method, constructor or
block, or essential parts of an object's state that must be present throughout the class.
Class/Static Variables
1. Class variables also known as static variables are declared with the static keyword in a class, but
outside a method, constructor or a block.
2. There would only be one copy of each class variable per class, regardless of how many objects are
created from it.
3. Static variables are rarely used other than being declared as constants. Constants are variables that are
declared as public/private, final, and static. Constant variables never change from their initial value.
4. Static variables are stored in the static memory. It is rare to use static variables other than declared
final and used as either public or private constants.

Sequence control with Expressions: -The control of the order of execution of the operations both primitive
and user defined.
Implicit: determined by the order of the statements in the source program
or by the built-in execution model
Explicit: the programmer uses statements to change the order of execution
(e.g. uses If statement)

Conditional Statements-
In the programs that we have examined to this point, each of the statements is executed once, in the order
given. Most programs are more complicated because the sequence of statements and the number of times
each is executed can vary. We use the term control flow to refer to statement sequencing in a program.

If Statement
The simplest if structure involves a single executable statement. Execution of the statement occurs only if the
condition is true.
Syntax:
if (condition)
statement;

If-else statement
In if-else statement if the condition is true, then the true statement(s), immediately following the if-statement
are executed otherwise the false statement(s) are executed. The use of else basically allows an alternative set
of statements to be executed if the condition is false.
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}

Page no: 10 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

IF -else if statement
It can be used to choose one block of statements from many blocks of statements. The condition which is true
only its block of statements is executed and remaining are skipped.
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}

Switch Statement
Switch statement is alternative of nested if-else.it is executed when there are many choices and only one is to
be executed.
Syntax:
switch(expression)
{
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;
default:
statement;
}

Loops
Looping statement are the statements execute one or more statement repeatedly several number of times. In
C programming language there are three types of loops; while, for and do-while.
Advantage with looping statement
i. Reduce length of Code
ii. Take less memory space.
iii. Burden on the developer is reducing.
iv. Time consuming process to execute the program is reduced.

Page no: 11 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Types of Loops.
There are three types of Loops available in 'C' programming language.
while loop-
Syntax-
While(expression)
{ Block of statements; }
for loop
Syntax
for ( expression1; expression2; expression3)
{
Single statement
or
Block of statements;
}

do..while
Syntax-
do
{
Single statement
or
Block of statements;
}while(expression);

Exception handling-
An exception is a problem that arises during the execution of a program. A C++ exception is a response to an
exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is
built upon three keywords: try, catch, and throw.
Throw − A progra thro s a e eptio he a pro le sho s up. This is do e usi g a throw keyword.
Catch − A progra at hes a e eptio ith a e eptio ha dler at the pla e i a progra here ou a t
to handle the problem. The catch keyword indicates the catching of an exception.
Try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by
one or more catch blocks.

Page no: 12 Follow us on facebook to get real-time updates from RGPV


We hope you find these notes useful.
You can get previous year question papers at
https://qp.rgpvnotes.in .

If you have any queries or you want to submit your


study notes please write us at
[email protected]

You might also like