Guides:C/C Crash Course/Arithmetic

From CoderGuide

Jump to: navigation, search

Back to TOC


Your calculator can do math, so definitely your computer can. We can perform several mathematical operations in C, such as addition, subtraction, division, multiplication, and modulus division (division, but the result is the remainder of the division). More complicated mathematical operations are performed by other functions in the standard library, along with the ability to work with complex numbers. The following symbols are used to represent various mathematical operations. Note that there is no power symbol. The power symbol used on many graphing calculators, the carrot '^', is a bitwise xor operator and will give you a very different result than you expect.

%Modulus division (remainder after division)

The order of operations is just the same as was drilled into your head by your Algebra teacher: Parenthesis, Multiplication, Division, Addition, Subtracting. First, the expressions within parenthesis are evaluated, then multiplication and division, and finally subtractions and additions are performed. It is helpful to add parenthesis even though they are not technically needed to get the same result to make the code easier to read.

If you are familiar with a graphing calculator, these symbols and uses are already very familiar to you. In either case, they are very easy to understand and follow. So rather than boring you to death, and me writing more than I have to, lets move on. But, we have two special operators for increasing and decreasing a value of a variable by one. We call these the increment and decrement operators and they are ++, and --. They can appear before or after a variable and do not require any assignment operators. Here is a brief example:

a++; /*increase a by one*/
a--; /*decrease a by one*/

This is the best way to add one to a number because many CPUs have a special machine instruction that will perform these operations. In fact, if you have to add two to a number, it is better to just increment that number twice than to add two to it. Your code actually will be smaller, and faster.

When the increment or decrement operator is before the variable, then that the increment or decrement operation is performed first /++before anything else. If it comes after, the that operation is performed last. Confusing? Okay well here's a quick little example:

#include <stdio.h>
int main(){
        int a=0;
        /*first decrease a, then pass it to printf*/
        printf(" %d",a);
        /*pass a to printf, then increase a*/
        printf(" %d",a++);
        printf(" %d\n",a);

The code will output:

-1 -1 -1 0

You will see the increment and decrement operators often in loops.

Another set of many operators is the arithmetic assignment operator (also called shorthand operators). Basically, they perform some mathematical operation on two variables, and then store the result in the variable to the left of the operator. Here is a list of these assignment operators, followed by an example (there are others, but we're only going to cover these in this document):


#include <stdio.h>
int main(){
        int a,c;
        a+=a; /*a is now equal to 2 (1+1=2)*/
        a*=c; /*a is now equal to 6 (2*3=6)*/
        a/=2; /*a is now equal to 3 (6/2=3)*/
}/*end main()*/

You should use the short hand operators, increment, and decrement wherever they fit to help optimizing compilers better optimize your code when converting to machine code.

Personal tools