Lesson 3 C Operators
Lesson 3 C Operators
C
Operators
1
Programming Operators
C programming has various operators to perform tasks including
arithmetic, conditional and bitwise operations.
An operator is a symbol which operates on a value or a variable. For
example: + is an operator to perform addition.
C programming has wide range of operators to perform various
operations.
2
Types Of Operators
C language offers many types of operators. They
are,
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Conditional operators (ternary operators)
Increment/decrement operators
Special operators
3
Arithmetic Operators
C Arithmetic operators are used to perform mathematical
calculations like addition, subtraction, multiplication, division
and modulus in C programs.
4
Arithmetic Operators
// C Program to demonstrate the
working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
Output when the file arithmetic-1 is
printf("a+b = %d \n",c); run
c = a-b;
printf("a-b = %d \n",c); a+b = 13
c = a*b;
printf("a*b = %d \n",c);
a-b = 5
c=a/b; a*b = 36
printf("a/b = %d \n",c); a/b = 2
Remainder when a divided by b=1
c=a%b;
printf("Remainder when a
divided by b = %d \n",c);
return 0;
}
5
Arithmetic Operators:
In this example program, two values “40” and “20” are used to perform arithmetic operations such as
addition, subtraction, multiplication, division, modulus and output is displayed for each operation.
• #include <stdio.h>
• int main()
• {
• int a=40,b=20, add,sub,mul,div,mod;
• add = a+b;
OUTPUT
• sub = a-b;
• mul = a*b; Addition of a, b is : 60
• div = a/b; Subtraction of a, b is : 20
• mod = a%b; Multiplication of a, b is : 800
Division of a, b is : 2
• printf("Addition of a, b is : %d\n", add);
Modulus of a, b is : 0
• printf("Subtraction of a, b is : %d\n", sub);
• printf("Multiplication of a, b is : %d\n", mul);
• printf("Division of a, b is : %d\n", div);
• printf("Modulus of a, b is : %d\n", mod);
• }
6
Increment and decrement
operators
• C programming has two operators increment ++ and
decrement -- to change the value of an operand
(constant or variable) by 1.
• Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1.
• These two operators are unary operators, meaning they
only operate on a single operand.
7
Increment and decrement
operators
• // C Program to demonstrate the working of increment and decrement operators
• #include <stdio.h>
• int main()
• {
• int a = 10, b = 100; Output
• float c = 10.5, d = 100.5;
• printf("++a = %d \n", ++a); ++a = 11
--b = 99
++c = 11.50
• printf("--b = %d \n", --b);
- -d = 99.50
• printf("++c = %.2f \n", ++c);
• printf("--d = %.2f \n", --d);
• return 0;
• }
8
Assignment Operators
• An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =
• Operator Example Same as
• = a=b a=b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b
9
Assignment Operators
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Line 1 - Value of c is %d\n", c );
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
10
Relational Operators
• A relational operator checks the relationship between two operands.
• If the relation is true, it returns 1; if the relation is false, it returns value 0.
• Relational operators are used in decision making and loops.
Relational
Operators In Usage Description Example
C
> a>b a is greater than b 7 > 3 returns true (1)
< a<b a is less than b 7 < 3 returns false (0)
a is greater than or equal
>= a >= b 7 >= 3 returns true (1)
to b
11
Relational Operators
• /* C Relational Operations on integers */
• #include <stdio.h>
• int main()
• {
• int a = 9;
• int b = 4;
• printf(" a > b: %d \n", a > b);
• printf("a >= b: %d \n", a >= b);
• printf("a <= b: %d \n", a <= b);
• printf("a < b: %d \n", a < b);
• printf("a == b: %d \n", a == b);
• printf("a != b: %d \n", a != b);
• }
12
Relational Operators
• #include<stdio.h>
• int main()
• {
• int x = 12, y = 13;
• printf("x = %d\n", x);
• printf("y = %d\n\n", y);
• // x is greater than y
• printf("x > y : %d\n", x > y);
• // x is greater than or equal to y
• printf("x >= y : %d\n", x >= y);
• // x is smaller than y
• printf("x < y : %d\n", x < y);
• // x is smaller than or equal to y
• printf("x <= y : %d\n", x <= y);
• // x is equal to y
• printf("x == y : %d\n", x == y);
• // x is not equal to y
• printf("x != y : %d\n", x != y);
• // Signal to operating system everything works fine
• return 0;
• }
13
Logical Operators
• These operators are used to perform logical operations on the given expressions.
• There are 3 logical operators in C language.
– They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operators Example/Description
&& (logical (x>5)&&(y<5)
AND) It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical OR)
It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
! (logical NOT)
If “((x>5) && (y<5))” is true, logical NOT operator makes
it false
14
Conditional Operator (?:)
• Conditional operators return one value if condition is true and
returns another value if condition is false.
• This operator is also called as ternary operator.
• Syntax : (Condition? true_value: false_value);
• Example : (A > 100 ? 0 : 1);
• In above example, if A is greater than 100, 0 is returned else 1 is
returned. This is equal to if else conditional statements.
15
Conditional Operator
• #include <stdio.h>
• int main(){
• char February;
• int days;
• printf("If this year is leap year, enter 1. If not enter any integer: ");
• scanf("%c",&February);
• printf("Number of days in February = %d",days); If this year is leap year, enter 1. If not
• return 0; enter any integer: 1
Number of days in February = 29
• }
16
Conditional Operators Example
• #include <stdio.h>
• main() {
• int a = 5;
• int b = 20;
• int c ;
• if ( a && b ) {
• printf("Line 1 - Condition is true\n" );
• }
• if ( a || b ) {
• printf("Line 2 - Condition is true\n" );
• }
• /* lets change the value of a and b */
• a = 0;
• b = 10;
•
• if ( a && b ) {
• printf("Line 3 - Condition is true\n" );
• } else {
• printf("Line 3 - Condition is not true\n" );
• }
•
• if ( !(a && b) ) {
• printf("Line 4 - Condition is true\n" );
• }
•
• }
Conditional/Ternary Operators
• #include <stdio.h>
•
Output:
• int main() x value is 1
• { y value is 2
• int x=1, y ;
• y = ( x ==1 ? 2 : 0 ) ;
• printf("x value is %d\n", x);
• printf("y value is %d", y);
• }
The End
Thank You
19