Site icon Learn Web Development & Programming, Artificial Intelligence, WordPress, Shopify Articles

Operators in C

operators-in-c-programming

An operator  tells the compiler to perform specific mathematical or logical functions. It is a symbol which is used to perform different task. C language is rich in built-in operators and provides the following types of operators −

Assignment Operators :

The assignment operator is the single equals sign ( = ). The assignment also acts as an expression which returns the newly assigned value. Some programmers will use that feature to write things like the following.

Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand C = A + B will assign the value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It 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. It 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. It 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. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= 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

Mathematical (Arithmetic) Operators:

C includes the usual binary and unary arithmetic operators. The operators are sensitive to the type of the operands. So division (/) with two integer arguments will do integer division.  If either argument is a float, it does floating point division. Assume variable A holds 10 and variable B holds 30 then −

Operator Description Example
+ Adds two operands. A + B = 40
Subtracts second operand from the first. A − B = -20
* Multiplies both operands. A * B = 300
/ Divides numerator by de-numerator. B / A = 3
% Modulus Operator and remainder of after an integer division. B % A = 0

Unary Increment Operators:

The unary ++ and — operators increment and decrement the value in a variable. There are “pre” and “post” variants for both operators which do slightly different things (Explained below)

var ++       increment  “post” variant

++var        increment  “pre” variant

Relational Operators:

These operate on integer or floating point values and return a 0 or 1 boolean value. Suppose variable A holds 10 and variable B holds 30 then −

Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <=

logical Operators:

The value 0 is false, anything else is true. The operators evaluate left to right and stop as soon as the truth or falsity of the expression can be deduced. Suppose variable A holds 1 and variable B holds 0, then −

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true.

Bitwise Operators:

C includes operators to manipulate memory at the bit level. This is useful for writing low- level hardware or operating system code where the ordinary abstractions of numbers, characters, pointers, etc. The bitwise operations are typically used with unsigned types. In particular, the shift operations are guaranteed to shift 0 bit into the newly vacated positions when used on unsigned values.

~  Bitwise Negation (unary) – flip 0 to 1 and 1 to 0 throughout

&  Bitwise And

|  Bitwise Or

^  Bitwise Exclusive Or

>>  Right shift by right hand side (RHS) (divide by power of 2)

<<  Left Shift by RHS (multiply by power of 2)

Exit mobile version