Operators in C programming

An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. It is used in programs to manipulate data and variables. In a simple words operators are words or symbol that cause a program to do something to variable.

Example :
a+b 
Here, a and b are operands or data items while + is an operator.  
    
C operators are classified into number of categories namely:

1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Miscellaneous operator

1. Arithmetic Operator

Arithmetic operators perform mathematical calculations with the operands.

Operator Description
+ Adds two operands.
- Subtracts second operand from the first.
* Multiplies operands.
/ Divides numerator by de-numerator.
% (Modulo Division) Gives remainder after division of the operands.
++ (Increment Operator) Increases the integer value by one.
-- (Decrement Operator) Decreases the integer value by one.

2. Relative Operator

Relative operators are used to compare one variable with another in C programming. Hence, we can say it comparision operator too. It will produce a boolean value (true or false) depending upon the condition given.

Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to

3. Logical operators

Logical operators are used to combine multiple relative operators in C programming. These operators are highly useful to check more than one conditions before further execution of the program. These operators also return a boolean value.

Operator Meaning
&& AND (All conditions must be true)
|| OR (One of the given conditions must be true)
! NOT (Given condition must not be true)

4. Bitwise operators: 

Bitwise operators is utilized to control the information of data at bit level. These operators are utilized for testing the bits, or moving them right or left. Bitwise operators may not be applied to Float or two fold. 

Operator Description Example
& Binary AND Operator (Copies a bit to the result if it exists in both operands).

(x & y) = 1

0001 0001

0000 0101

0000 0001

Only the last bit value matches in both values. Hence, the result will be 0000 0001 which is qual to 1.
| Binary OR Operator (Copies a bit if it exists in either operand).

(x | y) = 21

0001 0001

0000 0101

0001 0101

Getting bit values from both operands. Hence, the result will be 0001 0101 which is qual to 21.
^ Binary XOR Operator (Copies the bit if it is set in one operand but not both).

(x ^ y) = 20

0001 0001

0000 0101

0001 0100

Rejecting values that matches in both operands. Hence, the result will be 0001 0100 which is equal to 20.
~ (Binary Complement Operator) (Flips the last two digits and gives a negative value).

(~x) = -18

0001 0001

0001 0010

Flips the last two binary digits. Hence the result will be -0001 0010 which is equal to -18. If the last two digits are 00, it will be flipped to 01.
<< Binary Left Shift Operator (Left operand value is moved left by the number of bits specified by the right operand).

(x << 2) = 68

0001 0001

0100 0100

Shifts the binary value by two places to the left, removing the first two digits and adding 2 zeros at the end. Hence the result will be 0100 0100 which is equal to 68.
>> Binary Right Shift Operator (Left operand value is moved right by the number of bits specified by the right operand).

(x >> 2) = 4

0001 0001

0000 0100

Shifts the binary value by two places to the right, adding 2 zeros at the beginning and removing the last two digits. Hence the result will be 0000 0100 which is equal to 4.
#include <stdio.h>

main() {

  int x = 17;  /* 17 = 0001 0001 (binary value)*/
  int y = 5; /* 5 =  0000 0101 */
  int z;

  z = x & y;       /* 1 = 0000 0001 */
  printf("Value of x&y is %d\n", z );

  z = x | y;       /* 21 = 0001 0101 */
  printf("Value of x|y is %d\n", z );

  z = x ^ y;       /* 20 = 0001 0100 */
  printf("Value of x^y is %d\n", z );

  z = ~x;          /*-18 = -0001 0010 */
  printf("Value of ~x is %d\n", z );

  z = x << 2;     /* 68 = 0100 0100 */
  printf("Value of x<<2 is %d\n", z );

  z = x >> 2;     /* 4 = 0000 0100 */
  printf("Value of x>>2 is %d\n", z );
}

Output

Value of x&y is 1
Value of x|y is 21
Value of x^y is 20
Value of ~x is -18
Value of x<<2 is 68
Value of x>>2 is 4

5. Assignment Operators

Assignment operators are used to assign definite value or the result of an expression to a variable. 

Operator Description Example
= Simple assignment (Assigns value of right side operand to the left side operand). x = 10
+= Add AND (Adds the right operand to the left operand and assign the result to the left operand). x += y => x = x+y
-= Subtract AND (Subtracts the right operand from the left operand and assigns the result to the left operand). x -= y => x = x-y
*= Multiply AND (Multiplies the right operand with the left operand and assigns the result to the left operand). x *= y => x = x*y
/= Divide AND (Divides the left operand with the right operand and assigns the result to the left operand). x /= y => x = x/y
%= Modulus AND (Takes modulus using two operands and assigns the result to the left operand). x %= y => x = x%y
<<= Left shift AND x <<= 2 => x = x << 2
>>= Right shift AND x >>= 2 => x = x >> 2
&= Bitwise AND x &= 2 => x = x & 2
^= Bitwise exclusive OR x ^= 2 => x = x ^ 2
|= Bitwise inclusive OR x |= 2 => x = x | 2

6. Miscellaneous Operator  

Along with the operators stated above, C programming also a supports a couple of other operators.

sizeof() - Returns the size of variable.

& - Returns the address of variable.

* - Returns the pointer of variable.

?: - Ternary operator

Ternary operator is the shorthand for if else conditional statement in C programming.

Example :
char a[] = "x is smaller";
char b[] = "y is smaller"; 
s = x < y ? a : b  

Here, x and y are compared first. If the condition is true, the value of a will be assigned to s else the value of b will be assigned to s.

#include<stdio.h>

int main ()
{
  int a,b,s;
  printf ("\n Enter two numbers :");
  scanf ("%d%d",&a,&b);
  s = a >= b ? a : b;
  printf ("\n The biggest number is :%d ",s);
  return 0;
}

0 Like 0 Dislike 0 Comment Share

Leave a comment