7 Operators (1)
7 Operators (1)
Course Objectives
CO5 Design and develop modular programs for real world problems
using control structure and selection structure.
3
ASSESSMENT PATTERN
The performance of students is evaluated as follows:
Theory Practical
Continuous Internal Semester End Continuous Internal Semester End
Components Assessment (CAE) Examination (SEE) Assessment (CAE) Examination (SEE)
Marks 40 60 60 40
Total Marks 100 100
4
• Space for visual (size 24)
CONTENTS
• Arithmetic Operator
• Relational Operator
• Logical Operator
• Assignment Operator
• Increment and Decrement
Operator
• Conditional Operator
• Misc Operator
15/12/2024 5
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.
15/12/2024 6
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
15/12/2024 7
Arithmetic Operator
• C programming language provides all basic arithmetic operators: +, -, *, / and %.
Operator Meaning
+ Addition on unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo
15/12/2024 8
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;
}
15/12/2024 9
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
15/12/2024 10
Relational Operator
#include <stdio.h>
int main(void)
{
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);
15/12/2024 11
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;
}
15/12/2024 12
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
15/12/2024 13
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);
15/12/2024 14
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;
}
15/12/2024 15
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;
15/12/2024 16
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);
15/12/2024 17
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;
}
15/12/2024 18
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
15/12/2024 19
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;
}
15/12/2024 20
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
15/12/2024 21
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;
}
15/12/2024 22
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 ;
15/12/2024 23
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"));
}
15/12/2024 24
Conditional Operator
15/12/2024 25
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 in Unit-1.
15/12/2024 26
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)
15/12/2024 27
Comma Operator
Example1:
#include<stdio.h>
int main()
{
int i;
i = 1,2,3;
printf("i:%d\n",i);
return 0;
}
Output:1
15/12/2024 28
Comma Operator
Example2:
#include<stdio.h>
int main()
{
int i;
i = (1,2,3);
printf("i:%d\n",i);
return 0;
}
Output: 3
15/12/2024 29
Sizeof 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);
15/12/2024 30
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;
15/12/2024 31
Summary
15/12/2024 33
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
15/12/2024 34
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
15/12/2024 35
Assessment
Q4. Print the output of the program
#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 2;
y = ++x * ++x;
cout << x << y;
x = 2;
y = x++ * ++x;
cout << x << y;
return 0;
}
15/12/2024
Ans: 41649 36
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.
15/12/2024 37
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/