C Programs Tutorials | IT Developer
IT Developer

C Programming - C Assignment Operators



Share with a Friend

C Programming - C Assignment Operators

C Assignment Operators

Assignment operators in C are used to assign values to variables. In addition to the basic assignment operator (=), C provides a variety of compound assignment operators that combine arithmetic or bitwise operations with assignment.

List of Assignment Operators

Operator Description Example Equivalent To Result

=

Simple Assignment

a = b

a = b

Assigns b to a

+=

Add and Assign

a += b

a = a + b

Adds b to a

-=

Subtract and Assign

a -= b

a = a - b

Subtracts b from a

*=

Multiply and Assign

a *= b

a = a * b

Multiplies a by b

/=

Divide and Assign

a /= b

a = a / b

Divides a by b

%=

Modulus and Assign

a %= b

a = a % b

Stores remainder of a / b

&=

Bitwise AND and Assign

a &= b

a = a & b

Performs bitwise AND

`

Bitwise OR and Assign

=`

`a

= b`

^=

Bitwise XOR and Assign

a ^= b

a = a ^ b

Performs bitwise XOR

<<=

Left Shift and Assign

a <<= b

a = a << b

Shifts a left by b bits

>>=

Right Shift and Assign

a >>= b

a = a >> b

Shifts a right by b bits

Examples of Assignment Operators

  1. Basic Assignment (=)

C

int a;

a = 10; // Assigns 10 to variable a

printf("a = %d\n", a); // Output: a = 10

  1. Compound Assignment Operators

Add and Assign (+=)

C

int a = 5;

a += 3; // Equivalent to a = a + 3

printf("a = %d\n", a); // Output: a = 8

Subtract and Assign (-=)

C

int a = 10;

a -= 4; // Equivalent to a = a - 4

printf("a = %d\n", a); // Output: a = 6

Multiply and Assign (*=)

C

int a = 7;

a *= 2; // Equivalent to a = a * 2

printf("a = %d\n", a); // Output: a = 14

Divide and Assign (/=)

C

int a = 20;

a /= 4; // Equivalent to a = a / 4

printf("a = %d\n", a); // Output: a = 5

Modulus and Assign (%=)

C

int a = 15;

a %= 4; // Equivalent to a = a % 4

printf("a = %d\n", a); // Output: a = 3

Bitwise AND and Assign (&=)

C

int a = 12; // Binary: 1100

a &= 10;    // Binary: 1010

             // Result: 1000 (8 in decimal)

printf("a = %d\n", a); // Output: a = 8

Bitwise OR and Assign (|=)

C

int a = 12; // Binary: 1100

a |= 10;    // Binary: 1010

             // Result: 1110 (14 in decimal)

printf("a = %d\n", a); // Output: a = 14

Bitwise XOR and Assign (^=)

C

int a = 12; // Binary: 1100

a ^= 10;    // Binary: 1010

             // Result: 0110 (6 in decimal)

printf("a = %d\n", a); // Output: a = 6

Left Shift and Assign (<<=)

C

int a = 3;  // Binary: 0011

a <<= 2;    // Shift left by 2: 1100 (12 in decimal)

printf("a = %d\n", a); // Output: a = 12

Right Shift and Assign (>>=)

C

int a = 12; // Binary: 1100

a >>= 2;    // Shift right by 2: 0011 (3 in decimal)

printf("a = %d\n", a); // Output: a = 3

Precedence of Assignment Operators

In the operator precedence hierarchy, assignment operators have lower precedence than arithmetic, relational, and logical operators but higher precedence than the comma operator.

Example:

C

int a = 5, b = 10;

int result = a + b * 2; // Multiplication (*) is evaluated first

printf("result = %d\n", result); // Output: result = 25

Applications of Assignment Operators

  1. Efficient Code:
    • Compound operators reduce redundancy and make code concise.

C

x = x + 10; // Can be written as x += 10;

  1. Bitwise Operations:
    • Useful for low-level programming like hardware interfacing.

C

int flags = 0xF0;

flags &= 0x0F; // Clear upper 4 bits

  1. Shifting Bits:
    • Optimizing multiplication or division by powers of 2.

C

int x = 5;

x <<= 3; // Multiply x by 8 (2^3)

  1. Iterative Computations:
    • Using assignment operators in loops.

C

for (int i = 1; i <= n; i++) {

    sum += i; // Add i to sum

}

Tips for Using Assignment Operators

  1. Always initialize variables before using assignment operators to avoid undefined behavior.
  2. Understand operator precedence to prevent unexpected results in complex expressions.
  3. Use compound operators for better readability and efficiency in code.

Assignment operators are foundational in C programming, enabling clear and concise handling of variables during computation.