©1994-2003 Kevin Boone
Home     Section index     K-Zone home C++ tutorial (under construction)

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
compilers
contents
fundamentals
C++ tutorial: arithmetic


C/C++ uses all the standard arithmetic symbols
In a C/C++ program, '2+2' has almost exactly the same meaning as it does in any other context, i.e., add 2 and 2. 'x+y' has the same meaning as it does in algebra: add x to y. I say 'almost exactly', because there are some subtle differences that the programmer has to cope with; these will be discussed below. As well as addition, the program can subtract, divide and multiply. The symbols for these operations are '-', '/' and '*' respectively. Only the asterisk (*) for multiplication is likely to be unfamiliar; tradionally this is used because the alternative, e.g., 'a x b' would lead to a confusion between the 'times' symbol and the letter 'x'. C/C++ has some other arithmetic operations, which are less widely known; for example 'a % b' means 'the remainder when a is divided by b'. You can find further details in any standard reference book on C or C++.

'cout' can handle numbers as well as text
For example,

cout << "2 + 2 = " << 2 + 2 << "\n";

Displays the text '2 + 2 = ', followed by the result of adding 2+2, then moves printing onto the next line. In other words, cout is smart enough to figure out how to print different types of data.

Gotcha!
There are a few things that the C/C++ programmer has to be aware of when using arithmetic. Click here to see the program 'arith1.cpp'. This program calculates the results of various arithmetic operations, and prints them on the display. Have a look at the program, and compare it with the output it produces, which is as follows:

2 + 2 = 4
4 / 2 = 2
4 * 2 = 8
5 - 2 = 4
but...
2 / 3 = 0

Hang on a minute: 2 divided by 3 is 0 ? That can't be right! What do you think is going on here? The answer is that 2 / 3 is zero, if we only allow the computer to use whole numbers. The nearest whole number below 2 / 3 is zero. Technically a whole number is usually referred to as an 'integer'. C++ will assume that you want to use integer arithmetic unless you tell it otherwise. Odd as it sounds, there is a good reason for this. All modern computers can easily work with fractional numbers ('real' numbers), but integers are much, much faster. Anyway, one way to tell the computer that you want to use fractional numbers is to write '2.0' rather than just '2'. The program arith2.cpp demonstrates this, and gives the 'right' answer to '2 / 3', which is 0.6666. The crucial difference is replacing the line

cout << "2 / 3 = " << 2 / 3 << "\n";

with

cout << "2 / 3 = " << 2.0 / 3.0 << "\n";

Exercise Write a program, or modify arith2.cpp, to work out 123 times 12. Check that you get the correct answer, which is 1476.

Gotcha (again)!
This one is even more peculiar. What happens if you execute this line of program?

cout << "123 * 456 = " << 123 * 456 << "\n";

The correct answer to 123 x 456, according to my calulator, is 56088. The result that the C++ program will give depends on the compiler! This is a real downer in software engineering terms: a standardized language like C++ should not give results that depend on the compiler used. However, the C++ standards do not define how much memory should be allocated to store an integer number. Borland C++ version 3 uses 16 bits, and gives the wrong answer. Borland C++ version 5 uses 32 bits, and gives the right answer. The result of 123 x 456 is simply too big to fit in a 16 bit number, and you don't get any warning of this. Of course, you can tell the compiler to use a 'bigger' number (i.e., 32 bits) to store the result, but the problem is that you have to tell it. It can't figure it out.

As before, you will get the right answer if you tell the computer to use real-number, rather than integer, arithmetic. That is, if you calculate '123.0 * 456.0', rather than '123 * 456'.

These peculiarities rarely cause problems for experienced programmers, but they are often a nuisance for beginners. As ever, plenty of practise will help.