Increment operator
C has two special unary operators called increment(++) and decrement (--) operators. These operators increment and decrement value of a variable by 1. It is used to increment the value of variable by 1. These are two types:
- Pre increment
- Post increment
Pre increment
Increment operator is palced before the operand in per increment and the vlaue is first incremented and then operation is performed on it.
Example: a = 10;b = ++a;
Output 'a' value is 11.
'b' value is 11.
Here ++a means it increments the value at first attempt only not postponed to another attempt, that's why 'a' value is incremented by 1 (10 + 1 = 11). It is called pre increment.
Pre increment not only increments 1 but also increments any number. a+2. It increments 2 values at a time.
Post increment
Increment operator is palced after the operand in post increment and the value is incremented after the operation is performed.
Examplea = 10;
b = a++;
Output
'a' value is 11.
'b' value is 10
Decrement Operator
It is used to decrement the value of a variable by 1. These are two types:
- Pre Decrement
- Posr Decrement
Pre Decrement
Pre Decrement operator is palced before the operand in pre Decrement and the value is first decremented and then opewration is performed on it.
Examplea = 10;
b = --a;
Output
'a' value is 9
'b' value is 9
Post Decrement
Post Decrement operator is placed after the operand in post decrement and the value is decremented after the operation is performed.
Examplea = 10;
b = a--;
Output
'a' value 9
'b' value 10
Assignment Operator
Using assignment operators, we can assign value to the variables. Equality sign (=) is used as an assignment operator in C.
Examplex = a + b;
Basically, the value of right side operand will be assigned to the left side operand.
leftoperand = rightoperandIn c language assignment operators are classified into two types:
- Simple assignment
- Compound assignment / Short hand assignment
simple assignment is nothing but assignning a value to variable
Compound assignment / Short hand assignmentOperator | Meaning | Example (a = 10, b = 5) |
+= | L = L + R Add left and right operand and assign the result in left |
a += b; same as a = a+b; after execution 'a' will hold 15 |
-= | L = L - R Subtract right operand from left operand and assignning result in left. |
a -= b; same as a = a-b; after exeution 'a' will hold 5. |
*= | L = L * R Multiply both right and left operand and store result in left. |
a *= b; same as a = a*b; after execution 'a' will hold 50 |
/= | L = L / R Divides left operand by right operand and store result in left. |
a /= b; same as a = a/b after execution 'a' will hold 2 |
%= | L = L % R After left and right operand division, the remainder will be stored in left. |
a %= b; same as a = a%b after execution 'a' will hold 0 |
Here '/' operator stores the quotient value and modulus(%) operator stores the remainder value.