C programming - assignment operators

https‮gi.www//:‬iftidea.com

Assignment operators in C programming are used to assign a value to a variable. The basic assignment operator in C is the equal sign (=). It assigns the value on the right side of the operator to the variable on the left side.

Here is an example of using the basic assignment operator:

#include <stdio.h>

int main() {
  int num = 5;
  printf("num = %d\n", num);

  num = 10;
  printf("num = %d\n", num);

  return 0;
}

In this example, we declared an integer variable num and initialized it with the value 5. We then printed the value of num to the console using printf(). We then assigned the value 10 to num using the basic assignment operator and printed the new value of num to the console.

The output of the program will be:

num = 5
num = 10

In addition to the basic assignment operator, there are also compound assignment operators in C. These operators combine an arithmetic operation with the assignment operation. For example, the addition assignment operator (+=) adds a value to a variable and assigns the result to the variable. The other compound assignment operators are subtraction (-=), multiplication (*=), division (/=), and modulus (%=).

Here is an example of using a compound assignment operator:

#include <stdio.h>

int main() {
  int num = 5;
  printf("num = %d\n", num);

  num += 10;
  printf("num = %d\n", num);

  return 0;
}

In this example, we used the addition assignment operator (+=) to add the value 10 to num and assign the result back to num. We then printed the new value of num to the console.

The output of the program will be:

num = 5
num = 15

In summary, assignment operators in C programming are used to assign a value to a variable. The basic assignment operator is the equal sign (=), and there are also compound assignment operators that combine an arithmetic operation with the assignment operation.