Sams Teach Yourself C in 24 Hours (20 page)

BOOK: Sams Teach Yourself C in 24 Hours
10.79Mb size Format: txt, pdf, ePub

08 067231861x pt 2 1/25/00 10:25 AM Page 89

PART II

Operators and Control-

flow Statements

Hour

6 Manipulating Data

7 Working with Loops

8 Using Conditional Operators

9 Working with Data Modifiers and Math

Functions

10 Controlling Program Flow

08 067231861x pt 2 1/25/00 10:25 AM Page 90

09 067231861x CH06 4.10.2000 11:01 AM Page 91

HOUR 6

Manipulating Data

“The question is,” said Humpty Dumpty,

“which is to be master—that’s all.”

—L. Carroll

You can think of operators as verbs in C that let you manipulate data (which are like nouns). In fact, you’ve learned some operators, such as + (addition),

- (subtraction), * (multiplication), / (division), and % (remainder), in Hour 3,

“Learning the Structure of a C Program.” The C language has a rich set of operators. In this hour, you’ll learn about more operators, such as

• Arithmetic assignment operators

• Unary minus operator

• Increment and decrement operators

• Relational operators

• Cast operator

09 067231861x CH06 4.10.2000 11:01 AM Page 92

92

Hour 6

Arithmetic Assignment Operators

Before jumping into the arithmetic assignment operators, let’s first take a closer look at the assignment operator itself.

The Assignment Operator (
=
)

In the C language, the = operator is called an
assignment operator
, which you’ve seen and used for several hours.

The general statement form to use an assignment operator is

left-hand-operand = right-hand-operand;

Here the statement causes the value of the right-hand-operand to be assigned (or written) to the memory location of the left-hand-operand. Thus, after the assignment, left-hand-operand will be equal to the value of right-hand-operand
.
Additionally
,
the entire assignment expression evaluates to the same value that is assigned to the left-hand-operand.

For example, the statement a = 5; writes the value of the right-hand operand (5) into the memory location of the integer variable a (which is the left-hand operand in this case).

Similarly, the statement b = a = 5; assigns 5 to the integer variable a first, and then to the integer variable b. After the execution of the statement, both a and b contain the value of 5.

It is important to remember that the left-hand operand of the assignment operator must be an expression to which you can legally write data. An expression such as 6 = a, although it may look correct at a glance, is actually backwards and will not work. The =

operator always works from right to left; therefore the value on the left must be some form of a variable that can receive the data from the expression on the right.

Don’t confuse the assignment operator (=) with the relational operator, ==

(called the
equal-to operator
). The == operator is introduced later in this hour.

Combining Arithmetic Operators with
=

Consider this example: Given two integer variables, x and y, how do you assign the addition of x and y to another integer variable, z?

09 067231861x CH06 4.10.2000 11:01 AM Page 93

Manipulating Data

93

By using the assignment operator (=) and the addition operator (+), you get the following statement:

z = x + y;

As you can see, it’s pretty simple. Now, consider the same example again. This time, instead of assigning the result to the third variable, z, let’s write the result of the addition back to the integer variable, x:

x = x + y;

Remember, the = operator always works from right to left, so the right side will be evaluated first. Here, on the right side of the assignment operator (=), the addition of x and y is executed; on the left side of =, the previous value of x is replaced with the result of the addition from the right side.

The C language gives you a new operator, +=, to do the addition and the assignment together. Therefore, you can rewrite the statement x = x + y; as

x += y;

The combinations of the assignment operator (=) with the arithmetic operators, +, -, *, /, and %, give you another type of operators—
arithmetic assignment operators
:
Operator

Description

+=

Addition assignment operator

-=

Subtraction assignment operator

*=

Multiplication assignment operator

/=

Division assignment operator

%=

Remainder assignment operator

The following shows the equivalence of statements:

6

x += y; is equivalent to x = x + y;

x -= y; is equivalent to x = x - y;

x *= y; is equivalent to x = x * y;

x /= y; is equivalent to x = x / y;

x %= y; is equivalent to x = x % y;

Note that the statement

09 067231861x CH06 4.10.2000 11:01 AM Page 94

94

Hour 6

z = z * x + y;

is not equivalent to the statement

z *= x + y;

because

z *= x + y

multiplies z by the entire right-hand side of the statement, so the result would be the same as

z = z * (x + y);

Listing 6.1 gives an example of using some of the arithmetic assignment operators.

TYPE

LISTING 6.1

Using Arithmetic Assignment Operators

1: /* 06L01.c: Using arithemtic assignment operators */

2: #include

3:

4: main()

5: {

6: int x, y, z;

7:

8: x = 1; /* initialize x */

9: y = 3; /* initialize y */

10: z = 10; /* initialize z */

11: printf(“Given x = %d, y = %d, and z = %d,\n”, x, y, z);

12:

13: x = x + y;

14: printf(“x = x + y assigns %d to x;\n”, x);

15:

16: x = 1; /* reset x */

17: x += y;

18: printf(“x += y assigns %d to x;\n”, x);

19:

20: x = 1; /* reset x */

21: z = z * x + y;

22: printf(“z = z * x + y assigns %d to z;\n”, z);

23:

24: z = 10; /* reset z */

25: z = z * (x + y);

26: printf(“z = z * (x + y) assigns %d to z;\n”, z);

27:

28: z = 10; /* reset z */

29: z *= x + y;

30: printf(“z *= x + y assigns %d to z.\n”, z);

31:

32: return 0;

33: }

09 067231861x CH06 4.10.2000 11:01 AM Page 95

Manipulating Data

