Maths

Arithmatic

C provides the standard aritmetic operators '+', '-', '*', and '/' which provide addition, subtraction, multiplication, and division respectively. Additionaly there is the power opwerator '^'.

Shortcuts

Often an artimetic operation is used to alter the value of a variable, so there is a short form for this pattern.

int accountTotal = 5000;
int deposit = 300;
accountTotal += deposit; // accountTotal = accountTotal + deposit

'-=', '*=', and '/=' are also available.

Unary Operators

Incrementing and decrimenting a number by 1 is such a common operation that there's a shortcut for it.

int a = 5;
--a;
NSLog(@"%d", a); // prints 4
int b = 6;
++b;
NSLog(@"%d", b); // prints 7

Exercises

  1. Write a program that performs some arithmatic on both decimal and integer numbers. What happens when you mix the two?