Subscribe

Header Ads

Operators in C

    



Operators are the symbols that tell the compiler to operate the given values or variable.

The operators may be classified as:

Unary Operator:

The operator that works on a single operand is known as a unary operator.
For example: ++ (increment operator), --(decrement operator), etc.

Binary Operator:

The Operator that works on 2 operands is known as a binary operator.
For Example: + (addition operator), - (Subtraction operator), etc.

Various operators used in C are given below:

1. Arithmetic Operators: 

The operator that tells the compiler to perform a mathematical operation on the given operand(s) is known as an arithmetical operator.

Arithmetical Operator of C are given below:

+

It is an addition operator. It is used to add the 2 operands given with this operator.

For example: 2 + 4 gives 6


-

It is known as the subtraction operator which is used to subtract the operand given after the subtraction operator from the operand given before the operator.

For example, 10 – 5 gives 5


*

It is known as the multiplication operator that is used to give the product of the operands given with the operator.

For example, 2 * 3 gives 6


/

It is known as the division operator that is used to divide the operand given before the operator by the operand given after the operator.

For example, 20 / 2 gives 10


%

It is known as the modulus operator that is used to find out the remainder when the first operator is divided by the second operator.

For example, 55 % 10 gives 5

     10 ) 65 (6

             60

             ----

              5

             ----


++

It is a unary operator known as increment operator. It is used to increment the value of the given operand by 1.

For example, 5++ will be 6


--

It is a unary operator known as the decrement operator.

It is used to decrement the value of given operand by 1.

For example 10– will give 9


Increment Operators are of two types :

Pre-Increment Operator:

When the increment operator is used before the operand then it is called a pre-increment operator. In this, the value is first incremented and then assigned.
For example: x=5;
                      a = ++x;
            The value of both a and x will be 6.


Post-Increment operator:

When increment operator is used after the operant then it is known as post-increment operator. In this, the value is first assigned and then incremented. Go through the example, it will make you more clear.
For example: x=5;
                      a= x++;
            The value of x will be 6 but the value of a will be 5 because it is first assigned to a and then incremented.

Decrement operators are also of two types:

Pre-Decrement operator:

When the decrement operator is used before the operand then it is called a pre-decrement operator. In this, the value is first decremented and then assigned.
For example: x=5;
                      a = --x;
            The value of both a and x will be 4.

Post-Decrement Operator:

When the decrement operator is used after the operant then it is known as the post-decrement operator. In this, the value is first assigned and then decremented. Go through the example, it will make you more clear.
For example: x=5;
                      a= x--;
            The value of x will be 4 but the value of a will be 5 because it is first assigned to a and then decremented.

2. Relational Operators:

It is the operator used to check or define the relationship between the 2 given entities.
The 6 different relational operators are given below:


==

It is the operator used to check whether the given two entities are equal or not.

For example, 10 == 20 evaluated to 0

                    and, 50 ==50 evaluated to 1


!=

It is the operator used to check if the given 2 entities are not equal.

For example, 20 != 20 evaluated to 0

                but, 20 != 30 evaluated to 1


<

It is the operator used to check if the first entity is less than the second entity

 or not.

For example, 20 < 30 evaluated to 1

               but, 30 < 20 evaluated to 0


>

It is the operator used to check whether the first entity is greater than the 

second entity.

For example, 20 > 30 evaluated to 0

              and   30 >20 evaluated to 1


<=

It is the operator used to check whether the first entity is either less than or 

equal to the second entity or not.

For example, 30 <= 20 evaluated to 0


>=

It is the operator used to check whether the first entity is either greater than 

or equal to the second operator.


3. Logical Operators:

Logical operators are the operators used to join the given expressions.
commonly, there are 3 logical operators in C:

&&   It is known as AND operator. By using this operator if either both or any one of the expressions is false then the result will be false. It will be true only if both expressions are true.

||        It is known as the OR operator. By using this operator if any one of the expressions is true the result will be true.

!        It is known as the not operator. It gives the true value when the operand is false and gives the false value when the operand is true.

4. Bitwise Operators:

& -> BinaryAND operator.
|   -> Binary OR operator.
^   -> Binary XOR operator.
~  -> Binary ones complement.
<< -> Binary left shift operator.
>> -> Binary right shift operator.

5. Assignment operators:

=        Simple assignment operator.
+=      Add and assignment operator
-=       Subtract and assignment operator   
*=       Multiply and assignment operator
/=        Divide and assignment operator
%=      Modulus and assignment operator
<<=     Left shift and assignment operator
>>=     Right shift and assignment operator
&=       Bitwise AND assignment operator
^=        Bitwise exclusive OR assignment operator
|=        Bitwise inclusive OR assignment operator

6. Miscellaneous Operators:

sizeof  Use to find out the size of the variable.

?: (condition ? x : y)   It is the conditional operator. If the condition is true then x will execute otherwise y will execute.

& This operator is used to return the address of the variable.

* It is pointer to thevariable.

Post a Comment

0 Comments