Operators in C Language

This C Tutorial Explains Operators & Various Types of Operators in C Language.

What is Operator in C?
An Operator is something that performs some fixed, predefined action on its operands.

For example:

    15 + 34; /* '+' operator adds 34 to 15 */
    x * y;   /* '*' multiplication operator here */
    -x;      /* '-' minus operator changes the sign of its operand */
    ++y;     /* increment operator */

C offers huge set of operators comparative to other programming languages especially to meet every possible programming need. There are some 40 different types of operators in C. Further, these operators depending upon their actions on the number of operands at a time are categorized as Unary Operators which have one operand, Binary Operators which have two operands and Ternary Operators which takes three operands. For example:

/* diff_operators.c: Program displays some common c operators */
 
#include <stdio.h>
int main(void)
{
    int num1, num2;
    int sum;
 
    num1 = num2 = 10;  /* num1 & num2 both r 10 */
 
    /* '=' assignment operator assigns value of exp/variable at its right */
    /* to var at its left. '=' operator associates from right to left */
 
    sum = num1 + num2;
 
    /* Plus '+' is a binary operator. It adds value of num2 to num1. */
     * Result is assigned to sum by the assignment operator. */
 
    -num1; 
 
    /* Minus '-' is here a Unary Operator. */
    /* It changes sign of its operand. num1 is now -10. */
 
    --num1;
 
    /* '--' is a unary operator called decrement operator. */
    /* comes in two versions: prefix & postfix. */
    /* it reduces its operand value by 1. num1 now became -11. */
 
    return 0;
}

Types of C Operators & their Meaning:

  1. Arithmetic Operators
  2. Relational Operators
  3. Assignment Operators
  4. Logical Operators
  5. Conditional Operator/Ternary Operator
  6. Pointer-Related Operators
  7. Sign Operators
  8. Bitwise Operators
  9. Miscellaneous Operators
advertisement
advertisement

1. Arithmetic Operators

+ Operator
+ adds the value at its right to the value at its left.
+, as a unary operator, produces a value equal in magnitude (and of the same sign) to the operand to the right.

– Operator
– subtracts the value at its right from the value at its left.
-, as a unary operator, produces a value equal in magnitude (but opposite in sign) to the operand to the right.

* Operator
* multiplies the value at its right by the value at its left.

/ Operator
/ divides the value at its left by the value at its right. The answer is truncated if both operands are integers.

% Operator
% yields the remainder when the value at its left is divided by the value to its right (integers only).

++ Operator
++ adds 1 to the value of the variable to its right (prefix mode) or adds 1 to the value of the variable to its left (postfix mode).

— Operator
— is like ++, but it subtracts 1 to the value of the variable to its right (prefix mode) or subtract 1 to the value of the variable to its left (postfix mode).

advertisement

2. Relational Operators

Each of the following operators compares the value at its left to the value at its right:

    <	Less than
    <=	Less than or equal to
    ==	Equal to
    >=	Greater than or equal to
    >	Greater than
    !=	Unequal to

3. Assignment Operators

C has one basic assignment operator and several combination/arithmetic assignment operators. The = operator is the basic form.

advertisement

= assigns the value at its right to the lvalue on its left. lvalue is modifiable value. lvalue or modifiable lvalue refers to some location in the memory into which to hold some value.

Each of the following assignment operators updates the lvalue at its left by the value at its right, using the indicated operation (we use R–H for right-hand and L–H for left-hand):

  • += adds the R–H quantity to the L–H variable and places the result in the L–H variable.
  • -= subtracts the R–H quantity from the L–H variable and places the result in the L–H variable.
  • *= multiplies the L–H variable by the R–H quantity and places the result in the L–H variable.
  • /= divides the L–H variable by the R–H quantity and places the result in the L–H variable.
  • %= gives the remainder from dividing the L–H quantity by the R–H quantity and places the result in the L–H variable.
  • &= assigns bitwise & of L–H and R–H to the L–H quantity/l-H variable.
  • |= assigns bitwise | of L–H and R–H to the L–H quantity/L–H variable.
  • ^= assigns bitwise ^ of L–H and R–H to the L–H quantity/L–H variable.
  • >>= assigns L–H >> R–H to the L–H quantity and places the result in the L–H variable.
  • <<= assigns L–H << R–H to the L–H quantity and places the result in the L–H variable.

4. Logical Operators

There are three Logical Operators in C. They normally take relational expressions as operands. The ! operator takes one operand. The rest take two: one to the left, and one to the right.

    &&	  AND
    ||	  OR
    !	  NOT

5. The Conditional Operator/Ternary Operator

? : takes three operands, each of which is an expression. They are arranged this way:

expression1 ? expression2 : expression3

The value of the whole expression equals the value of expression2 if expression1 is true, and equals the value of expression3 otherwise.

6. Pointer-Related Operators

&” is the address operator. When followed by a variable name, & gives the address of that variable.
*” is the indirection or dereferencing operator. When followed by a pointer, * gives the value stored at the pointed-to address.

7. Sign Operators

” is the minus sign and reverses the sign of the operand.
+” is the plus sign and leaves the sign unchanged.

All the following bitwise operators, except ~, are binary operators:

8. Bitwise Operators

  • ~ Operator: It is the unary operator and produces a value with each bit of the operand inverted.
  • And Operator: & is And produces a value in which each bit is set to 1 only if both corresponding bits in the two operands are 1.
  • OR Operator: | is OR and produces a value in which each bit is set to 1 if either, or both, corresponding bits of the two operands are 1.
  • EXCLUSIVE OR Operator: ^ is EXCLUSIVE OR and produces a value in which each bit is set to 1 only if one or the other (but not both) of the corresponding bits of the two operands is 1.
  • Left Shift Operator: << is left-shift and produces a value obtained by shifting the bits of the left-hand operand to the left by the number of places given by the right-hand operand. Vacated slots are filled with zeros.
  • Right Shift Operator: >> is right-shift and produces a value obtained by shifting the bits of the left-hand operand to the right by the number of places given by the right-hand operand. For unsigned integers, the vacated slots are filled with zeros. The behaviour for signed values is implementation dependent.

9. Miscellaneous Operators

sizeof Operator:
sizeof yields the size, in units the size of a char value, of the operand to its right. Typically, a char value is 1 byte in size. The operand can be a type-specifier in parentheses, as in sizeof (float), or it can be the name of a particular variable, array, or so on, as in sizeof boys. A sizeof expression returns value of type size_t.

type Operator:
(type) is the cast operator and converts the value that follows it to the type specified by the enclosed keyword(s). For example, (float) 9 converts the integer 9 to the floating-point number 9.0.

comma Operator:
, is the comma operator; it links two or more expressions into one and guarantees that the leftmost expression is evaluated first. The value of the whole expression is the value of the right-most expression. This operator is typically used to include more information in a for loop, while loop control expression.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.