types of incrementation code example

Example: With a suitable example, explain increment, decrement and compound assignment operators

// composite assignment operators
float x, y;
int a, b;

// +=, -=
a = 8;
b = 5;
a += b; // a = a + b = 13
b -= 4; // b = b - 4 = 1

// *=, /=
x = 4;
y = 5;
x *= y; // x = x * y = 4 * 5 = 20
y /= 2.5; // y = y / 2.5 = 2.0

// &=, |=
a = 8;
b = 3;
a &= b + 5; // a = a & b + 5 = a & (b+5) = 8
a |= b; // a = a | b = 11

// >>=, <<=
a = 34;
a >>= 1; // a = a >> 1 = 17
a = 6;
a <<= 3; // a = a << 3 = 48

// %=
b = 15;
b %= 6; // b = b % 6 = 3

Tags:

C Example