PPS Unit-1
PPS Unit-1
Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-1
Evolution of Programming & Languages - Problem solving
through programming - Writing algorithms & Pseudo code -
Single line and multiline comments - Introduction to C:
Structure of the C program - Input and output statements.
1952: Autocode
• Developed by Alick Glennie.
• The first compiled computer programming language.
• COBOL and FORTRAN are the languages referred to as Autocode.
Evaluation of Programming & Languages
1957: FORTRAN
• Developers are John Backus and IBM.
• It was designed for numeric computation and scientific computing.
• Software for NASA probes voyager-1 (space probe) and voyager-2 (space probe)
was originally written in FORTRAN 5.
1958: ALGOL
• ALGOL stands for ALGOrithmic Language.
• The initial phase of the most popular programming languages of C, C++, and
JAVA.
• It was also the first language implementing the nested function and has a simple
syntax than FORTRAN.
• The first programming language to have a code block like “begin” that indicates
that your program has started and “end” means you have ended your code.
1959: COBOL
• It stands for COmmon Business-Oriented Language.
• In 1997, 80% of the world’s business ran on Cobol.
• The US internal revenue service scrambled its path to COBOL-based IMF
(individual master file) in order to pay the tens of millions of payments mandated
by the coronavirus aid, relief, and economic security.
Evaluation of Programming & Languages
1964: BASIC
• It stands for beginners All-purpose symbolic instruction code.
• In 1991 Microsoft released Visual Basic, an updated version of Basic
• The first microcomputer version of Basic was co-written by Bill Gates,
Paul Allen, and Monte Davidoff for their newly-formed company,
Microsoft.
1972: C
• It is a general-purpose, procedural programming language and the most
popular programming language till now.
• All the code that was previously written in assembly language gets
replaced by the C language like operating system, kernel, and many
other applications.
• It can be used in implementing an operating system, embedded system,
and also on the website using the Common Gateway Interface (CGI).
• C is the mother of almost all higher-level programming languages like
C#, D, Go, Java, JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix’s
C shell.
Evaluation of Programming & Languages
The evolution of programming and programming languages is a fascinating
journey of how humans have developed increasingly sophisticated ways to
communicate with computers. Here's an overview of the key milestones:
1. Problem Analysis:
1. Understand the problem requirements.
2. Identify inputs, outputs, and constraints.
3. Break the problem into smaller, manageable parts.
2. Algorithm Development:
1. Develop a step-by-step solution (algorithm).
2. Use flowcharts or pseudocode for visualization.
3. Ensure efficiency and correctness.
3. Programming Basics:
1. Familiarize yourself with programming languages (e.g., Python, C++, Java).
2. Understand variables, data types, and operators.
3. Learn control structures like loops (for, while) and conditionals (if-else).
4. Data Structures:
1. Use arrays, lists, stacks, queues, and dictionaries for efficient data handling.
2. Solve problems using advanced structures like trees, graphs, and heaps.
Problem solving through programming
5. Functions and Modularity:
• Write reusable code using functions.
• Understand the scope and lifecycle of variables.
• Practice modular programming for clean, maintainable
code.
6. Debugging and Testing:
• Debug programs to identify and fix errors.
• Write test cases for various scenarios.
• Use debugging tools and logging.
7. Optimization:
• Analyze the time and space complexity of solutions.
• Optimize algorithms for better performance.
Problem Solving through programming
What is problem solving?
Problem solving is the act of defining a problem; determining the cause of the
problem; identifying, prioritizing, and selecting alternatives for a solution; and
implementing a solution.
Problem Solving through programming
Stages of Problem solving
1. Understand the problem
2. Define the problem
3. Define boundaries
4. Plan solution
5. Check solution
Writing algorithms & Pseudo code
What is an Algorithm?
An algorithm is a set of instructions or step-by-step
procedures for solving problems or performing tasks.
These sets of instructions are unambiguous and very
well-defined.
What is Pseudocode?
It is a high-level description of an algorithm that uses
natural language and simple programming constructs to
convey the logic of an algorithm. Pseudocodes are like a
special language software developers use to plan an
algorithm before writing the code.
Algorithms
• Example: Find the maximum value in a given list of numbers.
• Solutions: Here is a set of instructions (or algorithm) that you can
follow to find the maximum value in the list.
1. Start with the first number and assign it as the maximum value.
2. Now, move to the second number; if the second number > the first
number, then set the maximum value to the second number; there is
no change.
i.e., if the current number is greater than the maximum value, then set the
maximum value to the current number.
3. Move on to the next number in the list, and repeat step 2 until you
have reached the end of the list.
4. The maximum value is the value that was stored in the maximum
variable.
Algorithms
Writing Algorithms
An algorithm is a set of steps designed to solve a problem or accomplish a task.
Algorithms are usually written in pseudocode, or a combination of your speaking
language and one or more programming languages, in advance of writing a
program.
Step-1: Obtain detailed information on the problem.
Step-2: Analyze the Problem
Step-3: Think of a problem solving approach
Step-4: Review the problem solving approach and try to think of a better
alternative
Step-5: Develop a basic structure of the algorithm
Step-6: Optimize, improve and refine.
Writing Algorithms
Characteristics of a good algorithm
1: Input and output must be specified
2: All important steps must be mentioned
3: Instructions must be perfectly ordered
4: Short and effective descriptions
5: The algorithm must contain finite
number of steps
Writing Algorithms
Examples of Algorithm
1. Addition of two numbers
Step-1: Start
Step-2: Declare variables num1, num2, and sum
Step-3: Read values of num1 and num2
Step-4: Add the values of num1 and num2 and assign the result
Step-5: Display the sum
Step-6: End
Writing Algorithms
Examples of Algorithm
2. Comparison of 3 numbers to find the largest number
1: Start
2: Declare variables num1, num2, and num3
3: Read values of num1 and num2 and num3
4: Compare num1, num2 and num3
5: If num1>num2 and num1>num3
Display num1 is the largest number
Else
If num2>num1 and num2>num3
Display num2 is the largest number
Else
Display num3 is the largest number
6: End
Creating Algorithms Contd…
Example : Print 1 to 20
Step 1: Start
Step 2: Initialize X as 0,
Step 3: Increment X by 1,
Step 4: Print X,
Step 6: Stop
Creating Algorithms
Example
Convert Temperature from Fahrenheit (℉)to Celsius (℃)
Step 1: Start
Step 4: Print C
Step 5: Stop
Creating Algorithms
Example
Algorithm to Add Two Numbers Entered by User
Step 1: Start
Step 6: Stop
Creating Algorithms
Write an Algorithm to:
Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-1
Evolution of Programming & Languages - Problem solving
through programming - Writing algorithms & Pseudo code -
Single line and multiline comments - Introduction to C:
Structure of the C program - Input and output statements.
START
DISPLAY "Enter the first number:"
READ number1
DISPLAY "Enter the second number:"
READ number2
result ← number1 + number2
DISPLAY "The sum is:", result
END
pseudocode to convert a temperature
from degrees Celsius to Fahrenheit:
START
DISPLAY "Enter the temperature in degrees
Celsius:"
READ celsius
fahrenheit ← (celsius × 9/5) + 32
DISPLAY "The temperature in Fahrenheit is:",
fahrenheit
END
pseudocode to check if a number is
even or odd:
START
DISPLAY "Enter a number:"
READ number
IF (number MOD 2 = 0) THEN
DISPLAY "The number is even."
ELSE
DISPLAY "The number is odd."
ENDIF
END
pseudocode for detecting Abnormal
Heart Rate
START
DISPLAY "Enter the heart rate (in beats per minute):"
READ heart_rate
IF heart_rate < 60 THEN
DISPLAY "Heart rate is below normal (bradycardia)."
ELSE IF heart_rate > 100 THEN
DISPLAY "Heart rate is above normal (tachycardia)."
ELSE
DISPLAY "Heart rate is normal."
ENDIF
END
pseudocode for calculating Body Mass
Index (BMI)
START
DISPLAY "Enter weight (in kilograms):"
READ weight
DISPLAY "Enter height (in meters):"
READ height
bmi ← weight / (height × height)
• Single-line comment
• Multi-line comment
Single line comments
Multiline comments
Single line and Multi line comments
Example:
/* This program takes age input from the user It stores it in the age variable
And, print the value using printf() */
#include <stdio.h>
int main() {
//declare integer variable
int age;
printf("Enter the age: ");
scanf("%d", &age);
printf("Age = %d", age);
return 0;
}
Basic Structure of C
Program
The sections of a C program are listed below:
❖ Documentation section
❖ Preprocessor section
❖ Definition section
❖ Global declaration
❖ Main function
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
int a; int a;
a=10; scanf(“%d”, &a);
} }
Input and Output Functions Contd…
}
Input and Output Functions Contd…
/* Getting Multiple Input using scanf ( ) function */
#include<stdio.h>
#include<conio.h>
void main( )
{
int a, b;
float c;
scanf(“%d %d”, &a, &b);
scanf(“%f ”, &c);
}
Input and Output Functions Contd…
ii. The printf ( ) Function
To print Instructions / Output onto the Screen
Requires Format Specifiers & Variable names to print
data
Syntax
Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-1
Evolution of Programming & Languages - Problem solving
through programming - Writing algorithms & Pseudo code -
Single line and multiline comments - Introduction to C:
Structure of the C program - Input and output statements.
A block or a region where a variable is declared, defined and used and when a
block or a region ends, the variable is automatically destroyed.
#include <stdio.h>
int main()
{
int var = 34; // Scope of this variable is within main() function only.
// Therefore, called LOCAL to main() function.
printf("%d", var);
return 0;
}
Scope
Local Variables:
Variables that are declared within the function block and can be used only
within the function are called local variables.
Local Scope or Block Scope
A local scope or block is a collective program statement placed and declared
within a function or block (a specific area surrounded by curly braces). C also
has a provision for nested blocks, which means that a block or function can
occur within another block or function. So it means that variables declared
within a block can be accessed within that specific block and all other internal
blocks of that block but cannot be accessed outside the block.
Scope
Example (Local Variable):
#include <stdio.h>
int main ()
{
//local variable definition and initialization
int x,y,z;
//actual initialization
x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %d\n",
x, y, z);
return 0;
}
Scope
Example (Global Variable):
#include <stdio.h>
int z; //global variable
int main ()
{
//local variable definition and initialization
int x,y;
//actual initialization
x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
return 0;
}
Binding
In C, binding refers to the association of a name with a particular entity, such
as a variable or a function. Binding is typically done through the use of
declarations or definitions in the code.
Example:
#include <stdio.h>
int main() {
int x = 5; // Binding the name 'x' to the value 5
printf("The value of x is: %d\n", x);
{
int x = 10; // Binding a new 'x' in a nested block
printf("The value of nested x is: %d\n", x);
}
printf("The value of x is still: %d\n", x); // Accessing the outer 'x'
return 0;
}
DATA TYPES
A data type specifies the type of data that a variable can store such as integer,
floating, character, etc.
DATA TYPES
Types Description
Basic They are arithmetic types and are further classified into:
Types (a) integer types and (b) floating-point types.
Derived They include (a) Pointer types, (b) Array types, (c)
Types Structure types, (d) Union types and (e) Function types.
DATA TYPES
Integer
Types
DATA TYPES
Integer Types
To get the exact size of a type or a variable on a particular platform, you can
use the sizeof operator. The expressions sizeof(type) yields the storage size
of the object or type in bytes.
DATA TYPES
Floating-Point
Types
DATA TYPES
The void
type
21CSS101J
Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-1
Evolution of Programming & Languages - Problem solving
through programming - Writing algorithms & Pseudo code -
Single line and multiline comments - Introduction to C:
Structure of the C program - Input and output statements.
int main() {
int x = 10; // 'x' is an l-value
int y = x; // 'x' is an r-value on the right side of the assignment, and 'y' is an l-
value
int* ptr = &x; // '&' operator gets the address of 'x', which is an l-value
// The following lines would result in compilation errors:
// int* ptr2 = &10; // Error: Cannot take the address of an r-value
// &x = 20; // Error: 'x' is an l-value, but the left side of the assignment
operator expects an l-value
return 0
}
Operator Precedence
❖ The precedence of operators in C indicates the order in which the operators
will be evaluated in the expression.
❖ Associativity, on the other hand, defines the order in which the operators of
the same precedence will be evaluated in an expression. Also, associativity
can occur from either right to left or left to right.
❖ The precedence of operators determines which operator is executed first if
there is more than one operator in an expression.
OPERATORS
Arithmetic Operator
An arithmetic operator performs mathematical operations on numerical values
such as addition, subtraction, multiplication, and division (constants and
variables). Operato
Meaning of Operator
r
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
OPERATORS
Arithmetic Operator
#include <stdio.h>
int main()
{ Output
int a = 10,b = 20, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b
= %d \n",c);
return 0;
}
OPERATORS
Relational Operators
A relational operator checks the relationship between two operands.
Relational operators are specifically used to compare two quantities or
values in a program. If the relation is true, it returns 1; if the relation is false, it
returns value 0.
OPERATORS
Relational Operators
Operato
Meaning of Operator
r
== Equal to
!= Not equal to
Relational Operators
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x == y); // returns 0
(false) because 5 is not equal to 3
return 0;
}
OPERATORS
Relational Operators
#include <stdio.h>
int main() {
int x = 5;
int y = 5;
printf("%d", x == y); // returns 1
(true) because 5 is equal to 5
return 0;
}
OPERATORS
Relational Operators
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x != y); // returns 1
(true) because 5 is not equal to 3
return 0;
}
OPERATORS
Relational Operators
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x>y); // returns 1 (true)
because 5 is greater than 3
return 0;
}
OPERATORS
Logical Operators
An expression containing logical operator returns either 0 or 1 depending
upon whether expression results true or false. Logical operators are commonly
used in decision making in C programming.
OPERATORS
Logical Operators
#include <stdio.h>
int main()
{
int a = 10,b=6,c=12,d=15;
printf("a=%d \n", a++);
printf("b=%d \n", b--);
printf("c=%d \n", c++);
printf("d=%d \n", d--);
return 0;
}
OPERATORS
#include <stdio.h>
int main()
{
int a = 10;
printf("a=%d \n", a++);
printf("b=%d \n", a);
printf("c=%d \n", ++a);
printf("d=%d \n", a);
return 0;
}
OPERATORS
Comma operator
Comma operators are used to link related expressions together.
OPERATORS
sizeof operator
The sizeof is a unary operator that returns the
size of data (constants, variables, array, structure,
etc).
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
21CSS101J
Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-1
Evolution of Programming & Languages - Problem solving
through programming - Writing algorithms & Pseudo code -
Single line and multiline comments - Introduction to C:
Structure of the C program - Input and output statements.
Types of Expression
EXPRESSIONS
Types of Expression
Arithmetic Expression
It consists of arithmetic operators ( + , - , * , and / ) and computes values
of int, float, or double type.
Relational Expression
It usually consists of comparison operators (> , < , >= , <= , === , and !==
) and computes the answer in the bool type, i.e., true (1) or false (0).
Logical Expression
It consists of logical operators (&&, ||, and !) and combines relational
expressions to compute answers in the bool type.
EXPRESSIONS
Types of Expression
Conditional Expression
It consists of conditional statements that return true if the condition is met
and false otherwise.
Pointer Expression
It may consist of an ampersand (&) operator and returns address values.
Bitwise Expression
It consists of bitwise operators ( >>, <<, ~, &, |, and ^ ) and performs
operations at the bit level.
EXPRESSIONS
#include <stdio.h>
int main(){
int a = (6 * 2) + 7 - 9; //Arithmetic Expression
printf("The arithmetic expression returns: %d\n", a);
int b = 10;
printf("The relational expression returns: %d\n", b % 2 == 0);
//Relational Expression
int c = (7 > 9) && ( 5 <= 9); //Logical Expression
printf("The logical expression returns: %d\n", c);
int d = (34 > 7) ? 1 : 0; //Conditional Expression
printf("The conditional expression returns: %d\n", d); //Pointer
Expression
int e = 20;
int *addr = &e;
printf("The pointer expression returns: %p\n", addr); //Bitwise
Expression
int f = 10;
int shift = 10 >> 1;
printf("The bitwise expression returns: %d\n", shift);
return 0;
}`
EXPRESSIONS
Postfix operators:
Postfix operators like function calls and array subscripting have higher
precedence than most other operators.
int arr[5] = {1, 2, 3, 4, 5};
int value = arr[2] * 3; // value = 9
arr[2]=3
3*3=9
Operator Precedence
Parentheses:
The expressions within parentheses are evaluated first. This allows you to
control the order of evaluation.
int result = (2 + 3) * 4; // result = 20
(2+3) is evaluated first and then it is multiplied with 4.
1) 2+3=5
2) 5*4=20
Postfix operators:
Postfix operators like function calls and array subscripting have higher
precedence than most other operators.
int arr[5] = {1, 2, 3, 4, 5};
int value = arr[2] * 3; // value = 9
arr[2]=3
3*3=9
Operator Precedence
Unary operators:
Unary operators, such as the increment (++) and decrement (--) operators, are
applied next.
int a = 5;
int result = ++a; // a becomes 6, (Right to Left)
result = 6
Multiplicative operators:
Multiplication (*), division (/), and modulo division (%) operators have
higher precedence than additive operators.
int result = 10 + 2 * 5; // result = 20 (multiplication is done first)
Additive operators:
Addition (+) and subtraction (-) operators are applied after the multiplicative
operators.
int result = 10 - 2 + 5; // result = 13 (subtraction is done first) (Left to Right)
Operator Precedence
Relational and equality operators:
These operators compare values and have higher precedence than logical
operators.
int result = 5 < 10 && 2 == 2; // result = 1 (true)
Order of Evaluation
1. 5<10 =>1(Hence 5 is less than 10)
2. 2==2=>1
3. Finally && is evaluated (1&&1)=1
Operator Precedence
Logical operators:
Logical AND (&&) and logical OR (||) operators are applied after relational
and equality operators.
int result = 5 < 10 || 2 > 5; // result = 1 (true)
Assignment operators:
Assignment operators (=, +=, -=, *=, /=, %=) are applied after all other
operators.
int a = 5;
a += 3; // a becomes 8
a=a+3;
a=5+3=>8
Conditional operator (ternary operator):
The conditional operator (?:) is evaluated after all other operators. It is used
for conditional expressions.
int a = 5; int result = (a > 10) ? a : 10; // result = 10
Assignment Operators
Bitwise Operators in C
❖ Bitwise operators are used to manipulate one or more bits from integral
operands like char, int, short, long.
❖ Bitwise operators operate on individual bits of integer (int and long) values.
❖ If an operand is shorter than int, it is promoted to int before doing the
operations.
❖ In the calculation, just the individual bits of a number are considered, not the
complete number.
❖ Negative integers are stored or represented in two’s complement form. For
example, -4 is 1111 1111 1111 1111 1111 1111 1111 1100.
Bitwise Operators in C
Output:
2
Bitwise Operators in C
Bitwise OR ‘|’
int a=2; //
0010
Truth Table
int b=3; // 0010
0011 0011
---------
int c=a | b;
0011
Output:
3
Bitwise Operators in C
Bitwise XOR ‘^’ 0010
int c=a ^ b;
Output:
1
Bitwise Operators in C
2
Bitwise Operators in C
Program
#include <stdio.h>
int main()
{
printf("%d",10<<2);
return 0;
}
Output: 40
Bitwise Operators in C
Program
#include <stdio.h>
int main()
{
printf("%d",10>>2);
return 0;
}
Output: 2
21CSS101J
Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-1
Evolution of Programming & Languages - Problem solving
through programming - Writing algorithms & Pseudo code -
Single line and multiline comments - Introduction to C:
Structure of the C program - Input and output statements.