# Basics of Math Operators in C Programming

Two things make math happen in C programming. The first are the math operators, which allow you to construct mathematical equations and formulas. The second are math functions, which implement complex calculations by using a single word.

Operator | Function | Example |
---|---|---|

+ | Addition | var=a+b |

– | Subtraction | var=a-b |

* | Multiplication | var=a*b |

/ | Division | var=a/b |

% | Modulo | var=a%b |

++ | Increment | var++ |

— | Decrement | var– |

+ | Unary plus | +var |

– | Unary minus | -var |

## How to increment and decrement in C programming

Here’s a handy trick, especially for those loops in your code: the increment and decrement operators. They’re insanely useful.

To add one to a variable’s value, use ++, as in:

var++;

After this statement is executed, the value of variable *var* is increased (incremented) by 1. It’s the same as writing this code:

var=var+1;

You’ll find ++ used all over, especially in for loops; for example:

for(x=0;x<100;x++)

This looping statement repeats 100 times. It’s much cleaner than writing the alternative:

for(x=0;x<100;x=x+1)

**Exercise ****1:** Code a program that displays this phrase ten times: “Get off my lawn, you kids!” Use the incrementing operator ++ in the looping statement.

**Exercise ****2:** Recode your answer for Exercise 1 using a while loop if you used a for loop, or vice versa.

The ++ operator’s opposite is the decrementing operator –, which is two minus signs. This operator decreases the value of a variable by 1; for example:

var--;

The preceding statement is the same as

var=var-1;

**Exercise ****3:** Write a program that displays values from -5 through 5 and then back to -5 in increments of 1. The output should look like this:

-5 -4 -3 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 -2 -3 -4 -5

This program can be a bit tricky, so you can see the solution in Counting Up and Down. Please don’t look ahead until you’ve attempted to solve Exercise 3 on your own.

**COUNTING UP AND DOWN**

#include <stdio.h> int main() { int c; for(c=-5;c<5;c++) printf("%d ",c); for(;c>=-5;c--) printf("%d ",c); putchar('n'); return(0); }

The crux happens at Line 9 in Counting Up and Down, but it also plays heavily off the first for statement at Line 7. You might suspect that a loop counting from -5 to 5 would have the value 5 as its stop condition, as in:

for(c=-5;c<=5;c++)

The problem with this construct is that the value of c is incremented to trigger the end of the loop, which means that c equals 6 when the loop is done. If c remains less than 5, as is done at Line 7, then c is automatically set to 5 when the second loop starts. Therefore, in Line 9, no initialization of the variable in the for statement is necessary.

**Exercise ****4:** Construct a program that displays values from -10 to 10 and then back down to -10. Step in increments of 1, as was done in Counting Up and Down, but use two while loops to display the values.

## How to prefix the ++ and — operators

The ++ operator always increments a variable’s value, and the — operator always decrements. Knowing that, consider this statement:

a=b++;

If the value of variable b is 16, you know that its value will be 17 after the ++ operation. So what’s the value of variable a — 16 or 17?

Generally speaking, C language math equations are read from left to right. Based on this rule, after the preceding statement executes, the value of variable a is 16, and the value of variable b is 17. Right?

The source code in What Comes First — the = or the ++?** **helps answer the question of what happens to variable a when you increment variable b on the right side of the equal sign (the assignment operator).

**WHAT COMES FIRST — THE = OR THE ++?**

#include <stdio.h> int main() { int a,b; b=16; printf("Before, a is unassigned and b=%dn",b); a=b++; printf("After, a=%d and b=%dn",a,b); return(0); }

**Exercise ****5:** Type the source code from What Comes First — the = or the ++? into a new project. Build and run.

When you place the ++ or — operator after a variable, it’s called *post-incrementing* or *post-decrementing,* respectively. If you want to increment or decrement the variable before it’s used, you place ++ or — *before* the variable name; for example:

a=++b;

In the preceding line, the value of b is incremented, and then it’s assigned to variable a. Exercise 6 demonstrates.

**Exercise ****6:** Rewrite the source code from What Comes First — the = or the ++? so that the equation in Line 9 increments the value of variable b before it’s assigned to variable a.

And what of this monster:

a=++b++;

Never mind! The ++*var*++ thing is an error.

## How to discover the remainder (modulus)

Of all the basic math operator symbols, % is most likely the strangest. No, it’s not the percentage operator. It’s the *modul**us* operator. It calculates the remainder of one number divided by another, which is something easier to show than to discuss.

Displaying Modulus Values codes a program that lists the results of modulus 5 and a bunch of other values, ranging from 0 through 29. The value 5 is a constant, defined in Line 3 in the program. That way, you can easily change it later.

**DISPLAYING MODULUS VALUES**

#include <stdio.h> #define VALUE 5 int main() { int a; printf("Modulus %d:n",VALUE); for(a=0;a<30;a++) printf("%d %% %d = %dn",a,VALUE,a%VALUE); return(0); }

Line 11 displays the modulus results. The %% placeholder merely displays the % character, so don’t let it throw you.

**Exercise ****7****:** Type the source code from Displaying Modulus Values into a new project. Build and run.

A modulus operation displays the remainder of the first value divided by the second. So 20 % 5 is 0, but 21 % 5 is 1.

**Exercise ****8****:** Change the VALUE constant in Displaying Modulus Values to 3. Build and run.