Operators in C:Types and Examples

What is an operator??

Operator is a character which performs some operation on operands.For More

Ex: a+b
      Here a,b are operands and '+' is an operation.

Based on number of operands required to perform operation, the operator can be a

  • Unary Operator
  • Binary Operator
  • Ternary Operator

Unary Operator:

     The operator which takes only one operand are comes under unary operators
    Ex:Increment and decrement operators

Binary Operator:

 The operator which takes two operands are comes under binary operators
    Ex: Arithmetic Operators

Ternary Operator:

Ternary operator is also known as conditional operator

Based on operations,the operators are
  1. Arithmetic Operators
  2. Assignment Operator
  3. Relational Operators
  4. Logical Operators
  5. Bit-wise Operators
  6. Increment and Decrement Operators
  7. Conditional Operator
  8. Special Operators

Arithmetic Operators: 

Arithmetic Operators are used to perform arithmetic operations between the operands
There are 5 Arithmetic operators.They are 
  •  Addition Operator ("+")
  •  Subtraction Operator ("-")
  •  Multiplication Operator ("*")
  •  Division Operator ("/")
  •  Modulo Division Operator ("%")
Ex: c=a+b
      Values of 'a' and 'b' are added and the sum will be stored in 'c'.
      c=a-b
      value of 'b' is subtracted from 'a' and the result will be stored in 'c'.
      c=a*b
      Values of 'a' and 'b' are multiplied and product will be stored in 'c'.
      c=a/b
      'a' is divided by 'b' and the coefficient will be stored in 'c'.
     c=a%b
     'a' is divided by 'b' and the remainder will be stored in 'c'.

Assignment Operator: 

There is only one assignment operator in C ('=') which is used to assign value to a variable
Ex: a=10
      10 will be assigned to 'a'
      similarly a=b
      value of 'b' is assigned to 'a'

Relational Operators:

Relational operators are used for comparison. If you want to compare two variables or values then you can use relational operators.Various Relational operators in C are:
  • Equals ("==") : Used to Check whether two values are equal or not
  • Greater than(">") :Used to check whether the first operand is greater than second operand or not
  • Less than("<") :Used to check whether the first operand is less than second operand or not
  • Greater than or equals(">="):Used to check whether the first operand is greater than or equals to second operand or not
  • Less than or equals("<="):Used to check whether the first operand is less than or equals to second operand or not
Logical Operators:
Logical operators are used to find the logical relationship between the operands.There are three logical operators in C. They are
  • Logical AND ("&&"):Logical AND operator returns true("1") when both the statements are true otherwise it returns false("0")
  • Logical OR("||"):Logical OR operator returns false when both the statements are false("1") otherwise it returns true("1")
  • Logical NOT("!"):Logical NOT is used to negate the value.It returns true("1") if operand is false("0")
For more about Logical Operators

Bit-wise Operators:

Bit Wise Operators are used perform operations on bits.Bit Wise Operators are
  • Bit-wise AND("&"): Works same as logical AND but it works on bits.
  • Bit-wise OR("|"):Works same as logical OR but it works on bits.
  • Bit-wise NOT("~"):Works same as logical NOT but it works on bits.
  • Bit-wise XOR("^"):Returns '0' if both bits are same otherwise returns '1'.
  • Left Shift("<<"):Shifts all the bits by a specified number of places to left.
  • Right Shift(">>"):Shifts all the bits by a specified number of places to right.

Increment and Decrement Operators:

These are unary operators.Increment operators used to increase the value of the variable by 1.Increment can be Pre-Increment("++a") or Post-Increment("a++").
In Pre-Increment:Value is incremented first then substituted in the expression
 Ex: int a=10;
       c=++a;
       Here Value in 'c' is 11 and value in 'a' is also 11;
Where as in Post-Increment first the value is substituted in the expression then incremented
Ex: int a=10;
     c=a++;
 Here value in 'c' is 10 and value in 'a' is 11.

Decrement operator also works same as increment operator but instead of increasing the value it decreases by 1.

Conditional Operator:

To know about Conditional Operator

Special Operators:

Special Operators in C are 

Address of Operator('&'):Used to get the address of a variable.

Size of Operator:used to get the size of a variable

Dot operator('.'):Used in structures to access the structure members
    
Comma Operator(','):Used to separate the variables at the time of declaration.

Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment