Making Variables Work for You

Returning to our example:

   int i, j ;

   i = 5 ;
   j = 3 ;
   printf( "sum = %d, product = %d\n", i + j, i * j ) ;

It shows a few of the things that we can do to/with variables. These include setting the value of a variable. We do this with a statement like:

i = 5 ;

The equal sign (=) here doesn't mean quite the same thing that it does in mathematics. It is the assignment operator. In many ways it's best to think of it as if it were a left-pointing arrow. It takes whatever is on the right and puts it into the variable on the left. If that variable had a value previously, then the old value is replaced.

The thing on the right of an assignment may be more than just a number. It may be any expression such as the expressions

i + j
i * j

in the printf statement. These expressions are used to do arithmetic. When the program runs, anywhere an expression appears, that expression is evaluated and it's value is used as if a number had been put there. For example, if instead of the statement j = 3 ; we had the statement j = i + 12 ;, then j would have the value 17 after that statement was executed.

What statement would you use to set the variable foo to the square of the value of bar? (Remember that to square a number, you multiply it by itself.)