Skip to main content

Assignment Operators in C

Assignment operator in C

In C, the assignment operator is the "=" symbol. It assigns the value on the right side of the operator to the variable on the left side.

For example:

In the statement

x = 5;

The value 5 is assigned to the variable x.

The assignment operator can also be used to chain assignments, such as

x = y = z = 0;

which assigns the value 0 to the variables x, y, and z.

Example

Below is a program that demonstrates the use of the assignment operator:

#include <stdio.h>

int main() {
int x = 5; // assigns the value 5 to the variable x
int y = 10; // assigns the value 10 to the variable y
int z; // declares the variable z without initializing it

z = x; // assigns the value of x to z
printf("The value of z after first assignment: %d\n",z);
z = y; // assigns the value of y to z, replacing the previous value of z
printf("The value of z after second assignment: %d\n",z);

int a = x + y; // assigns the value of x + y to a, so a becomes 15
printf("The value of a after addition: %d\n",a);
int b = x - y; // assigns the value of x - y to b, so b becomes -5
printf("The value of b after subtraction: %d\n",b);
return 0;
}
Output:

Output:

The value of z after first assignment: 5
The value of z after second assignment: 10
The value of a after addition: 15
The value of b after subtraction: -5

Types of Assignment operators in C

There are multiple types of assignment operators beyond the basic assignment operator ("=") described above. These include:

  • Compound assignment operators
  • Increment and Decrement operators

Compound assignment operators

These operators are a shorthand way of performing a specific operation on a variable and then assigning the result to the same variable.

For example:

The statement "x += 5;" is equivalent to "x = x + 5;" 

Other common compound assignment operators include:

Compound Assignment OperatorEquivalent ExpressionDescription
+=x = x + yAdds the value of y to x and assigns the result to x
-=x = x - ySubtracts the value of y from x and assigns the result to x
*=x = x * yMultiplies x by y and assigns the result to x
/=x = x / yDivides x by y and assigns the result to x
%=x = x % yAssigns the remainder of x divided by y to x
<<=x = x << yShifts the bits of x left by y positions and assigns the result to x
&=x = x & yPerforms a bitwise AND operation on x and y and assigns the result to x
^=x = x ^ yPerforms a bitwise XOR operation on x and y and assigns the result to x
`=x = `x
note

The compound assignment operators perform the operation and assignment in one step, so it's more efficient than writing the equivalent expression.

Example

Below is a Compound assignment operator Example in C:

#include <stdio.h>

int main() {
int x = 5;
int y = 10;
int z = 15;

x += 2; // x = x + 2
printf("The value of x after addition: %d\n", x);
y *= 3; // y = y * 3
printf("The value of y after multiplication: %d\n", y);
z /= 5; // z = z / 5
printf("The value of z after division: %d\n", z);
x -= 1; // x = x - 1
printf("The value of x after subtraction: %d\n", x);
y %= 4; // y = y % 4
printf("The value of y after modulus: %d\n", y);
z <<= 2; // z = z << 2
printf("The value of z after bitwise left shift: %d\n", z);
x &= 2; // x = x & 2
printf("The value of x after bitwise AND: %d\n", x);
y ^= 3; // y = y ^ 3
printf("The value of y after bitwise XOR: %d\n", y);
z |= 4; // z = z | 4
printf("The value of z after bitwise OR: %d\n", z);

return 0;
}
Output:

Output:

The value of x after addition: 7
The value of y after multiplication: 30
The value of z after division: 3
The value of x after subtraction: 6
The value of y after modulus: 2
The value of z after bitwise left shift: 12
The value of x after bitwise AND: 2
The value of y after bitwise XOR: 1
The value of z after bitwise OR: 12

Increment and Decrement operators

In C, the increment operator (++) and the decrement operator (--) are used to increase or decrease the value of a variable by 1.

They can be used as prefix or postfix.

OperatorEquivalent ExpressionDescription
++xx = x + 1Increments the value of x by 1, then the new value is used in the expression
x++x = x + 1The current value of x is used in the expression, then x is incremented by 1
--xx = x - 1Decrements the value of x by 1, then the new value is used in the expression
x--x = x - 1The current value of x is used in the expression, then x is decremented by 1

In the above table, x is a variable, and the increment operator(++) increments the value of x by 1, and the decrement operator(--) decrements the value of x by 1.

note

Increment and decrement operators are not limited to integers, but it can be applied to any numeric datatype such as float, double, etc.

Also, these operators can be used in expressions and in combination with other operators. For example, x++ + y-- is a valid expression.

Prefix and Postfix Types

Prefix Increment (or Decrement) Operator

The prefix increment or decrement operator first increments or decriments the value of a variable, then the new value of variable is used in the expression.

For example:

In the statement y = ++x;, the value of x is first incremented by 1, and then the new value of x is assigned to y.

Postfix Increment (or Decrement) Operator

The Postfix increment or decrement operator first increments or decriments the value, then the new value is used in the expression.

For example:

The Postfix increment or decrement operator first uses the current value of variable in the expression and then increments the value of variable.

For example:

In the statement y = x++;, the current value of variable x is assigned to variable y, and then x is incremented by 1.

Example

#include <stdio.h>

int main() {
int x = 5;
int y;
int z;
y = ++x; // x is incremented to 6 and then assigned to y
printf("The value of x after prefix increment: %d\n", x);
printf("The value of y after prefix increment: %d\n", y);
z = x++; // z is assigned the current value of x (6) and then x is incremented to 7
printf("The value of x after postfix increment: %d\n", x);
printf("The value of z after postfix increment: %d\n", z);
y = --x; // x is decremented to 6 and then assigned to y
printf("The value of x after prefix decrement: %d\n", x);
printf("The value of y after prefix decrement: %d\n", y);
z = x--; // z is assigned the current value of x (6) and then x is decremented to 5
printf("The value of x after postfix decrement: %d\n", x);
printf("The value of z after postfix decrement: %d\n", z);
return 0;
}
Output:

Output:

The value of x after prefix increment: 6
The value of y after prefix increment: 6
The value of x after postfix increment: 7
The value of z after postfix increment: 6
The value of x after prefix decrement: 6
The value of y after prefix decrement: 6
The value of x after postfix decrement: 5
The value of z after postfix decrement: 6

Explanation:

In this program,

  • Three variables x, y and z are declared and initialized with the value 5.
  • Then the prefix increment operator is used to increment the value of x, and the new value of x is assigned to y.
  • Then the postfix increment operator is used to increment the value of x, and the current value of x is assigned to z.
  • Then prefix and postfix decrement operator is used to decrement the value of x and assigned to y and z respectively.
note

Please note that the order of evaluation is different for prefix and postfix operators. The prefix operator increments/decrements the value of the variable first and then uses the new value, whereas the postfix operator uses the current value of the variable first and then increments/decrements the value.