C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

3.3. Assignment, Increment and Decrement Operators 


Assignment Operator

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

  1. Simple Assignment (=):

Assigns the value on the right side to the variable on the left side.

int x;
x = 10;  // Assigns the value 10 to the variable x
  1. Addition Assignment (+=):

Adds the value on the right side to the current value of the variable.

int a = 5;
a += 3;  // Equivalent to: a = a + 3;
  1. Subtraction Assignment (-=):

Subtracts the value on the right side from the current value of the variable.

int b = 8;
b -= 2;  // Equivalent to: b = b - 2;
  1. Multiplication Assignment (*=):

Multiplies the current value of the variable by the value on the right side.

int c = 4;
c *= 5;  // Equivalent to: c = c * 5;
  1. Division Assignment (/=):

Divides the current value of the variable by the value on the right side.

int d = 20;
d /= 4;  // Equivalent to: d = d / 4;
  1. Modulus Assignment (%=):

Computes the remainder when dividing the current value of the variable by the value on the right side.

int e = 15;
e %= 7;  // Equivalent to: e = e % 7;

Increment And Decrement Operator


Increment and decrement operators

C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand. Both the increment and decrement operators can either precede (prefix) or follow (postfix) the operand. 

  • Post-increment (x++) and post-decrement (x--) use the current value of the variable in the expression before updating it.
  • Pre-increment (++x) and pre-decrement (--x) update the variable first and then use its updated value in the expression.

Here is example code explaining the concept of pre and post increment operator.

Note: pre and post decrement works the same way, it just decreases the value by 1.

#include <stdio.h>

int main() {
    // Declare and initialize variables
    int a = 5, b, c;

    // Post-increment: a++ means assign the current value of 'a' to b, then increment 'a'
    b = a++;
    printf("\nPost-increment:\n");
    printf("a = %d (after increment), b = %d (using original value of 'a')\n", a, b);

    // Pre-increment: ++b means increment 'b' first, then assign the updated value of 'b' to c
    c = ++b;
    printf("\nPre-increment:\n");
    printf("b = %d (after increment), c = %d (using updated value of 'b')\n", b, c);

    return 0;
}

output:

Post-increment:
a = 6 (after increment), b = 5 (using original value of 'a')

Pre-increment:
b = 6 (after increment), c = 6 (using updated value of 'b')