Operators Prof.(Dr.
) Ganesh Gupta
Course Outcomes
CO
Number Course Outcomes
CO1 demonstrate the algorithm, Pseudo-code and flow chart for
the given problem.
CO2 develop better understanding of basic concepts of C
programming.
CO3 create and implement logic using array and function.
CO4 construct and implement the logic based on the concept of
strings and pointers.
CO5 apply user-defined data types and I/O operations in file.
CO6 design and develop solutions to real world problems using C.
2
Scheme of Evaluation
CA MTE ETE
Weightage
Distribution
30% 20% 50%
3
• Space for visual (size 24)
CONTENTS
• Arithmetic Operator
• Relational Operator
• Logical Operator
• Assignment Operator
• Increment and Decrement
Operator
• Conditional Operator
• Misc Operator
4
Operators
• An operator is a symbol that tells the compiler to perform certain mathematical or
logical manipulations.
• Operators are used in program to manipulate data and variables. The data items
that operators act upon are called operands.
• Some operators require two operands, while others act upon only one operand.
The operators are classified into unary, binary and ternary depending on whether
they operate on one, two or three operands respectively.
11-11-2022 5
Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operator
6. Bitwise Operators
7. Conditional Operators
8. Some Special Operators
11-11-2022 6
Arithmetic Operator
• C programming language provides all basic arithmetic operators: +, -, *, / and %.
Operator Meaning
+ Addition on unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo
11-11-2022 7
Arithmetic Operator
#include<stdio.h>
int main()
{
int a,b;
printf("enter the value of a and b");
scanf("%d %d",&a,&b); //Input the value of a and b
printf("Result of a+b is %d\n",a+b); //Addition of a and b
printf("Result of a-b is %d\n",a-b); //Substraction of and b
printf("Result of a*b is %d\n",a*b); //Multiplication of a and b
printf("Result of a/b is %d\n",a/b); // Division of A/B
printf("Result of a%b is %d\n",a%b); //Remainder of division
return 0;
}
11-11-2022 8
Relational Operator
• Relational operator compares between two operands and returns in true and false.
• Operators are as follows:
Operator Meaning
> Greater then
< Less then
>= Greater then equal to
<= Less then equal to
== Equal to
!= Not equal to
11-11-2022 9
Relational Operator
#include <stdio.h>
int main()
{
int a,b,c;
printf("enter the value of a,b and c");
scanf("%d %d %d",&a,&b, &c);
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
11-11-2022 10
Relational Operator
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
11-11-2022 11
Logical Operator
• Logical operator is used to compare or evaluate logical and relational expressions.
• Operators are as follows:
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
11-11-2022 12
Logical Operator
#include <stdio.h>
int main()
{
int a,b,c, result;
printf("enter the value of a,b and c");
scanf("%d %d %d",&a,&b, &c);
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
11-11-2022 13
Logical Operator
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
11-11-2022 14
Assignment Operator
• Assignment operators are used to assign result of an expression to a variable.
• ‘=’ is the assignment operator in C.
• You can use assignment operator for multiple assignments as follows:
• x=y=z=2;
11-11-2022 15
Assignment Operator
#include <stdio.h>
int main()
{
int a , c;
printf("Enter the value of a");
scanf("%d",&a);
c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
11-11-2022 16
Assignment Operator
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;
printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);
return 0;
}
11-11-2022 17
Increment and Decrement
Operator
Increment operator(++)
• The increment operator ++ adds 1 to the operand.
• Increment operator are of two types:
✓ Pre increment
✓For example x=10; ++x; 11
✓Post increment
✓For example x=10; x++; 10
11-11-2022 18
Pre-Increment & Pre-Decrement
#include <stdio.h>
int main()
{
int a, b;
printf("enter the value of a,b");
scanf("%d %d",&a,&b);
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
return 0;
}
11-11-2022 19
Increment and Decrement
Operator
Decrement operator(--)
• The decrement operator -- subtracts 1 from the operand.
• Decrement operator are of two types:
✓ Pre decrement
✓For example x=10; --x; 9
✓Post decrement
✓For example x=10; x--; 10
11-11-2022 20
Post-Increment & Post-Decrement
#include <stdio.h>
int main()
{
int a, b;
printf("enter the value of a,b");
scanf("%d %d",&a,&b);
printf("a++ = %d \n", a++);
printf("b-- = %d \n", b--);
return 0;
}
11-11-2022 21
Conditional Operator
• Main features of Conditional operator are as follows:
• There are three operands
• It works from left to right.
• The operator pair “?” and “:” is known as conditional operator.
• Syntax: expression1 ? expression2 : expression3 ;
• Condition? True part: False part
• Example: a = 3 ; b = 5 ;
• x = (a > b) ? a : b ;
11-11-2022 22
Conditional Operator
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year or not \n");
scanf("%d", &year);
(year%4==0 && year%100!=0) ? printf("Leap year") :
((year%400 ==0 ) ? printf("Leap Year") : printf("not a leap year"));
}
11-11-2022 Computer Programming 20CST111 23
Conditional Operator
Q1. Use conditional operator to find the greatest among
three numbers
11-11-2022 24
Special Operator
• C programming supports special operators like comma operator, sizeof operator,
pointer operators (& and *) and member selection operators (. and ->). We will
discuss Comma operator and sizeof operator later.
11-11-2022 25
Comma Operator
• The comma operator can be used to link the related expressions together
• Evaluation of comma operator is from left to right.
For example:
• x = (a = 2, b = 4, a+b)
11-11-2022 26
Comma Operator
Example1:
#include<stdio.h>
int main()
{
int i;
i = 1,2,3;
printf("i:%d\n",i);
return 0;
}
Output:1
11-11-2022 Computer Programming 20CST111 27
Comma Operator
Example2:
#include<stdio.h>
int main()
{
int i;
i = (1,2,3);
printf("i:%d\n",i);
return 0;
}
Output: 3
11-11-2022 28
Size of Operator
• The sizeof operator is usually used with an operand which may be variable,
constant or a data type qualifier.
• It calculates the size of data ie. how many bit a specific data is having.
• Syntax: sizeof(<variable>);
• Example: x = sizeof (a);
11-11-2022 29
Sizeof Operator
#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;
}
11-11-2022 30
Summary
Important points to remember:
•C operators operates on one or more operands to produce a value.
•The operators that take one operand are called unary operators.
•The operators that require two operands are called binary operators.
•The operator that accepts three operands is called ternary operator. C has
only one ternary operator.
Assessment
Q1. Predict the output
int main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
(A) 10
(B) 20
(C) 30
(D) Compilation Error
11-11-2022 Computer Programming 20CST111 32
Assessment
Q2. [Postfix] operator has the highest precedence.
Q3. What will be the output of the C program?
#include<stdio.h>
int main()
{
int x = 2;
(x & 1) ? printf("true") : printf("false");
return 0;
}
A. Compilation error
B. true
C. false
D. Runtime error
11-11-2022 Computer Programming 20CST111 33
Assessment
Q5. Your task is to take two numbers of int data type, two numbers of float data type as
input and output their sum:Declare variables: two of type int and two of type float.Read
lines of input from stdin (according to the sequence given in the 'Input Format' section
below) and initialize your variables.Use the and operator to perform the following
operations: Print the sum and difference of two int variable on a new line.Print the sum
and difference of two float variable rounded to one decimal place on a new line.
Sample Input
10 4
4.0 2.0
Sample Output
14 6
6.0 2.0
Solve the above problem in c.
11-11-2022 Computer Programming 20CST111 34
References
S.No Title Content link
1 Book Programming in C by Reema Thareja.
2 Book Programming with C (Schaum's Outline Series) by Byron
Gottfried Jitender Chhabra, Tata McGraw Hill.
3 Book The C Programming Language by Brian W. Kernighan, Dennis
Ritchie, Pearson education.
4 Book Programming in ANSI C by E. Balaguruswamy, Tata McGraw
Hill.
5 Weblink https://www.tutorialspoint.com/cprogramming/c_operators.htm
6 Weblink https://www.programiz.com/c-programming
7 Weblink https://fresh2refresh.com/c-programming/
References
S.No Title Content link
8 Weblink https://www.studytonight.com/c/
9 Weblink https://www.javatpoint.com/c-operators
10 Video Link https://www.youtube.com/watch?v=t9WKOcRB63Q&lis
t=PLJ5C_6qdAvBFzL9su5J-FX8x80BMhkPy1
11 Video Link https://www.youtube.com/watch?v=XTiIiI-
LOY8&list=PLJvIzs_rP6R73WlvumJvCQJrOY3U5zq1j
12 Online Course Link https://www.coursera.org/
13 Online Course Link https://www.udemy.com/
14 Online Course Link https://www.niit.com/
THANK YOU