BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Assignment Operators in C with Examples

By Chaitanya Singh | Filed Under: c-programming

Assignment operators are used to assign value to a variable. The left side of an assignment operator is a variable and on the right side, there is a value, variable, or an expression. It computes the outcome of the right side and assign the output to the variable present on the left side. C supports following Assignment operators:
Assignment Operators in C List

1. Simple Assignment = Operator Example

This is one of the simplest assignment operator, it simply assigns the right side value to the left side operand.

#include <stdio.h>
int main ()
{
  int n; //integer variable
  char ch; //character variable
  float f; //float variable

  // Simple assignment operator to assign values to variables
  n = 1;
  ch = 'A';
  f = 15.565f;

  // Displaying the values of all the variables
  printf("The value assigned to 'n': %d", n);
  printf("\nThe value assigned to 'ch': %c ", ch);
  printf("\nThe value assigned to 'f': %f ", f);

  return 0;
}

Output:
Output = operator

2. += Operator Example

The += assignment operator is a combination of + arithmetic operator and = simple assignment operator. For example, x += y; is equivalent to x = x+y;.

It adds the right side value to the value of left side operand and assign the result back to the left-hand side operand.

#include <stdio.h>
int main ()
{
  int x = 100, y = 20, z = 50;

  // += Operator demonstration
  x += 10;
  y += 10;
  z += 10;

  //Display values after += operations
  printf("Value of variable x: %d", x);
  printf("\nValue of variable y: %d", y);
  printf("\nValue of variable z: %d", z);

  return 0;
}

Output:

Value of variable x: 110
Value of variable y: 30
Value of variable z: 60

3. -= Operator Example

The -= assignment operator is a combination of – and = operators. It subtracts the right side value from the left side value and assign the result to left side operand. For example, x -= y; is equivalent to x = x-y;.

#include <stdio.h>
int main ()
{
  int x = 100, y = 20, z = 50;

  // -= Operator demonstration
  x -= 50;
  y -= 10;
  z -= 5;

  //Print variable's values after -= operations
  printf("Value of variable x: %d", x);
  printf("\nValue of variable y: %d", y);
  printf("\nValue of variable z: %d", z);

  return 0;
}

Output:

Value of variable x: 50
Value of variable y: 10
Value of variable z: 45

4. *= Operator Example

The *= assignment operator is a combination of * and = operators. It multiplies the right side value to the left side value and assign the product of these numbers to the left side variable. For example, x *= y; is equivalent to x = x*y;.

#include <stdio.h>
int main ()
{
  int x = 100, y = 20, z = 50;

  // *= Operator to multiply the value of left operand
  // by the value of right operand
  x *= 2;
  y *= 10;
  z *= 1;

  //Displaying values after *= assignment operations
  printf("Value of variable x: %d", x);
  printf("\nValue of variable y: %d", y);
  printf("\nValue of variable z: %d", z);

  return 0;
}

Output:

Value of variable x: 200
Value of variable y: 200
Value of variable z: 50

5. /= Operator Example

The /= assignment operator is a combination of / and = operators. It divides the left side operand by the right side operand and assign the quotient value of this division to the left side variable. For example, x /= y; is equivalent to x = x / y;.

#include <stdio.h>
int main ()
{
  int num1 = 100, num2 = 50, div;

  // The / operator returns the quotient value
  div = num1 / num2;

  printf("Value of num1: %d", num1);
  printf("\nValue of num2: %d", num2);
  printf("\nValue of num1 / num2 is: %d", div);

  return 0;
}

Output:
/= Assignment Operator Example

6. %= Operator Example

The %= assignment operator is a combination of % and = operators. It divides the left side operand by the right side operand and assign the remainder value to the left side variable. For example, x %= y; is equivalent to x = x % y;.

#include <stdio.h>
int main ()
{
  int num1 = 100, num2 = 50, mod;

  // The Modulus % operator returns the remainder after division
  mod = num1 % num2;

  printf("Value of num1: %d", num1);
  printf("\nValue of num2: %d", num2);
  printf("\nRemainder after dividing num1 by num2 is: %d", mod);

  return 0;
}

Output:
Output of %= Operator Example

Recommended Posts

  • Unary Operators in C with Examples
  • Arithmetic Operators in C with Examples
❮ C Tutorial

Top Related Articles:

  1. Operators in C++
  2. Operators in Java With Examples
  3. Relational Operators in Java with Examples
  4. Assignment Operators in Java with Examples
  5. Unary Operator in C with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2023 BeginnersBook . Privacy Policy . Sitemap