Programming Paradigms-1-79
Programming Paradigms-1-79
by
Dr. Amit Biswas
Assistant Professor
Dept. of Computer Science and Engineering
MNNIT Allahabad
UNIT II: Data, Data Types, and Basic Statements: Names, Variables, Binding, Type
Checking, Scope, Scope Rules, Lifetime and Garbage Collection, Primitive Data Types, Strings,
Array Types, Associative Arrays, Record Types, Union Types, Pointers and References,
Arithmetic Expressions, Overloaded Operators, Type Conversions, Relational and Boolean
Expressions, Assignment Statements, Mixed Mode Assignments, Control Structures, Selection,
Iterations, Branching, Guarded Statements
UNIT IV: Object-Orientation, Concurrency, and Event handling: Grouping of Data and
Operations — Constructs for Programming Structures, Abstraction Information Hiding, Program
Design with Modules, Defined Types, Object Oriented Programming — Concept of Object,
Inheritance, Derived Classes and Information Hiding — Templates, Semaphores, Monitors,
Message Passing, Threads, Statement Level Concurrency Exception Handling
(Using C++ and Java as Example Language).
1. Syntax: The specific rules and structure are used to write code in a
programming language.
2. Data Types: The type of values that can be stored in a program, such as
numbers, strings, and booleans.
Some popular programming languages are C, Python, Java, C++, JavaScript, and
Ruby. Each language has its own strengths and weaknesses and is suited for
different types of projects.
➢ Low-level language
➢ Middle-level language
➢ High-level language
Low-level language
o It is machine readable.
o It works based on the binary number 0’s and 1’s.
o The processor runs low-level programs directly without the need of a
compiler or interpreter so the program written in low-level language can be
run very fast.
Middle-level language
High-level language
Text / Code Segment: It contains program instructions for execution. It is a read only section
of a program and cannot be changed during the execution.
Data Segment: This segment is divided in to two sub-segments i.e., initialized data segment
and uninitialized data segment. Initialized data segment contains global and static variables
that are explicitly initialized and uninitialized data segment contains global and static
variables that are not initialized.
Heap Segment: Memory allocated dynamically during the program execution occurs in the
heap section.
Stack Segment: It contains activation records / stack frames. Stack frames are created for
each function call and pushed in the stack. All the local variables of function are stored in its
stack frame. A stack frame is popped from stack when corresponding function is returned.
#include <stdio.h>
int x = 10; int main()
float y; {
char a = 'A';
void fun() static int b;
{ int c;
int p = 30; y = 20.5;
int *ptr = (int*) malloc(sizeof(int)*2); printf("a = %c, x = %d, y = %f, b =
*ptr = 50, *(ptr+1) = 60; %d",a,x,y,b);
static float q = 40.9; fun();
printf("\n p = %d, q = %f, %d, return 0;
%d",p,q, *ptr, *(ptr+1)); }
free(ptr);
}
Decode: Decoded
captured data is
transferred to the unit for
execution.
Execute: Instruction is
finally executed and
result is stored in the
register or RAM.
12-02-2024 Dr. Amit Biswas Programming Paradigms 18
Communication
Wants to communicate
a = b + c * 60;
Symbol Table
1 a float Lexical Analyzer
2 b float
3 c float
<id, 1> = <id, 2> + <id, 3> * 60;
Syntax Analyzer
Semantic Analyzer
temp1 = int_to_float(60)
Intermediate code temp2 = id3 * temp1
generation temp3 = id2 + temp2
id1 = temp3
temp1 = int_to_float(60)
temp2 = id3 * temp1 temp1 = id3 * 60.0
Code optimization id1 = id2 + temp1
Temp3 = id2 + temp2
id1 = temp3
MOV id3, R2
temp1 = id3 * 60.0 MUL #60.0, R2
Target code generator
id1 = id2 + temp1 MOV id2, R1
ADD R2, R1
MOV R1, id1
Grammar in theory of computation is a finite set of formal rules that are generating
syntactically correct sentences. Formally, it is defined as four tuples –
G=(V,T,P,S)
G=(V,T,P,S) where
V = { S , A} ⇒ Non-Terminal symbols,
T={a,b} ⇒ Terminal symbols,
Production rules P = { S → aA, A → aA, A → bA , A→ ε },
S={S} ⇒ Start symbol.
a, b
L = {a, aa, ab, aba, aab, aaa, abb ……..}
a q2
q0 a, b
b
q1
A parse tree or derivation tree is a tree that represents the syntactic structure of
a string according to some context-free grammar.
x+y*z
A computer program that calculates the total payable amount of a product. Here, the
programming language should know the type of data to perform the operation on it.
Data types are present in programming languages for at least three different reasons:
Primitive or Built-in Data types are those data types that are pre-defined by the
programming language. Most languages have native data types that are created for
ease of execution of data.
A boolean is a data type with two possible values: true (1) or false (0). The two
values help represent truth conditions found in logic control structures. The name
comes from a branch of mathematics called Boolean algebra, named after George
Bool.
Logical and, or, not etc. operations can be performed on Boolean data type.
Character Data Type: The char type is used to store single characters (letters,
digits, symbols, etc...). We have to remember, when values of variables are stored in
the computer's memory, they must ultimately be stored in terms of only 1's and 0’s.
There are two popular character sets used frequently in programming languages:
ASCII
o Originally required 7 bits of memory to store, but modern extensions of
ASCII require 1 full byte (8 bits) of storage space.
o Has a base set of 128 characters, including all of the English letters (upper
and lower case), digits 0-9, some common symbols, and several unprintable
characters like tab, return, backspace, etc... The extended versions of ASCII
add several more printable characters.
Unicode
Numeric data types are types of data that consist of numbers, which can be computed
mathematically with various standard operators such as add, minus, multiply, divide,
and more. Examples of numeric data types are examination marks, height, weight,
the number of students in a class, share values, price of goods, monthly bills, fees,
and others.
A nothing data type is a feature of some programming languages which allow the
setting of a special value to indicate a missing or uninitialized value or absence of
data. For example – null or none.
Derived Data Type: The data types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types. For example – function, array,
pointer etc.
User Defined Data Type: The data types that are defined by the user are known
as user-defined data types. For example: class, structure, union, Enumeration etc.
Storage Class in C
A storage class is used to describe the following things:
o The variable scope.
o The location where the variable will be stored.
o The initial value of a variable.
o A lifetime of a variable.
o Who can access a variable?
Default initial value: An unpredictable value, which is often called a garbage value.
Life: Till the control remains within the block in which the variable is defined.
Life: Till the control remains within the block in which the variable is defined.
Scope: Global.
The scope of a variable is the region in which it can be used. Beyond that area, we
cannot use that variable. A variable may be in the memory but may not be accessible
though. So, the area of our program where we can actually access our entity (variable
in this case) is the scope of that variable.
o Global scope: When variable is defined outside all functions. It is then available to
all the functions as well as all the blocks of the program.
o Local scope: When variable is defined inside a function or a block, then it is locally
accessible within that function or block.
Lifetime of any variable is the time for which the particular variable outlives in
memory during running of the program.
12-02-2024 Dr. Amit Biswas Programming Paradigms 57
Scope
#include<stdio.h>
int main()
{
int x = 100, y = 50;
if(x = = y){
int z = 1;
}
else {
int z = 0;
}
printf(“%d”, z);
return 0;
}
12-02-2024 Dr. Amit Biswas Programming Paradigms 58
Example
int a = 10;
float fun()
{
float b = 20.5;
return b*b;
}
Static Binding: When the compiler acknowledges all the information required to
call a function or all the values of the variables during compile time, it is called
static binding. As all the required information is known before runtime, it increases
the program efficiency and it also enhances the speed of execution of a program.
Static Binding makes a program very efficient, but it declines the program’s
flexibility, as ‘values of the variable’ and ‘function calling’ are predefined in the
program.
➢ In statically typed languages like C, C++, and Java, the data types of variables are
explicitly defined during declaration. The compiler performs type checking
during the compilation phase to detect type errors, such as assigning incompatible
data types or using incompatible operations.
➢ In dynamically typed languages like Python and JavaScript, variables can change
their data type during runtime. Type checking occurs at runtime, and errors
related to type incompatibility are identified when the program is executed.
Type conversion, also known as type casting, is the process of converting data from
one data type to another. It allows developers to perform operations between
different data types or to store data of one type into a variable of a different type.
Data type conversions are two types –
An array is a linear data structure and simply a collection of data items of the same
data type, which you can access using a common name. In an array, data items are
stored in contiguous memory locations.
Arrays can be –
Single dimensional
Multidimensional
To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].
int arr[5];
for (int i = 0; i < 5; i++)
{
arr[i] = value;
}
For 1D array
➢ Given that the base address of an array A[5:20] is 1020 and the size of
each element is 2 bytes in the memory, find the address of A[17].
➢ Given that an array, Arr[1:10][1:15] with base value 100 and the size of each
element is 2 Byte in memory. Find the address of Arr[8][6] with the help of
row-major order.
12-02-2024 Dr. Amit Biswas Programming Paradigms 75
Array
➢ Given that an array, Arr[1:10][1:15] with base value 100 and the size of each
element is 2 Byte in memory. Find the address of Arr[8][6] with the help of
column-major order.