- If computer programming didn't involve strange and unusual symbols, then it just wouldn't be that interesting. To keep you on your toes, many of the popular programming languages employ special symbols as operators. Some of these operators you recognize, such as + and - , others are puzzling and strange. The C language employs a host of oddball symbols. Specifically, some of the mathematical operators can be quite unusual to behold. They do, however, serve a practical and useful function.
All assignment operators fill a variable with a value. It assigns the value. The basic assignment operator is the equal sign. It takes whatever value appears on the right and assigns it to the variable on the left. That value can be a literal value, such as 98.6. It can be the value stored in another variable. It can be the result of a calculation, which can involve literal values or other variables. It can be a value returned from a function. Or it can be any combination of these options.
In all cases, the equal sign operator is used and the variable's original value, if any were assigned, is replaced by the new value. When the variable itself needs to be modified, you can do so by stating it twice, once on both sides of the assignment operator. This equation works, because the value generated on the right side of the equation is calculated first. Then it's assigned to whatever's on the left. In the desire for efficiency, as well as for a love of obscurity, the C programming language offers you a shortcut by removing the redundant variable name and instead using one of the assignment operators.
+=, -=, /=, *=, and %=. Each of these operators serves to modify a single variable by a literal value, an equation, or the result of some function. Remember the order in which the operators go. To help you remember, just reverse their order. += is an assignment operator that increases a variable's value. =+ is an assignment operator that assigns a positive value to a variable.
That's the way I remember. In CodeBlocks, open exercise 01-01_assignments.c. This code takes a variable v and modifies it several times. The same chunk of text is repeated over and over, first with addition, then subtraction, then division, then multiplication, and finally, modulus or the remainder operator. If you build and run the code, you see the predictable output. Each equation modifies the value of variable v by changing its existing value.
Therefore, each equation is ripe for replacement with an assignment operator. At line 10, edit the equation to read v += 20, which means that 20 is added to whatever value is stored in variable v. The equation works. Similar modifications can be made to lines 14, 18, 22, and 26. The key is to remember that the order is math operator, then equal sign. If it were the other way, for example, at line 10, if I wrote v =+ 20, then I'm merely assigning the value +20 to v.
That's not what I want. Save the changes, and build and run. The output is the same. Remember, these operators are used when you modify an existing value in a variable. If the variable is uninitialized, then the result of the operation is unpredictable.
- Using assignment operators
- Working with arguments in the main function
- Setting up global variables
- Using static variables
- Sorting an array
- Building an array of structures
- Working with the ampersand (&) and asterisk (*) pointer operators