Operators

Operators are just symbols that inform the compiler to perform mathematical or logical functions in C language. There are different types of operators in C.

  • Arithmetic Operators

  • Logical Operators

  • Relational Operators

  • Bitwise Operators

  • Assignment Operators

  • Misc Operators

 

Arithmetic Operators

The table below displays all the Arithmetic operators used in the C language.

 

Operator

Description

Example

+

For adding two operands.

A + B = 30

For subtracting the second operand from the first.

A − B = -10

*

For multiplying both operands.

A * B = 200

/

For Dividing numerator by de-numerator.

B / A = 2

%

Modulus Operator and the remainder of after an integer division.

B % A = 0

++

The incrementing operator increases the integer value by one.

A++ = 11

--

Decrementing operator decreases the integer value by one.

A-- = 9


Logical Operators

If the value of A is 5 and the value of B is 0. Then 

Operator

Description

Example

&&

It is an AND operator. If both of the operands are non-zero, then the condition is true.

(A && B) is false.

||

Known as Logical OR Operator. If any two operands are non-zero, then the condition states true.

(A || B) is true.

!

Called as Logical NOT Operator and is used to reverse the state of its operand. If a condition is true, then the Logical NOT operator will change it to false.

!(A && B) is true.

 

Relational Operators

The following table below shows all the relational operators supported in the C language. For example, if the value of A is 25 and B is 30.

Operator

Description

Example

==

This operator checks whether the values of the two operands are equal or not. If yes, then the condition becomes true.

(A == B) is not true.

!=

This operator checks whether the values of the two operands are equal or not. If the values are not equal, then the condition becomes true.

(A != B) is true.

>

It checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition states true.

(A > B) is not true.

<

It checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true.

(A < B) is true.

>=

This operator checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.

(A >= B) is not true.

<=

It checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.

(A<=B) is true

 

Bitwise Operator

Bitwise operators usually work on bits and always perform bit by bit operations. To know about the bitwise operator we have analyzed the following table. Let's assume the value of A is 30 and B is 10. 

Operator

Description

Example

&

It is known as a binary AND Operator that copies a bit to the result if it exists in both operands.

(A & B) = 12, i.e., 0000 1100

^

Known as binary XOR Operator that copies the bit if it is set in one operand but not both.

(A ^ B) = 49, i.e., 0011 0001

|

Called as binary OR Operator that copies a bit if it exists in either operand.

(A | B) = 61, i.e., 0011 1101

>>

It is known as the binary Right Shift Operator which moves to the right by the number of bits specified by the right operand.

A >> 2 = 15 i.e., 0000 1111

<<

Binary Left Shift Operator which moves the value to the left by the number of bits specified by the right operand.

A << 2 = 240 i.e., 1111 0000

~

Binary One's Complement Operator is unary and has the effect of 'flipping' bits.

(~A ) = ~(60), i.e,. -0111101

 

Assignment Operators

Assignment operators are like =, +=, -= that helps in several mathematical functions. For example "C+=A" means "C= C+A". Here is the Assignment operator supported in the C language.

Operator

Description

Example

=

Known as simple assignment operator that assigns values from right side operands to left side operand

C = A + B will assign the value of A + B to C

+=

It is called the add AND assignment operator that adds the right operand to the left operand and also assigns the result to the left operand.

C += A is equivalent to C = C + A

-=

This operator is known as the subtracting AND assignment operator which subtracts the right operand from the left operand and assigns the result to the left operand.

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator which multiplies the right operand with the left operand and assigns the result to the left operand.

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator which divides the left operand with the right operand and assigns the result to the left operand.

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator also takes modulus using two operands and assigns the result to the left operand.

C %= A is equivalent to C = C % A

<<=

Known as left shift AND assignment operator.

C <<= 2 is same as C = C << 2

>>=

Also, known as right shift AND assignment operator.

C >>= 2 is same as C = C >> 2

&=

Bitwise AND assignment operator.

C &= 2 is same as C = C & 2

^=

Bitwise exclusive OR and assignment operator.

C ^= 2 is same as C = C ^ 2

|=

Bitwise inclusive OR and assignment operator.

C |= 2 is same as C = C | 2

 

Misc Operator

Operator

Description

For example

sizeof()

It returns the size of a variable.

sizeof(a), where a is an integer, will return 4.

&

It returns the address of a variable.

&a; returns the actual address of the variable.

*

Pointer to a variable.

*a;

? :

Known as Conditional Expression.

If Condition is true ? then value X : Otherwise value Y