In mathematics, the *equal* (`=`

) sign is a symbol that represents equality between two parts. In
computer science, the *equal* operator has a different meaning: assignment. That is to say
that the right part of the operator will be assigned to the left part which is necessarily a variable.

`x = a + b;`

In the example above, the result of the calculation `a + b`

will be assigned to the variable `x`

. To clarify
this concept of assignment, let's study the `a = a + 1`

from the **mathematical** and **computer science** point of view:

In the case of a mathematical equation, this is a relationship that can be simplified:

$$ \begin{align} a &= a+1 \\ \cancel{a} &= \cancel{a}+1 \\ 0 &= 1 \end{align} $$

This relationship is always false since 0 ≠ 1.

In C, the statement `a = a + 1;`

means that the new value of the variable `a`

will be equal to the old value plus one.
We would represent this relation in the following mathematical form:

$$ a_{n+1} = a_n + 1 $$

In C, we frequently use instructions of the type `i = i + 2;`

where the variable
which is assigned the final value is also used in the calculation. There is a
short notation `i += 2`

which is equivalent to `i = i + 2;`

.

We introduce here a new assignment operator `+=`

which sums the two operands
before assigning the result to the left operand. Here is an example:

```
int x = 3;
// Here the value of x is 3
x += 7;
// Now x is 10 (3+7)
```

These combined operators allow to simplify operations such as adding a value to a variable and storing the result. Here is the list of assignment operators with their equivalent instruction :

Operator | Example | Same as |
---|---|---|

`+=` |
`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;` |

Consider the following example:

```
int a = 18;
// Division combined operator
a /= 3;
printf ("a = %d\n", a );
```

In the following code, modify each operation in order to use combined operators:

```
int x1=4, x2=2, x3=6;
// USE COMBINED OPERATORS (+=, -=, *= ...)
x1 = 2 + x1;
x1 = x1 * x2;
x2 = x2 % (1+x1);
x3 = x3 / x2;
// Display the results
printf ("x1= %d\n", x1);
printf ("x2= %d\n", x2);
printf ("x3= %d\n", x3);
```

The result must obviously be the same:

```
x1 = 12
x2 = 2
x3 = 3
```

**What does the following program display?**

```
int x=3;
x += 2;
printf ("%d", x );
```

Check
Bravo! The equivalent instruction is

`x = x +2;`

.
Try again...
**What does the following program display?**

```
float x=10.0;
x /= 3.0;
printf ("%f", x );
```

Check
Bravo! The equivalent instruction is

`x = x / 3.0;`

Try again...
**What is the equivalent instruction with a combined operator?**

`x = 12 % x;`

Check
Bravo! The destination variable must be to the left of the operator

`%`

, otherwise it is not possible (like here).
Try again...
**What is the equivalent instruction with a combined operator?**

`x = x % 12;`

Check
Bravo! here, it is possible because the destination variable

`x`

is to the left of the operator `%`

.
Try again...
- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/21/2022