Assignment Operators in C

Updated

Prerequisite – Operators in C
In C, assignment operators are used to assign a value to a variable. They combine the assignment operation (=) with an arithmetic, bitwise, or other operation. Here are the assignment operators in C:

List of Assignment Operators in C:

Operator Example Equivalent to
= a = b a = b
+= a += b a = a + b
-= a -= b a = a – b
*= a *= b a = a * b
/= a /= b a = a / b
%= a %= b a = a % b
<<= a <<= b a = a << b
>>= a >>= b a = a >> b
&= a &= b a = a & b
^= a ^= b a = a ^ b
|= a |= b a = a | b

Example:
Here’s an example code that demonstrates the use of assignment operators in C:

#include <stdio.h>

int main() {
    int a = 10;
    int b = 20;

    printf("a = %d, b = %d\n", a, b);

    a += b;
    printf("a += b: %d\n", a);

    a -= b;
    printf("a -= b: %d\n", a);

    a *= b;
    printf("a *= b: %d\n", a);

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

    a %= b;
    printf("a %%= b: %d\n", a);

    a <<= b;
    printf("a <<= b: %d\n", a);

    a >>= b;
    printf("a >>= b: %d\n", a);

    a &= b;
    printf("a &= b: %d\n", a);

    a ^= b;
    printf("a ^= b: %d\n", a);

    a |= b;
    printf("a |= b: %d\n", a);

    return 0;
}

When you run this code, you will see the following output:

a = 10, b = 20
a += b: 30
a -= b: 10
a *= b: 200
a /= b: 10
a %= b: 10
a <<= b: 10240
a >>= b: 20
a &= b: 20
a ^= b: 0
a |= b: 20

This output shows the results of using each of the assignment operators with the variables a and b. The value of a changes after each operation, and the result is printed to the console.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
Mithlesh Upadhyay Published 18 Feb 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read