C Tutorial (10) : Compound operators & Typecasting

C doesn’t have many commands, but it has a lot more operators than in most other programming languages; whereas most computer programming languages have relatively few operators and lots of commands, C retains its succinct nature by providing many powerful operators.

This tutorial explores a few more operators that you need as you write programs. The compound assignment operators and the typecast operator provide the vehicles for several advanced operations.

Compound Assignment

Many times in your programs, you will have to change the value of a variable. Until now, all variables have been assigned values based on constant literal values or expressions. However, often you will need to update a variable.

A counter variable is a variable that you add 1 to when a certain event takes place. Every time a profit value goes negative, you might do this:

lossCount = lossCount + 1;      /* Adds 1 to lossCount variable */

Note : In math, nothing can be equal to itself plus 1. With computers, though, the previous assignment statement adds 1 to lossCount and then assigns that new value to lossCount, essentially adding 1 to the value of lossCount. Remember that an equals sign means to take whatever is on the right of the equals sign and store that computed value in the variable on the left.

C provides several compound operators that let you update a variable in a manner similar to the methods just described (incrementing, decrementing, and updating by more than 1). However, instead of repeating the variable on both sides of the equals sign, you have to list the variable only once.

If you want to add 1 to a variable, you can use the compound addition operator, +=. These two statements produce the same result:

lossCount = lossCount + 1;      /* Adds 1 to lossCount variable */


lossCount += 1;         /* Adds 1 to lossCount variable */

Instead of multiplying sales by 1.25 and then assigning it to itself like this:

sales = sales * 1.25;     /* Increases sales by 25 percent */

you can use the compound multiplication operator, *=, to do this:

sales *= 1.25;        /* Increases sales by 25 percent */

Note : The compound operators are quicker to use than writing out the entire assignment because you don’t have to list the same variable name on both sides of the equals sign. Also, the compound operators reduce typing errors because you don’t have to type the same variable name twice in the same statement.

Table lists all the compound assignment operators and gives examples of each.


Order for compound operators

Look at the order of operators table in the previous tutorial and locate the compound assignment operators. You’ll see that they have very low precedence. The +=, for instance, is several levels lower than the +.

The order of operators table can haunt the unwary C programmer. Think about how you would evaluate the second of these expressions:

total = 5;
total *= 2 + 3;         /* Updates the total variable */

At first glance, you might think that the value of total is 13 because you learned earlier that multiplication is done before addition. You’re right that multiplication is done before addition, but compound multiplication is done after addition, according to the order of operators. Therefore, the 2 + 3 is evaluated to get 5, and then that 5 is multiplied by the old value of total



A C typecast temporarily changes the data type of one variable to another. Here is the format of a typecast:


The dataType can be any C data type, such as int or float. The value is any variable, literal, or expression. Suppose that age is an integer variable that holds 6. The following converts age to a float value of 6.0:


If you were using age in an expression with other floats, you should typecast age to float to maintain consistency in the expression.

Note : Because of some rounding problems that can automatically occur if you mix data types, you’ll have fewer problems if you explicitly typecast all variables and literals in an expression to the same data type.

Never use a typecast with a variable on a line by itself. Typecast where a variable or an expression has to be converted to another value to properly compute a result. The preceding typecast of age might be represented like this:

salaryBonus = salary * (float)age / 150.0;

age does not change to a floating-point variable—age is changed only temporarily for this one calculation. Everywhere in the program that age is not explicitly typecast, it is still an int variable.

You can typecast an entire expression. The following statement typecasts the result of an expression before assigning it to a variable:

value = (float)(number - 10 * yrsService);

The parentheses around the expression keep the typecast away from casting only the variable number. C typecasts the preceding expression for you before storing the result in value. Nevertheless, if you want to clarify all expressions and not depend on automatic typecasting, go ahead and typecast your expressions.

Basic Arithmetic < Prev                                       Next > if-else & Relational operators



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s