Basics of Math Operators in C Programming - dummies

Basics of Math Operators in C Programming

By Dan Gookin

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:


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


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


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


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:


The preceding statement is the same as


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.


#include <stdio.h>
int main()
 int c;
 printf("%d ",c);
 printf("%d ",c);

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:


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:


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).


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

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:


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:


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 modulus 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.


#include <stdio.h>
#define VALUE 5
int main()
 int a;
 printf("Modulus %d:n",VALUE);
 printf("%d %% %d = %dn",a,VALUE,a%VALUE);

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.