List all operators used in C and explain any three operators with example.


Subject Name: PROGRAMMING FOR PROBLEM SOLVING
Subject Code:3110003
Question: List all operators used in C and explain any three operators with example.
List all operators:
  1. Arithmetic Operators.
  2. Relational Operators.
  3. Logical Operators.
  4. Assignment Operators.
  5. Increment and Decrement Operators.
  6. Conditional Operator.
  7. Bitwise Operators.
  8. Special Operators.

1-Arithmetic Operators:
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

Example 1: Arithmetic Operators
  1.    // Working of arithmetic operators
  2.    #include <stdio.h>
  3.    int main()
  4.    {
  5.     int a = 9,b = 4, c;
  6.     
  7.     c = a+b;
  8.     printf("a+b = %d \n",c);
  9.     c = a-b;
  10.     printf("a-b = %d \n",c);
  11.     c = a*b;
  12.     printf("a*b = %d \n",c);
  13.     c = a/b;
  14.     printf("a/b = %d \n",c);
  15.     c = a%b;
  16.     printf("Remainder when a divided by b = %d \n",c);
  17.     
  18.     return 0;
  19.    }
Output:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
  • The operators +, - and * computes addition, subtraction, and multiplication respectively as you might have expected.
  • In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
  • It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
  • The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers.
  • Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,
// Either one of the operands is a floating-point number
a/b = 2.5  
a/d = 2.5  
c/b = 2.5  

// Both operands are integers
c/d = 2

2-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.

Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0

Example 2: Relational Operators
  1.     // Working of relational operators
  2.     #include <stdio.h>
  3.     int main()
  4.     {
  5.     int a = 5, b = 5, c = 10;
  6.     printf("%d == %d is %d \n", a, b, a == b);
  7.     printf("%d == %d is %d \n", a, c, a == c);
  8.     printf("%d > %d is %d \n", a, b, a > b);
  9.     printf("%d > %d is %d \n", a, c, a > c);
  10.     printf("%d < %d is %d \n", a, b, a < b);
  11.     printf("%d < %d is %d \n", a, c, a < c);
  12.     printf("%d != %d is %d \n", a, b, a != b);
  13.     printf("%d != %d is %d \n", a, c, a != c);
  14.     printf("%d >= %d is %d \n", a, b, a >= b);
  15.     printf("%d >= %d is %d \n", a, c, a >= c);
  16.     printf("%d <= %d is %d \n", a, b, a <= b);
  17.     printf("%d <= %d is %d \n", a, c, a <= c);
  18.     return 0;
  19.     }
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

3-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.


Operator Meaning Example
&& Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0.


Example 3: Logical Operators
  1.     // Working of logical operators
  2.     #include <stdio.h>
  3.     int main()
  4.     {
  5.     int a = 5, b = 5, c = 10, result;
  6.     
  7.     result = (a == b) && (c > b);
  8.     printf("(a == b) && (c > b) is %d \n", result);
  9.     result = (a == b) && (c < b);
  10.     printf("(a == b) && (c < b) is %d \n", result);
  11.     result = (a == b) || (c < b);
  12.     printf("(a == b) || (c < b) is %d \n", result);
  13.     result = (a != b) || (c < b);
  14.     printf("(a != b) || (c < b) is %d \n", result);
  15.     result = !(a != b);
  16.     printf("!(a == b) is %d \n", result);
  17.     result = !(a == b);
  18.     printf("!(a == b) is %d \n", result);
  19.     
  20.     return 0;
  21.     }
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program:
  • (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
  • (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
List all operators used in C and explain any three operators with example

Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post