C Programming Assignment Operators

C Programming Assignment Operators

02 Jul 2024
Beginner
1.51K Views
11 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

What is an Assignment Operator in C?

Assignment Operators in C are used to assign values to the variables. They come under the category of binary operators as they require two operands to operate upon. The left side operand is called a variable and the right side operand is the value. The value on the right side of the "=" is assigned to the variable on the left side of "=". The value on the right side must be of the same data type as the variable on the left side. Hence, the associativity is from right to left.

In this C tutorial, we'll understand the types of C programming assignment operators with examples. To delve deeper you can enroll in our C Programming Course.

Before going in-depth about assignment operators you must know about operators in C. If you haven't visited the Operators in C tutorial, refer to Operators in C: Types of Operators.

Types of Assignment Operators in C

There are two types of assignment operators in C:

Types of Assignment Operators in C

Read More: Top 50 C Interview Questions and Answers

1. Simple Assignment Operator (=)

This assigns the value on the right-hand side (RHS) to the variable on the left-hand side (LHS). You can use a literal, another variable, or an expression in the assignment statement.

Example of Simple Assignment Operator


#include <stdio.h>
int main()
{
 int x;
 x = 30; 
 printf("The value of x is: %d\n", x);
 return 0;
}   

In this example, we have assigned the value 30 to the variable x using the assignment operator (=).

Output

30   
Read More: Expressions in C Programming - Types of Expressions in C ( With Examples )

2. Compound Assignment Operators

In addition to the = operator, you can combine arithmetic and bitwise operators with the = symbol to form an augmented or compound assignment operator.

There are five combinations of arithmetic operators with the assignment operator, "=". Let's look at them one by one.

OperatorOperator NameDescription
+=addition assignmentIt adds the right operand to the left operand and assigns the result to the left operand.
-=subtraction assignmentIt subtracts the right operand from the left operand and assigns the result to the left operand.
*=multiplication assignmentIt multiplies the right operand with the left operand and assigns the result to the left operand
/=division assignmentIt divides the left operand with the right operand and assigns the result to the left operand.
%=modulo assignmentIt takes modulus using two operands and assigns the result to the left operand.

Example of Augmented Arithmetic and Assignment Operators


#include <stdio.h>

int main()
{
    int a = 20;
    printf("Value of a is %d\n", a);

    a += 20;
    printf("Value of a is %d\n", a);

    a -= 20;
    printf("Value of a is %d\n", a);

    a *= 20;
    printf("Value of a is %d\n", a);

    a /= 20;
    printf("Value of a is %d\n", a);

    return 0;
}    

Output

Value of a is 20
Value of a is 40
Value of a is 20
Value of a is 400
Value of a is 20   

There can be five combinations of bitwise operators with the assignment operator, "=". Let's look at them one by one.

Read More: Bitwise Operators in C: AND, OR, XOR, Shift & Complement
OperatorOperator NameDescription
&=bitwise AND assignmentIt performs the bitwise AND operation on the variable with the value on the right
|=bitwise OR assignmentIt performs the bitwise OR operation on the variable with the value on the right
^=bitwise XOR assignmentIt performs the bitwise XOR operation on the variable with the value on the right
<<=bitwise left shift assignmentShifts the bits of the variable to the left by the value on the right
>>=bitwise right shift assignmentShifts the bits of the variable to the right by the value on the right

Example of Augmented Bitwise and Assignment Operators


#include <stdio.h>

int main()
{
    int x=2, y=10;
   
    y <<= x;
    printf("Value of y is %d\n", y);

    y %= x;
      printf("Value of y is %d\n", y);

    y &= x;
        printf("Value of y is %d\n", y);

    y >>= x;
    printf("Value of y is %d\n", y);

    y |= x;
    printf("Value of y is %d\n", y);
    
    y ^= x;
    printf("Value of y is %d\n", y);
    
    return 0;
}

Output

Value of y is 40
Value of y is 0
Value of y is 0
Value of y is 0
Value of y is 2
Value of y is 0

Practice Problems on Assignment Operators in C

1. What will the value of "x" be after the execution of the following code?


int x = 50;
x += 5;
x -= 3;
A) 55
B) 57
C) 52
D) 60

The correct answer is 52. x starts at 50, increases by 5 to 55, then decreases by 3 to 52.

2. After executing the following code, what is the value of the number variable?


int number = 73;
number >>= 1;
number <<= 2;
A) 55
B) 57
C) 52
D) 60

The correct answer is 144. After right-shifting 73 (binary 1001001) by one and then left-shifting the result by two, the value becomes 144 (binary 10010000).

Benefits of Using Assignment Operators

  • Simplifies Code: For example, x += 1 is shorter and clearer than x = x + 1.
  • Reduces Errors: They break complex expressions into simpler, more manageable parts thus reducing errors.
  • Improves Readability: They make the code easier to read and understand by succinctly expressing common operations.
  • Enhances Performance: They often operate in place, potentially reducing the need for additional memory or temporary variables.

Best Practices and Tips for Using the Assignment Operator

  • Use Compound Assignment Operators for Clarity and Efficiency

    While performing arithmetic operations with the same variable, use compound assignment operators

    
    x += 5;  // Clear and concise compared to x = x + 5;
    
  • Initialize Variables When Declaring
    
    int count = 0;  // Initialization
    
  • Avoid Complex Expressions in Assignments
    
    a = (b + c) * (d - e);
    // Consider breaking it down:
    int temp = b + c;
    a = temp * (d - e);
    
  • Avoid Multiple Assignments in a Single Statement
    
    // Instead of this
    a = b = c = 0;
    // Do this
    a = 0;
    b = 0;
    c = 0;
    
  • Consistent Formatting
    
    int result = 0;
    result += 10;
    
  • Use Parentheses for Clarity

    When mixing assignments with other operations, use parentheses to ensure the correct order of evaluation.

    Example

    
    x = (y + z) * w;  
Summary
Assignment operators are important fundamental operators that are useful for various operations. They are capable of performing assignment, bitwise, and shift operations. To learn C effectively, you need to master these operators.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this