C

Examples Of Assignment Operators In C

Assignment operators are used to give a variable a value. A variable is on the left side of an assignment operator, and a value, variable, or expression is on the right side. It computes the right side’s result and assigns the output to the variable on the left side.

1. Example of a Simple Assignment = Operator

This is the most basic assignment operator; it simply assigns the right side operand’s 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;
}

2. += Operator Illustration

The += assignment operator combines the + arithmetic operator with the = simple assignment operator. For example, x += y; is the same as x = x+y;.
It adds the value of the right side operand to the value of the left side operand and returns the result 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 Illustration

The -= assignment operator is made up of the – and = operators. It takes the right side value and subtracts it from the left side value, then assigns the result to the left side operand. For example, x -= y; is the same as 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 Illustration

The *= assignment operator is made up of the * and = operators. It multiplies the right-side value by the left-side value and assigns the result to the left-side variable. For example, x *= y; is the same as 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 Illustration

The /= assignment operator is made up of the / and = operators. It divides the left operand by the right operand and assigns the quotient value to the left side variable. For instance, x = 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;
}

6.%= Operator Illustration

The %= assignment operator combines the % and = operators. It divides the left operand by the right operand and assigns the remainder to the left operand variable. For example, x%= y; is the same as 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;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button