C Programming IT Programming Tutorials

Operators in C

It is a symbol that tells the compiler to perform specific mathematical and logical manipulations. The Operators in C are defined as the part of the mathematical expression which is of various types each discussed in detail in this article.

Arithmetic Operators in C

OperatorsDescriptionExamples
A=10,B=2
+AdditionA+B=>10+2=12
SubtractionA-B=>10-2=8
*MultiplicationA*B=>10*2=20
/DivisionA/B=>10/2=5
%The modulo operator gives the remainder of the divisionA%B=>10%2=0
++Unary incrementA++=>10++=11
Unary decrementA–=9

Example 1:

Explanation:

2*((i/5)+(4*(j-3))%(i+j-2))
step1: 2*(1 + 4 * 2 % 11)
step2: 2*(1 + 8 % 11)
step3: 2*(1+8)
step4: 2*9
step5: 18
The expression is evaluated based on the operator’s precedence.
(), *, /, % is the order of the operators.

In the second printf statement, k++ is the post-increment, k– is the post decrement, it first prints the value then it is incremented or decremented. Whereas in the case of pre-increment or pre-decrement, it first increments or decrements then prints the value.

Relational Operators in C

OperatorDescriptionExample
==if the two operand values are equal, if yes then the condition is True else FalseA=10, B=10, A==B condition is True
!=In case if the two operands are not equal, if yes then the condition becomes TrueA=10, B=10, A!=B condition is False
>Whereas if the left operand is greater than the right, if yes then the condition becomes TrueA=12, B=10 then A>B is True
<If the left operand is lesser than the right, then the condition is TrueA=12,B=10 then A<B condition is False
>=Basically, if the left operand is either greater than or equal to the right operand then the condition is TrueA=12 or 10 and B=10 then both values of A satisfies A>=B, so the condition becomes True
<=In case if the operand is either lesser than or equal to the right operand then the condition is True else FalseA=12 and B=10 then A<=B is not satisfied which gives False

Logical Operators in C

OperatorsDescriptionExamples
&&Logical AND, if both x and y is Non zero, then it returns TRUEX&&Y=False
||Logical OR, if either x or y is Non zero, then it returns TRUEX||Y=True
!Logical NOT ,if x is 1 then it returns 0,which is FALSE and if x is 0 then it returns 1 means TRUE!X=False

Example 2:

Explanation:

From the operator’s precedence table we can understand the priority of operators in the above expression is
(), !, <, >, >=, ==, &&, ||.
The relational operators just check whether it is zero or non-zero value, if it is a non-zero value then it is considered as 1(True) and zero as 0(False).

k=((x>y)&&(i>=0)) || ((j<5)&&(!c==d))
step1: ((0.005>-0.01) && (8>=0)) || ((5<5) && (!’c’==’d’))                                                              step2: (1 && 1) || (0 && (0==1)) 

step3: 1 || (0&&0)
step4: 1 || 0
step5: 1
Note(!’c’=! 99(ASCII value of  ‘c’=99=>non-zero ‘c’=99=>non-zero, ‘d’=100=>non-zero=>1   

Bitwise Operators in C

Operator precedence

Category

Description

Example

Postfix

() [] -> . ++ – –

Left to right

Unary

+ – ! ~ ++ – – (type)* & sizeof

Right to Left

Multiplicative

* / %

Left to right

Additive

+ –

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %=>>= <<= &= ^= |=

Right to left

Comma

,

Left to right

Example 3:

Explanation:

The bitwise operations are done after converting decimal to equivalent binary values
k=((i | j) >>2) ^ ((~i&j) <<3)
k=((36 | 50) >>2) ^ ((~36&50) <<3)
=((0010 0100 | 0011 0010) >>2) ^ ((~0010 0100 & 0011 0010) <<3)
=(0011 0110 >>2) ^ ((1101 1011 & 0011 0010) <<3)
=(0000 1101) ^ (0001 0010 <<3)
=(0000 1101) ^ (1001 0000)
=1001 1101=>157
=157

Assignment Operators in C:

OperatorsDescriptionExample
=Assignment operator that assigns the value of the right operand to the leftIf A=10, B=5 then A=B =>A=5, B=5
+=Addition and Assignment operator, it adds both values and assigns to the left operandA+=B => A=A+B gives A=15
-=Subtraction and Assignment operator, it subtracts the right operand value from the left and assigned it to the left operandA-=B =>A=A-B gives A=5
*=Multiplication and Assignment operator, it multiply both operand values and the result is assigned to the left operand valueA*=B =>A=A*B

A=50
/=Division and Assignment operator, it divides the left operand value with right and assigns it to the left operandA/=B =>A=A/B

A=2
%=Modulo and Assignment operator, it divides left operand value with right and assigns a reminder to the left operandA%=B =>A=A%B

A=0
<<=Left shift and Assignment operatorA<<=B =>A=A<<B, A=10, B=2

0000 1010 <<2 =>0010 1000
>>=Right shift and Assignment operator.A>>=B =>A=A>>B

0000 1010 >>2 =>0000 0010
&=Bitwise AND & Assignment operatorA&=B =>A=A&B

0000 1010 & 0000 0010 =

0000 0010=>2
^=Bitwise exclusive OR and Assignment operatorA^=B =>A=A^B

0000 1010 ^ 0000 0010=

0000 1000=>8
|=Bitwise inclusive OR and Assignment operatorA|=B =>A=A|B

0000 1010| 0000 0010=

0000 1010 =>10

Example 4:

Explanation:

From the operator precedence, we can understand the order of the operators used in the above example is(), +=, *=
k+=(20/20)
k+=1
k=k+1=>0+1
k=1
i*=(20%1)
i=i*0=>0

Miscellaneous or Special Operators:

List of Miscellaneous Operators

Operators

Description

Example

sizeof()

This method returns the size of the variable

int A=10, sizeof(A) returns 2 or 4 based on a 32bit or 64bit processor.

&

It represents the address of the variable

A=10, B=&A, the address of A which holds value 10 is stored in B.

*

It represents the pointer variable

*B=> value inside the address =>10

?:

Ternary operator or Conditional Operator

A>B?A:`B if the condition is satisfied returns A else B

Example 5:

Explanation:

Ternary operator (Condition)? True: False
In the above example, x=10 is > y=3.5 and hence first printf statement is executed.
If the value of x=2 and y=3.5 then condition x>=y is not satisfied, in such case the second statement will be executed.

The sizeof(variable_name) is used to find the size of the variable. In the above example the variable x is integer type, sizeof(x)=>4bytes and y is float type, sizeof(y)=>4 bytes

‘&’-returns the address of the variable and the ‘*’ – pointer to the variable. We will see about these operators in the future while discussing about pointer.

Operator Precedence

An expression may have many operators in combination, the order in which the operator needs to be evaluated is done by using the operator precedence. 

Certain operators have high priority over other operators. The operator with high priority is evaluated first followed by others.

Operator precedence

Category

Description

Example

Postfix

() [] -> . ++ – –

Left to right

Unary

+ – ! ~ ++ – – (type)* & sizeof

Right to Left

Multiplicative

* / %

Left to right

Additive

+ –

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %=>>= <<= &= ^= |=

Right to left

Comma

,

Left to right

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *