Jump to: navigation, search

Chapter 4 - Arithmetic operators

The addition operator

To add two numbers together in C, we use the addition operator, +.

It is very simple to use, as the following program demonstrates:

int main(void)
{
  int firstnumber = 6;
  int secondnumber = 12;
  int result;

  result = firstnumber + secondnumber;

  /* result now holds the value 18 */

  return 0;
}

This program produces no output. Output is, in fact, quite a problem for the time being. This is not a shortcoming of C, but a consequence of the fact that this tutorial is designed to introduce a number of simple concepts before we get onto slightly more difficult and involved ideas. For now, let us just accept that the program does what the comment says it does, and move on to the subtraction operator.

The subtraction operator

As you might expect, the subtraction operator uses the symbol -. The following program illustrates the syntax:

int main(void)
{
  int firstnumber = 6;
  int secondnumber = 12;
  int result;

  result = firstnumber - secondnumber;

  /* result now holds the value -6 */

  return 0;
}

Perhaps somewhat oddly, C classes the - operator as an additive operator. It makes slightly more sense if you reflect that addition and subtraction are inverse operations.

The multiplication operator

To multiply two numbers together in C, we use the * operator. This isn't quite the symbol that we are accustomed to using to represent multiplication. In the early days of computers, only a very limited set of characters was available, and it wasn't possible to use any of the more traditional symbols for multiplication. The closest candidates were the letter x and the full stop (or period), neither of which would have been a good fit.

The following program illustrates the multiplication operator in use:

int main(void)
{
  int firstnumber = 6;
  int secondnumber = 12;
  int result;

  result = firstnumber * secondnumber;

  /* result now holds the value 72 */

  return 0;
}

The division operator

C, in common with most other programming languages, uses the / symbol to represent division. Again, this is a legacy of the limited character set available in the early days of computing. The logic of the / symbol for division is that it is reasonably close to the way we write fractions: for example, 3/4 (three quarters) can be interpreted as "three divided by four".

Division comes complete with a couple of caveats. Firstly, in keeping with mathematical convention, we are not supposed to divide by 0. If you try to divide an int value by 0, the computer will complain loudly (and your program will halt with an error).

Secondly, a division of one integer by another integer gives an integer result. So if you divide 12 by 6, you'll get the expected answer, 2. But if you divide 6 by 12, you'll get 0, not 0.5 as you might have anticipated. As it turns out, this is usually what we want anyway. When it isn't, there are ways of dealing with it, which we'll get to eventually.

Here is a program that uses the division operator:

int main(void)
{
  int firstnumber = 6;
  int secondnumber = 12;
  int result;

  result = firstnumber / secondnumber;

  /* result now holds the value 0, the result of rounding 6/12 down
     to an integer.
   */

  result = secondnumber / firstnumber;

  /* result now holds the value 2 */

  return 0;
}

Using the same logic as before, C classes the / operator as a multiplicative operator.

The remainder operator

C provides a fifth arithmetic operator, which gives us the remainder after a division. The symbol % is used for this operation. The way it works is fairly straightforward: if we have a line like:

  result = 17 % 5;

the computer divides 17 by 5, but instead of giving the result of the division (3, of course, because this is integer division, so the result is rounded down), it gives the remainder. 5 goes into 17 three times, with 2 remainder. So 17 % 5 is 2. Here is a full program illustrating this:

int main(void)
{
  int firstnumber = 17;
  int secondnumber = 5;
  int result;

  result = firstnumber % secondnumber;

  /* result now holds the value 2, the remainder resulting after a division
     of 17 by 5
   */

  return 0;
}

It is not permissible for 0 to be the second operand of the % operation, for the same reason that it cannot be the second operand of the / operation.

Since % (broadly speaking) yields a result that stems from a division (which is the inverse operation to multiplication), C classes it as a multiplicative operator.

Combining operators

It is perfectly legal to combine operators into more complicated expressions.

int main(void)
{
  int result;

  result = 4 * 5 + 3 * 2 - 9;

  /* result now holds the value 17 */

  return 0;
}

How do we get to 17? The answer lies in C's precedence rules. These rules are not actually defined by C as such; rather, they are consequences of the way that C's grammar is defined. But we can think of them in terms of precedence. Multiplication, division, and remainder take precedence over addition and subtraction. Apart from that, the arithmetic proceeds left to right. Thus, the overall result of the expression 24 - 3 * 5 + 6 is arrived at by taking the result of the multiplication of 3 * 5 and subtracting it from 24, and then adding 6 to that result. So 3 * 5 = 15, 24 - 15 = 9, and then 9 + 6 = 15.

Adjacence of digit characters

Every character constant has a numeric value -- an int. The precise value used will depend on your character set, and generally speaking it doesn't matter what it is. Nevertheless, it is helpful to know that the characters '0' through '9' are guaranteed to have consecutive values, regardless of the character set being used.

Thus, in the ASCII character set, the character constant '0' has the value 48. This means that, if we pass 48 to putchar like this:

#include <stdio.h>  /* needed for obtaining the prototype of putchar */

int main(void)
{
  putchar(48); /* assumes the ASCII character set */
  putchar('\n');
  return 0;
}

the program will write the character '0' on the standard output device (probably your screen). If, however, you are using a computer that employs the EBCDIC character set, the above program will instead print (in all likelihood) an unreadable control character. On the other hand, on an EBCDIC computer the following program will write the character '0':

#include <stdio.h>

int main(void)
{
  putchar(240);
  putchar('\n');
  return 0;
}

whereas on an ASCII computer it will print something else (it really doesn't matter what at this stage). If we don't know what computer our program will eventually run on, or if it has to run on computers with differing character sets, it is better to putchar('0');' as this will work irrespective of the character set in use.

In ASCII, 48 is the code point of the character '0'. In EBCDIC, the code point for the same character is 240.

Fortunately, C requires that the digits '0' through '9' must have consecutive code points, starting at '0'. So we know that, if '0' is represented internally by the code point 48, then '1' must be represented by code point 49, '2' by code point 50, and so on. If '0' is represented by the code point 240, then '1' must have the value 241, '2' must have the value 242, and so on. What's more, if we use '0' rather than rely on a specific code point value, we can stop worrying about whether we are using ASCII or EBCDIC. To convert a single-digit value (0 to 9) into the character equivalent, we can simply add '0' to it. And of course we can convert a single-digit character into something we can use for arithmetic, simply by subtracting '0' from it.

This means that we can demonstrate some very basic addition:

#include <stdio.h> /* for putchar */

int main(void)
{
  int zero = '0';

  int one = zero + 1;
  int three = zero + 3;

  putchar(one);

  putchar('\n'); /* don't forget to complete the
                    line with a newline character */

  putchar(three);
  putchar('\n');

  return 0;
}

The above program outputs 1, a newline, 3, and another newline.

Summary

In this chapter, you learned about C's arithmetic operators for addition, subtraction, multiplication, division, and remainder. You also learned that digit characters have consecutive code points.

In the next chapter, we will begin to learn about C's control structures, and we will start to tackle the problem of output.

Progress

Terminology
  • code point
  • object
  • type
  • value
  • definition
  • assignment
  • initialisation
  • standard input
  • standard output
  • standard error
  • preprocessor
  • standard library function
  • user function
  • control flow
  • function
  • function prototype
Syntax
  • comments
  • types
  • operators
    • assignment operators
      • the = operator
    • additive operators
      • the + operator
      • the - operator
    • multiplicative operators
      • the * operator
      • the / operator
      • the % operator
Standard library functions, by header
Personal tools