Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
C Programming Assignment Operators

C Programming Assignment Operators

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

C Programming Free Course with Certificate: 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)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this