PPS Notes
PPS Notes
Step 2: Initialize X as 0,
Step 3: Increment X by 1,
Step 4: Print X,
Step 6: Stop
1. 3 Creating Algorithms Contd…
Example 3
Convert Temperature from Fahrenheit (℉) to Celsius (℃)
Step 1: Start
Step 4: Print C
Step 5: Stop
1. 3 Creating Algorithms Contd…
Example 4
Algorithm to Add Two Numbers Entered by User
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum Step 6: Stop
1. 3 Creating Algorithms Contd…
Write an Algorithm to:
Clear Documentation
Only one flow line should enter a Decision symbol but multiple
Code – Instructions
char name;
Variable Initialization
Assigning a value to the declared variable
Values assigned during declaration / after declaration
Variables & Identifiers Contd…
Examples
i. int a, b, c;
a=10, b=20, c=30;
ii. int a=10 ,b=10, c=10;
Scope of Variables
Local Variables
Global Variables
Scope of Variables
Definition
A scope in any programming is a region of the program where a defined variable
can have its existence and beyond that variable it cannot be accessed
Variable Scope is a region in a program where a variable is declared and used
#include <stdio.h>
/* global variable declaration */
int g;
int main ( )
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
1. 12 Scope of Variables Contd…
Note: A program can have same name for local and global variables but the
value of local variable inside a function will take preference
12 Datatypes
Defines a variable before use
Specifies the type of data to be stored in variables
Basic Data Types – 4 Classes
a) int – Signed or unsigned number
b) float – Signed or unsigned number having Decimal Point
c) char – A Character in the character Set
Qualifiers
1. 12 Datatypes Contd…
Datatypes Contd…
Datatypes Contd…
a) Integer Data Type
Whole numbers with a range
No fractional parts
Integer variable holds integer values only
Keyword: int
Memory: 2 Bytes (16 bits) or 4 Bytes (32 bits)
Qualifiers: Signed, unsigned, short, long
Examples: 34012, 0, -2457
Datatypes Contd…
b) Floating Point Data Type
Numbers having Fractional part
Float provides precision of 6 digits
Integer variable holds integer values only
Keyword: float
Memory: 4 Bytes (32 bits)
Examples: 5.6, 0.375, 3.14756
Datatypes Contd…
c) Double Data Type
Also handles floating point numbers
Double provides precision of 14 digits
Integer variable holds integer values only
Keyword: float
Memory: 8 Bytes (64 bits) or 10 Bytes (80 bits)
Qualifiers: long, short
1. 12 Datatypes Contd…
d) Character Data Type
handles one character at a time
Keyword: char
Memory: 1 Byte (8 bits)
1. 13 Expressions
Expression : An Expression is a collection of operators and
operands that represents a specific value
Operator : A symbol which performs tasks like arithmetic
operations, logical operations and conditional operations
Operands : The values on which the operators perform the task
Expression Types in C
a) Infix Expression
b) Postfix Expression
c) Prefix Expression
1. 13 Expressions Contd…
a) Infix Expression
The operator is used between operands
General Structure : Operand1 Operator Operand2
Example : a + b
b) Postfix Expression
Operator is used after operands
General Structure : Operand1 Operand2 Operator
Example : ab+
1. 13 Expressions Contd…
c) Prefix Expression
Operator is used before operands
General Structure : Operator Operand1 Operand2
Example : +ab
1. 14 Input and Output Functions
Ability to Communicate with Users during execution
Input Operation
Feeding data into program
Data Transfer from Input device to Memory
Output Operation
Getting result from Program
Data Transfer from Memory to Output device
Header File : #include<stdio.h>
1. 14 Input and Output Functions Contd…
Input / Output Function Types
a) Formatted Input / Output Statements
b) Unformatted Input / Output Statements
1. 14 Input and Output Functions Contd…
a) Formatted Input / Output Statements
Reads and writes all types of data values
Arranges data in particular format
Requires Format Specifier to identify Data type
Basic Format Specifiers
%d – Integer
%f – Float
%c – Character
%s - String
1. 14 Input and Output Functions Contd…
i. The scanf ( ) Function
Reads all types of input data
Assignment of value to variable during Runtime
Syntax
#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);
} }
1. 14 Input and Output Functions Contd…
/* Getting Multiple Different Inputs
/* Getting Multiple Input using scanf ( )
using scanf ( ) function */
function */
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h> void
void main( )
main( )
{ {
int a, b;
int a, b, c;
float c;
scanf(“%d%d%d”,&a,&b,&c);
} scanf(“%d%d%f”,&a,&b,&c);
}
1. 14 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);
}
1. 14 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 printf(“Control String/Format Specifier”,arg1,arg2,… argn)
ii. getchar ( ) – Reads one character at a time till enter key is pressed
1. 14 Input and Output Functions Contd…
iii. gets ( ) – Accepts any string from Keyboard until Enter Key
is pressed
Unformatted Output Statements
i. putch ( ) – Writes alphanumeric characters to Monitor
(Output Device)
ii. putchar ( ) – Prints one character at a time
iii. puts ( ) – Prints a String to Monitor (Output Device)
1. 14 Input and Output Functions Contd…
/* Example 4 – Using printf ( ) & scanf ( ) function */
#include<stdio.h>
#include<conio.h> void main(
)
{
int a, b; float c;
printf(“Enter the Value of a, b & c”);
scanf(“%d %d%f”, &a, &b, &c);
printf(“Value of a, b & c is %d%d%f”, a, b, c); getch ( );
}
1. 15 Operators in C
C supports rich set of built in Operators
Used to manipulate Constants (Data) & Variables
Part of Mathematical (or) Logical expressions
Operators vs Operands
Operator – Definition
Symbol (or) Special character that instructs the compiler
to perform mathematical (or) Logical operations
Classification of Operators
a) Arithmetic Operators
b) Relational Operators
c) Logical Operators
d) Conditional Operators
e) Increment & Decrement Operators
f) Comma Operator
g) Arrow Operator
h) Assignment Operators
a) Arithematic Operators
Arithematic Expression : An expression containing
arithematic operator in between with two operands.
/* Program for Arithematic Operations*/
#include<stdio.h>
int main( )
{
int a,b;
printf("Enter the two Values\n");
scanf("%d%d", &a, &b);
printf(“a+b is %d\n“, (a+b));
printf(“a-b is %d\n“, (a-b));
printf(“a*b is %d\n“, (a*b));
printf(“a/b is %d\n“, (a/b));
printf(“a%b is %d\n“, (a%b));
printf(“a^b is %d\n“, (a^b));
return 0;
}
Output
4
2
a + b is 6
a - b is 2
a * b is 8
a / b is 2
a % b is 0
a ^ b is 16
b) Relational Operators
Binary Operators (or) Boolean Operators
Produces an integer result
Condition True : Integer value is 1
Condition False : Integer value is 0
Compares
Values between two variables
Values between variables and constants
1.15 Operators in C Contd…
b) Relational Operators Contd…
Relational Expression / Boolean Expression : An
expression containing a relational operator
b) Relational Operators Contd…
Consider a = 10 and b =4. The relational expression returns
the following integer values
#include<stdio.h>
int main( )
{
int a,b;
printf("Enter the two Values\n");
scanf("%d%d", &a, &b);
printf(“a>b is %d\n“, (a>b));
printf(“a<b is %d\n“, (a<b));
printf(“a>=b is %d\n“, (a>=b));
printf(“a<=b is %d\n“, (a<=b));
printf(“a==b is %d\n“, (a==b));
printf(“a!=b is %d\n“, (a!=b));
return 0;
}
Output
4
2
a > b is 1
a < b is 0
a > = b is 1
a < = b is 0
a = = b is 0
a ! = b is 1
1.15 Operators in C Contd…
c) Logical Operators
Combines two or more relations
Used for testing one or more conditions
1.15 Operators in C Contd…
c) Logical Operators Contd…
Logical Expression / Compound Relational Expression :
An expression which combines two or more relational
expression
#include<stdio.h>
int main( )
{
int age,height;
printf("Enter Age of Candidate:\n");
scanf("%d", &age);
printf(“Enter Height of Candidate:\n“);
scanf("%d", &height);
if ((age>=18) && (height>=5))
printf(“The Candidate is Selected”);
else
printf(“Sorry, Candidate not Selected”);
return 0;
}
Output 1
Enter Age of Candidate: 18
Enter Height of Candidate: 6
The Candidate is Selected
Output 2
Enter Age of Candidate: 19
Enter Height of Candidate: 4
Sorry, Candidate not Selected
1.15 Operators in C Contd…
d) Conditional Operators
? and are the Conditional Operators
Also called as Ternary Operators
Shorter form of if-then-else statement
Syntax Expression 1 ?Expression 2 : expression 3
#include <stdio.h>
int main( )
{
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d%d%d",&a, &b, &c);
max = (a > b && a > c) ? a : (b > c) ? b : c;
printf("\n Maximum between %d, %d and %d = %d", a, b, c, max);
return 0;
}
Output
Enter three numbers: 30 10 40
Maximum between a, b and c = 40
1. 15 Operators in C Contd…
e) Increment and Decrement Operators
Increment and decrement operators are unary operators that add or
subtract one from their operand
C languages feature two versions (pre- and post-) of each
operator
Operator placed before variable (Pre)
Operator placed before variable (Post)
The increment operator is written as ++ and the decrement
operator is written as --
1. 15 Operators in C Contd…
a) Increment and Decrement Operators Contd…
Classification
Pre Increment Operator
Post Increment Operator
Pre Decrement Operator
Post Decrement Operator
1. 15 Operators in C Contd…
a) Increment and Decrement Operators Contd…
Syntax
Examples
++count, ++a, ++i, ++count
Count++, a++, i++, count++
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Output
12345
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In this program, value of i “0” is compared with 5 in while
expression.
Step 2 : Then, value of “i” is incremented from 0 to 1 using post-
increment operator.
Step 3 : Then, this incremented value “1” is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes false and
output is displayed as “1 2 3 4 5”.
/* Program for Pre Increment*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 1;
while (++i<5)
{
printf(“%d”, i );
} }
getch ( );
}
Output
1234
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In above program, value of “i” is incremented from 0 to 1 using pre-
increment operator.
Output
98765
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In this program, value of i “10” is compared with 5 in
while expression.
Step 2 : Then, value of “i” is decremented from 10 to 9 using post-
decrement operator.
Step 3 : Then, this decremented value “9” is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes
false and output is displayed as “9 8 7 6 5”.
/* Program for Pre Decrement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 1;
while (--i<5)
{
printf(“%d”, i);
} }
getch ( );
}
Output
9876
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In above program, value of “i” is decremented from 10 to
9 using pre-decrement operator.
Step 2 : This decremented value “9” is compared with 5 in while
expression.
Step 3 : Then, this decremented value “9” is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes false
and output is displayed as “9 8 7 6”.
1. 15 Operators in C Contd…
b) Comma Operator
Special operator which separates the declaration of multiple
variables
Has Lowest Precedence i.e it is having lowest priority so it is
evaluated at last
Returns the value of the rightmost operand when multiple
comma operators are used inside an expression
Acts as Operator in an Expression and as a Separator while
Declaring Variables
1. 15 Operators in C Contd…
f) Comma Operator
#include<stdio.h>
int main( )
{
int i, j;
i=(j=10, j+20);
printf(“i = %d\n j = %d\n” , i,j );
return 0;
}
1. 15 Operators in C Contd…
g) Arrow Operator (->)
Arrow operator is used to access the structure members
when we use pointer variable to access it
Examples
num = 25; age = 18; pi = 31.4; area = 3.14 * r * r;
1. 15 Operators in C Contd…
Shorthand Assignment Operators
Simple Assignment
Shorthand Operator
Operator
a=a+1 a+=1
a=a–1 a-=1
a=a*2 a*=2
a=a/b a/=b
a=a%b a%=b
c = c * (a + b) c *= (a + b)
b = b / (a + b) b /=(a + b)
/* Program for Assignment Operations*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
a = 11;
a+ = 4;
printf(“Value ofA is %d\n”,a);
a = 11;
a- = 4;
printf(“Value ofA is %d\n”,a);
a = 11;
a* = 4;
printf(“Value ofA is %d\n”,a);
a = 11;
a/ = 4;
printf(“Value ofA is %d\n”,a);
a = 11;
a% = 4;
printf(“Value ofA is %d\n”,a);
getch ( );
}
Output
Value ofA is 15
Value ofA is 7
Value ofA is 44
Value ofA is 2
Value ofA is 3
1. 16 Single Line and Multiline Comments Contd…
a) Single Line Comment
Represented by double slash \\
#include<stdio.h>
int main( ){
//printing information
printf("Hello C");
return 0;
}
1. 16 Single Line and Multiline Comments Contd…
b) Multi-Line Comment
Represented by slash asterisk \* ... *\
#include<stdio.h>
int main( ){
/*printing information
Multi Line Comment*/
printf("Hello C");
return 0;
}
1. 16 Single Line and Multiline Comments
Comment – Definition
Used to provide information about lines of code
Provide clarity to the C source code
Allows others to better understand what the code was
intended to
Helps in debugging the code
Important in large projects containing hundreds or
thousands of lines of source code
Types – Single line and multiline comment
1. 16 Single Line and Multiline Comments Contd…
Single-Line Comments Multi-Line Comment
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=++i;
printf("x: %d",x);
printf("i: %d",i);
return 0;
}
Output
x: 11
i: 11
/* Expresssions using Pre-Decrement Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
return 0;
}
Output
x: 9
i: 9
/* Expresssions using Post-Increment Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=i++;
printf("x: %d",x);
printf("i: %d",i);
return 0;
}
Output
x: 10
i: 11
/* Expresssions using Post-Decrement Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
return 0;
}
Output
x: 10
i: 9
/* Expresssions using Increment / Decrement Operators*/
#include<stdio.h>
int main( )
{
int p,q,x,y;
printf(“Enter the value of x \n”);
scanf(“%d” ,&x);
printf(“Enter the value of y \n”);
scanf(“%d” ,&y);
printf(“x=%d\ny=%d\n”,x,y);
p=x++;
q=y++;
printf(“x=%d\ty=%d\n”,x,y);
printf(“x=%d\tq=%d\n”,p,q);
p=--x;
q=--y;
printf(“x=%d\ty=%d\n”,x,y);
printf(“p=%d\tq=%d\n”,p,q);
return 0;
}
Output
Enter the value of x 10
Enter the value of y 20
x = 10
y = 20
x = 11 y = 21
p = 10 q = 20
x = 10 y = 20
p = 10 q = 20
1.19 Expressions using Conditional Operator
Any operator is used on three operands or variable is known
as Ternary Operator
It can be represented with ? : . It is also called as conditional
operator
/* Program for Printing Odd or Even Number*/
#include<stdio.h>
int main( )
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even\n"):printf("Odd");
}
Output
Enter the Number : 10
Even
/* Program for Eligibility to Vote*/
#include<stdio.h>
int main( )
{
int age;
printf(" Please Enter your age here: \n ");
scanf(" %d ", &age);
(age >= 18) ? printf(" You are eligible to Vote ") : printf(" You are not
eligible to Vote ");
return 0;
}
Output
Please Enter your age here: 19
You are eligible to Vote
/* Program for Finding Biggest of 2 Numbers*/
#include<stdio.h>
int main( )
{
int a, b, max;
printf("Enter a and b: ");
scanf("%d%d", &a, &b);
max = a > b ? a : b; printf("Largest of the two numbers = %d\n", max);
return 0;
}
Output
Enter a and b: 10 20
Largest of the two numbers = 20
1.20 Expressions using Assignment Operator
Assignment Operator is used to assign value to an variable
Assignment Operator is denoted by equal to sign
Assignment Operator is binary operator which operates on two
operands
Assignment Operator have Two Values – L-Value and R-Value
Operator = copies R-Value into L-Value
Assignment Operator have lower precedence than all available
operators but has higher precedence than comma Operator
1.21 L-Value and R-Value of Expression
a) L-Value stands for left value
L-Value of Expressions refer to a memory locations
In any assignment statement L-Value of Expression must be a
container(i.e. must have ability to hold the data)
Variable is the only container in C programming thus L Value
must be any Variable.
L Value cannot be a Constant, Function or any of the available
data type in C
1.21 L-Value and R-Value of Expression Contd…
Diagram Showing L-Value of Expression :
1.21 L-Value and R-Value of Expression Contd…