Shorthand Operator in C

Shorthand Operator in C – C has a special operator called shorthand that simplifies coding a certain type of assignment statements.

Example for Shorthand Operator in C

a = a+2;  can be written as:  a += 2;

The operator += tells the compiler that a is assigned the value of a + 2;

This shorthand works for all binary operators in C.

The general form is  variable operator = variable / constant / expression

These operators are listed below:

C Shorthand Operators
Operators Example Meaning
+= x += 2 x = x+2
-= x -= 2 x = x-2
*= x *= 2 x = x*2
/= x /= 2 x = x/2
%= x %= 2 x = x%2
&&= x &&= y x = x&&y
||= x ||= y x = x||y

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: