operators in c language

Operator in c

An operator is a backbone of any operation. It gives the direction to the compiler to perform the specific task. C language has a lot of operators to perform the task like addition, subtraction, division etc.

Let’s take an example

Suppose I have written a statement a = a-b;

Then what would happen?

The compiler understands the ‘-’ instruction and subtract the value of ‘b’ from ‘a’ and store the result into ‘a’.

In “C” language operator divides into the following categories

  • Arithmetical operator

An arithmetical operator is used to perform a mathematical operation like addition multiplication etc.

There are following mathematical operators.

OperatorMeaning
+ (plus)Use to Add two number.
– (minus)Use to subtract a number.
*( asterisk)Use to multiply two number
/(division)Use to find the quotient of a number.
%(modulo division)Use to find modulo division of a number.

Note: Generally we are familiar with all Arithmetical operators but sometimes we get confused with division and modulo division operator. So before going into depth, I want to clear this confusion.

There is two example, where I am performing both division and modulo division operation.

  • Division Operation      7/3   =   2
  • Modulo Division Operation    7%3  = 1

Are you confused with result?

Actually, the division operator gives the quotient but modulo division operator give the Remainder. It is the basic difference between both operators.

Simple program to check the difference between division and modulo division.

arithmatic operation

Relational Operator

Relational operator is used to comparing the mathematical, character and logical expression.
the relational operator compares the right-hand expression with left-hand expression, if these expressions are true then return 1(True Value) and if it’s false then return 0 (False Value).

Note: Relation operator generally used with the conditional expression like if, while and do while etc.

There are following relational operators

Operator          Meaning
< (Less than)Return true when left operand less than right.
<= (less than or equal to)Return true when left operand less or equal than right.
> (Greater than)Return true when left operand greater than right.
>= (greater than or equal to )Return true when left operand greater or equal than right.
==( Equal to)Return true when left operand equal to right.
!=  (not equal to)Return true when the left operand not equal to right.

Simple program to describe relational operator

Suppose there is a scenario where you want to display “Welcome” and “Bye” as corresponding to the input value of ‘a’. If the value of a less than 100 then displays “Welcome” otherwise display “Bye”.

OutPut 1:

relational

OutPut 2:

relational operator

Logical operator

If any event depends on more than one condition then we have to use logical operator.

Suppose you are washing the clothe in a digital washing machine and you want your washing machine stop only in two situations when the clothe is completely clean or after 5 minutes of the start.

To manage this scenario programmer have used the logical operator, which would be like this.

There are three type logical operators in c.

Operator          Meaning
|| (Logical OR)Work like digital OR Gate.
&&(Logical AND)Work like digital AND Gate.
! ( Logical NOT)Work like digital NOT Gate.

Logical OR

It works like digital OR gate if any condition true then it returns a true value.

E.g.

In the above example, if the value of “a” less than 10 or value of “b” greater than 20, then print “hi aticleworld” otherwise in any scenario print “bye aticleworld”.

Note: Return true if any one of them expression true in logical OR operator.

Logical AND

It works like digital AND gate if both condition true then it returns the true value.
E.g.

In above example, if the value of “a” less then 10 and value of “b” greater then 20, then print hi aticleworld otherwise in any scenario print bye aticleworld.

Note: Return true only if both expressions will true in logical AND operator.

Logical NOT

It works like digital NOT gate if condition false then it returns the true value.
It works on a single expression, if the expression is true then it evaluates false and if the expression is false then it evaluates true.

E.g.

If expression false then print “hi aticleworld “either print “Bye aticleworld “.

Bitwise operator

These operators mainly use in low-level programming. Bitwise operator operates on each bits. It’s mainly useful for integer, not useful for float and double.
If we need to check a single bit or we want to make reset or set a single bit then we have to use bit-wise operator.

There are following bit-wise operators

Operator          Meaning
| (Bitwise OR)Use to Set a Bit of a Register.
& (Bitwise AND)Use to check a Bit of Register.
^ (Bitwise EX-OR)Use to toggle a Bit of a Register.
~( Bitwise complement)Use for the complement.
<< (Shift left)Use to shift a sequence of Bit toward left.
>> (Shift right)Use to shift a sequence of Bit toward Right

Ternary operator

It’s a conditional operator, consist two symbol question mark (?) and colon (:).
It’s similar to if else but somehow execution time of ternary operator is less as compared to if else.

Syntax:

(expression)  ?  statement1  :   statement2;

To understand ternary operator we take an example.

In the above example, if the value of “a” less than 10 then print “Hi” otherwise print “Bye“.

Assignment operator

Assignment operator assigns the value from right to left. Left values should be a valid storage or memory.
E.g.

 

 

One comment

Leave a Reply