Menu

Operators in C

  • The tokens that trigger some kind of operations are called operators. An operation requires operator and operands (s). Operands are the data on which the specified task is carried out.
  • They are classified in to three according to its operands.
    1. Unary (with one operand)
    2. Binary (with two operands)
    3. Ternary (with three operands)
  • Operators are again classified into
    1. Arithmetic operators
    2. Relational operators
    3. Bitwise operators
    4. Assignment operators
    5. Conditional operators
    6. Sizeof operators
    7. Comma operators
    8. Increment or decrement operators

Arithmetic Operators

The symbols (or tokens) that trigger computations in the program are called arithmetic operators. They are

Operators a b Evaluation Result
+addition 6 3 6+3 9
-substraction 6 3 6-3 3
*multiplication 6 3 6*3 18
/Division 6 3 6/3 2
% remainder 6 3 6%3 0

Relational Operators

These operators compare two quantities, that is, they determine the relation between two data. The result of the operation will be either TRUE (1) or FALSE (0).

Operators a b Evaluation Result
> greather than 6 3 6>3 TRUE
< less than 6 3 6<3 FALSE
>= Greather than or equal to 6 3 6>=3 TRUE
<= Less than or equal to 6 3 6<=3 FALSE
== equal to 6 3 6==3 FALSE
!= Not equal to 6 3 6!=3 TRUE

Logical Operators

Logical operators are used to combine relational expressions. The operands as well as the result of these operators are Boolean values (TRUE and / or FALSE). The logical AND (&&), Logical OR (||) and logical NOT (!) are the operators. Consider two relational operations with following output then,

Logical AND(&&) Logical OR(||) Logical NOT(!)
T&&T=T T||T=T !T=F
T&&F=F T||F=T !F=T
F&&T=F F||T=T  
F&&F=F F||F=F  

Bitwise operators

A bitwise operator is used to perform bitwise operations on bit patterns or binary numerals to perform bit manipulation.

Symbol Operator
& BitWise AND
| Bitwise Inclusive OR
^ Bitwise Exclusive OR
<< Left shift
>> Right shift
~ Bitwise NOT(1's complement,unary)

Conditional Operator (?:)

This operator returns a value depending on a condition. It is a ternary operator, which requires three operands to operate upon.

Syntax

         result=Condition1 ? Condition 2 : Condition 3;

Example:

        result=((a>b)?a:b);

If the value of a is greater than b the value of a will be stored in result otherwise the value of b will stored in b.

Sizeof() Operator

It is a unary operator that returns the number of bytes occupied by the variable or data type specified within parentheses

Example

  • sizeof (int) returns 2 or 4.
  • sizeof(float) returns 4.
  • sizeof(char) returns 1.

Comma Operator

This operator is used to separate group of expressions and evaluate from left to right in sequence.This operator has only limited use cases.

Example

int a=1,b=2,i=0

Increment / Decrement Operators

  • The Increment operator (++) adds 1 to its operand and Decrement operator (--) subtracts 1 from its operand.

                    int a=5, b=3;
                    ++a;-- b;

  • The values of a and b will be 6 and 2 respectively after the execution of these statements. i.e., ++a; is equivalent to a=a+1; and --b; is same as b=b-1;
  • There are two forms for these operators: postfix (a++ and b --) & prefix (++a and --b).
  • c = a++, c = ++a; and c = b-- , c = --b are different.
  • If a= 5 and b=3, In the first case c = 5 and c = 6, but in the latter case c=3 and c=2.
  • The statement c=a++; is equivalent to the statement sequence c=a; a=a+1; (use and change method)
  • But the statement b=++a; is equivalent to the statement sequence a=a+1; c=a; (change and use method)