C Language

To share this page click on the buttons below;

Mathematical operators

With C language you can make calculus, of course. The core part of the language just defines the basic mathematical operators. This note deals about that. For more complex mathematical operations (logarithms, cosine, power and so on) you can rely on the math C standard module, however that module is not described here.

C language defines the following mathematical operators:

  • +   the plus operator
  • -   the minus operator
  • *   the multiplication operator
  • /   the division operator
  • %   the division reminder operator

The use of those operators is quite straightforward: you can apply them to every numeric C type (integers, float, double) and they perform the related operation. So you can sum two variables or a variable and constant value and so on. It turns out that you can do math also with char type because (do you remember?) this type is a numeric value, although, if you are interested to the associated character, you might find a little bit difficult to predict the result of an operation.

As it happens for math multiplication and division are executed first and then addition and subtraction: always uses parenthesis to proper specify the order of the operations you want to perform.

A little terminological note: when you use real numbers to perform mathematical calculus, so you are using float or double C types, you might hear that this is a floating point calculus. That is due to the fact that real numbers are stored using a particular notation which is called indeed floating point. There are microprocessors (tiny ones used for embedded applications) which does not have the support for real numbers (so you can use only integer numbers): it this case you can still make some real number calculus by using the fixed point notation (but this is outside the scope of this chapter).

The division: integers and reminder

The only mathematical operator which may cause some trouble is the division. First of all remember: you cannot divide anything by 0. That is a mathematical rule that is valid also in the programming domain, but it has a particular value in this field: if a division by 0 is issued (by mistake) your program will miserably crash!

When you perform a division always check that the divisor is not 0.

The second important point you have to consider is that when you divide two integers you get an integer: 5 / 2 is 2 and not 2.5 (because 5 and 2 are integers); but 5.0 / 2.0 is 2.5 because 2.0 and 5.0 are not more integer values. Anyway when you divide two integer variables you will not get the fractional part of the result.

That matter leads to the use of the division reminder operator: this operator returns the reminder of the division between integer: so 5 % 2 gives as result 1 which is the reminder of the division between the two integers numbers. A pretty obvious note here (for people that have limited familiarity with math) is that you can use the reminder operator to know if a variable is (integer) divisible by another: the reminder of the division must be 0.

Keep it short

A quite common use of mathematical operators is to use them with an assignment: you make a calculus which involves two variables and you assign back the result of this operation to one of the two variable used for the operation. That is so common that C language defined special mathematical operators to perform such operation (calculus and assignment) all at once.

  • +=   the plus and assign operator
  • -=   the minus and assign operator
  • *=   the multiplication and assign operator
  • /=   the division and assign operator
  • %=   the division and assignreminder operator

a += 2 the content of a will be increment by 2 (the very same happens for all the other operators).

The order of the operations

I have to remind that the real numbers on a Personal Computer are "real" but with some limits (have a look at the note about C types). The main limit is represented by the precision of the machine (i.e. the tiniest number the microprocessor can handle). That limit leads to an important caveat: the order you make calculus matters.

From a mathematical point of view if you first multiply and then divide or if you first divide and then multiply you always got the same result. Unfortunately that is not true when you use a microprocessor to make calculus: the result might depend on the values you are using. I am not going any further on this topic, I just want you to be aware of that: as a rule of thumb the problems come when numbers become small, so always try to perform first operations that ‘enlarge‘ numbers and only at the end perform operation that get results smaller.

To share this page click on the buttons below;