95

After this program is compiled and linked, an executable file is created. On my machine, this executable file is named as 06L01.exe. The following is the output displayed after I run the executable:

Given x = 1, y = 3, and z = 10,

OUTPUT

x = x + y assigns 4 to x;

x += y assigns 4 to x;

z = z * x + y assigns 13 to z;

z = z * (x + y) assigns 40 to z;

z *= x + y assigns 40 to z.

Line 2 in Listing 6.1 includes the header file stdio.h by using the include
ANALYSIS
directive. The stdio.h header file is needed for the printf() function used in lines 4–33.

Lines 8–10 initialize three integer variables, x, y, and z, which are declared in line 6.

Line 11 then prints out the initial values assigned to x, y, and z.

The statement in line 13 uses one addition operator and one assignment operator to add the values contained by x and y, and then assigns the result to x. Line 14 displays the result on the screen.

Similarly, lines 17 and 18 do the same addition and display the result again, after the variable x is reset to the value 1 in line 16. This time, the arithmetic assignment operator,

+=, is used.

The value of x is reset again in line 20. Line 21 performs a multiplication and an addition and saves the result to the integer variable z; that is, z = z * x + y;. The printf() call in line 22 displays the result, 13, on the screen. Again, the x = 1; statement in line 20 resets the integer variable, x.

Lines 24–30 display two results from two computations. The two results are actually the same (that is, 40) because the two computations in lines 25 and 29 are equivalent. The only difference between the two statements in lines 25 and 29 is that the arithmetic assignment operator, *=, is used in line 29.

6

Getting Negations of Numeric Values

If you want to change the sign of a number, you can put the minus operator (-) right before the number. For instance, given an integer of 7, you can get its negative value by changing the sign of the integer like this: -7. Here, - is the minus operator.

The - symbol used in this way is called the
unary minus operator
. This is because the operator takes only one operand: the expression to its immediate right. The data type of the operand can be any integer or floating-point number.

09 067231861x CH06 4.10.2000 11:01 AM Page 96

96

Hour 6

You can apply the unary minus operator to an integer or a floating-point variable as well.

For example, given x = 1.234, -x equals -1.234. Or, given x = -1.234, -x equals 1.234 since negating a negative value results in a positive number.

Don’t confuse the unary minus operator with the subtraction operator,

although both operators use the same symbol. For instance, the following statement:

z = x - -y;

is actually the same as this statement:

z = x - (-y);

or this one:

z = x + y;

Here, in both statements, the first - symbol is used as the subtraction operator, while the second - symbol is the unary minus operator.

Incrementing or Decrementing by One

The increment and decrement operators are very handy to use when you want to add or subtract 1 from a variable. The symbol for the increment operator is ++. The decrement operator is --.

For instance, you can rewrite the statement x = x + 1; as ++x;, or you can replace x =

x - 1; with --x;.

Actually, there are two versions of the increment and decrement operators. In the ++x; statement, where ++ appears before its operand, the increment operator is called the
pre-increment operator
. This refers to the order in which things happen: the operator first adds 1 to x, and then yields the new value of x. Likewise, in the statement --x;, the
pre-decrement operator
first subtracts 1 from x and then yields the new value of x.

If you have an expression like x++, where ++ appears after its operand, you’re using the
post-increment operator
. Similarly, in x--, the decrement operator is called the
post-decrement operator
.

For example, in the statement y = x++;, y is assigned the original value of x first, then x is increased by 1.

The post-decrement operator has a similar story. In the statement y = x--; the assignment of y to the value of x takes place first, then x is decremented. The program in Listing 6.2 shows the differences between the two versions of increment operators and decrement operators.

09 067231861x CH06 4.10.2000 11:01 AM Page 97

Manipulating Data

97

TYPE

LISTING 6.2

Using Pre- or Post-Increment and Decrement Operators

1: /* 06L02.c: pre- or post-increment(decrement) operators */

2: #include

3:

4: main()

5: {

6: int w, x, y, z, result;

7:

8: w = x = y = z = 1; /* initialize x and y */

9: printf(“Given w = %d, x = %d, y = %d, and z = %d,\n”, w, x, y, z); 10:

11: result = ++w;

12: printf(“++w evaluates to %d and w is now %d\n”, result, w);

13: result = x++;

14: printf(“x++ evaluates to %d and x is now %d\n”, result, x);

15: result = --y;

16: printf(“--y evaluates to %d and y is now %d\n”, result, y);

17: result = z--;

18: printf(“z-- evaluates to %d and z is now %d\n”, result, z);

19: return 0;

20: }

The following result is obtained by running the executable file 06L02.exe: Given w = 1, x = 1, y = 1, and z = 1,

OUTPUT

++w evaluates to 2 and w is now 2

x++ evaluates to 1 and x is now 2

--y evaluates to 0 and y is now 0

z-- evaluates to 1 and z is now 0

Inside the main() function, line 8 in Listing 6.2 assigns 1 to each of the integer
ANALYSIS
variables, w, x, y, and z. The printf() call in line 9 displays the values contained by the four integer variables.

Then, the statement in line 11 is executed and the result of the pre-increment of w is assigned to the integer variable result. In line 12, the value of result, which is 2, is
6

printed out to the screen, along with the value of w after the pre-increment statement.

Other books

The Magician’s Land by Lev Grossman
The Noble Pirates by Rima Jean
Always and Forever by Lindsay McKenna
Sharpe's Rifles by Cornwell, Bernard
Kane by Steve Gannon
A Witch's Fury by Kim Schubert
Tempting Fate by Amber Lin