Sams Teach Yourself C in 24 Hours (21 page)

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

Note that w is still 2 since the increment took place before the new value of w was assigned to result.

Lines 13 and 14 get the post-increment of x and print out the result. As you know, the result is obtained before the value of x is increased. Therefore, you see the value 1 (the old value of x) from the result of x++, while the new value of x is 2 since it was incremented after the assignment to result in line 13 The pre-decrement operator in line 15

causes the value of y to be reduced by 1 before the new value is assigned to the integer variable result. Therefore, you see 0 as the result of --y shown on the screen, which reflects the new value of y, which is also 0.

09 067231861x CH06 4.10.2000 11:01 AM Page 98

98

Hour 6

In line 17, however, the post-decrement operator has no effect on the assignment because the original value of z is given to the integer variable result before z is decreased by 1.

The post-decrement acts as if line 17 was simply result = z, with z then being decremented by 1 after the statement was executed. Line 18 thus prints out the result 1, which is of course the original value of z, along with 0, the value of z after the post-decrement.

Greater Than or Less Than?

There are six types of relations between two expressions: equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to. Accordingly, the C language provides these six
relational operators
:

Operator

Description

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

All the relational operators have lower precedence than the arithmetic operators.

Therefore, all arithmetic operations on either side of a relational operator are carried out before any comparison is made. You should use parentheses to enclose operations of operators that have to be performed first.

Among the six relational operators, the >, <, >=, and <= operators have higher precedence than the == and != operators.

For example, the expression

x * y < z + 3

is interpreted as

(x * y) < (z + 3)

Another important point is that all relational expressions produce a result of either 0 or 1.

In other words, a relational expression evaluates to 1 if the specified relationship holds.

Otherwise, 0 is yielded.

Given x = 3 and y = 5, for instance, the relational expression x < y gives a result of 1.

Listing 6.3 shows more examples of using relational operators.

09 067231861x CH06 4.10.2000 11:01 AM Page 99

Manipulating Data

99

When several different operators appear together in an expression, the

operands in between are associated with operators in a given order.

Operator precedence
refers to the order in which operators and operands are grouped together. Operators that have the highest precedence within an expression are grouped with their operands first,

For example, in the expression

z + x * y - 3

The * operator has higher precedence than the + and - operators. Therefore, x * y will be evaluated first, and its result becomes the right-hand operand of the + operator. The result of that is then given to the - operator as its left-hand operand.

If you want to override the default operator precedence, you can use

parentheses to group operands within an expression. If, for example, you actually wanted to multiply z + x by y - 3, you could rewrite the above expression as

(z + x) * (y - 3)

In addition, you can always use the parentheses when you aren’t quite sure about the effects of operator precedence, or you just want to make your code easier to read.

TYPE

LISTING 6.3

Results Produced by Relational Expressions

1: /* 06L03.c: Using relational operators */

2: #include

3:

4: main()

5: {

6: int x, y;

7: double z;

8:

9: x = 7;

10: y = 25;

6

11: z = 24.46;

12: printf(“Given x = %d, y = %d, and z = %.2f,\n”, x, y, z);

13: printf(“x >= y produces: %d\n”, x >= y);

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

15: printf(“x < z produces: %d\n”, x < z);

16: printf(“y > z produces: %d\n”, y > z);

17: printf(“x != y - 18 produces: %d\n”, x != y - 18);

18: printf(“x + y != z produces: %d\n”, x + y != z);

19: return 0;

20: }

09 067231861x CH06 4.10.2000 11:01 AM Page 100

100

Hour 6

After the executable 06L03.exe is executed, the following output is displayed on the screen of my computer:

Given x = 7, y = 25, and z = 24.46,

OUTPUT

x >= y produces: 0

x == y produces: 0

x < z produces: 1

y > z produces: 1

x != y - 18 produces: 0

x + y != z produces: 1

There are two integer variables, x and y, and one floating-point variable z,
ANALYSIS
declared in lines 6 and 7, respectively.

Lines 9–11 initialize the three variables. Line 12 prints out the values assigned to the variables.

Because the value of x is 7 and the value of y is 25, y is greater than x. Therefore, line 13

prints out 0, which is the result yielded by the relational expression x >= y.

Likewise, in line 14, the relational expression x == y yields 0.

Lines 15 and 16 print out the result of 1,which is yielded by the evaluations of both x < z and y > z.

The statement in line 17 displays 0, which is the result of the relational expression x != y - 18. Since y - 18 yields 7, and the value of x is 7, the != relationship does not hold. In line 18, the expression x + y != z produces 1, which is displayed on the screen.

Be careful when you compare two values for equality. Because of truncation or rounding, some relational expressions, which are algebraically true, might yield 0 stead of 1. For example, look at the following relational expression: 1 / 2 + 1 / 2 == 1

This is algebraically true and one would expect it to evaluate to 1.

The expression, however, yields 0, which means that the equal-to relationship does not hold. This is because the truncation of the integer division—

that is, 1 / 2—produces an integer: 0, not 0.5.

Another example is 1.0 / 3.0, which produces 0.33333.... This is a number with infinite number of decimal places. But the computer can only hold a limited number of decimal places. Therefore, the expression

1.0 / 3.0 + 1.0 / 3.0 + 1.0 / 3.0 == 1.0

might not yield 1 on some computers, although the expression is alge-

braically true.

09 067231861x CH06 4.10.2000 11:01 AM Page 101

Manipulating Data

101

Using the Cast Operator

In C, you can convert the data type of a variable, expression, or constant to a different one by prefixing the cast operator. This conversion does not change the operand itself; when the cast operator is evaluated, it yields the same value (but represented as a different type), which you can then use in the rest of an expression.

The general form of the cast operator is

(data-type) x

Here data-type specifies the new data type you want. x is a variable (or constant or expression) of a different data type. You have to include the parentheses ( and ) around the new data type to make a cast operator.

For example, the expression (float)5 converts the integer 5 to a floating-point number, 5.0.

The program in Listing 6.4 shows another example of using the cast operator.

TYPE

LISTING 6.4

Playing with the Cast Operator

1: /* 06L04.c: Using the cast operator */

2: #include

3:

4: main()

5: {

6: int x, y;

7:

8: x = 7;

9: y = 5;

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

11: printf(“x / y produces: %d\n”, x / y);

12: printf(“(float)x / y produces: %f\n”, (float)x / y);

13: return 0;

14: }

6

The following output is obtained by running the executable 06L04.exe on my computer: Given x = 7, y = 5

OUTPUT

x / y produces: 1

(float)x / y produces: 1.400000

In Listing 6.4, there are two integer variables, x and y, declared in line 6, and ini-ANALYSIS tialized in lines 8 and 9, respectively. Line 10 then displays the values contained by the integer variables x and y.

09 067231861x CH06 4.10.2000 11:01 AM Page 102

102

Hour 6

The statement in line 11 prints out the integer division of x/y. Because the fractional part is truncated, the result of the integer division is 1.

However, in line 12, the cast operator (float) converts the value of x to a floating-point value. Therefore, the (float)x/y expression becomes a floating-point division that returns a floating-point number. That’s why you see the floating-point number 1.400000

shown on the screen after the statement in line 12 is executed.

Summary

In this lesson you learned about the following important operators:

• The assignment operator =, which has two operands (one on each side). The value of the right-side operand is assigned to the operand on the left side. The operand on the left side must be some form of a variable which can accept the new value.

• The arithmetic assignment operators +=, -=, *=, /=, and %=, which are combinations of the arithmetic operators with the assignment operator.

• The unary minus operator (-), which evaluates to the negation of a numeric value.

• The two versions of the increment operator, ++. You know that in ++x, the

++ operator is called the pre-increment operator; and in x++, ++ is the post-increment operator.

• The two versions of decrement operator, --. You have learned that, for example, in

--x, the -- operator is the pre-decrement operator, and in x--, -- is called the post-decrement operator.

• The six relational operators in C: == (equal to), != (not equal to), > (greater than),

< (less than), >= (greater than or equal to), and <= (less than or equal to).

• How to change the data type of an expression by prefixing a cast operator to the data.

In the next lesson, you’ll learn about loops in the C language.

Q&A

Q What is the difference between the pre-increment operator and the post-increment operator?

A
The pre-increment operator increases the operand’s value by 1 first, and then yields the modified value. On the other hand, the post-increment operator yields the original value of its operand first, then increments the operand. For instance, given x = 1, the ++x expression yields 2, whereas the expression x++ evaluates to 1

before actually modifying x.

09 067231861x CH06 4.10.2000 11:01 AM Page 103

Manipulating Data

103

Q Is the unary minus operator (
-
) the same as the subtraction operator (
-
)?

A
No, they are not the same, although the two operators share the same symbol. The meaning of the symbol is determined by the context in which it appears. The unary minus operator is used to change the sign of a numeric value. In other words, the unary minus operator yields the negation of the value. The subtraction operator is an arithmetic operator that performs a subtraction between its two operands.

Q Which one has a higher precedence, a relational operator or an arithmetic
operator?

A
An arithmetic operator has a higher precedence than a relational operator. For instance, in the expression x * y + z > x + y,the operator precedence from highest to lowest goes from * to + and finally >. The entire expression is therefore interpreted as ((x * y) + z) > (x + y).

Q What value is yielded by a relational expression?

A
A relational expression evaluates to either 0 or 1. If the relation indicated by a relational operator in an expression is true, the expression evaluates to 1; otherwise, the expression evaluates to 0.

Workshop

To help solidify your understanding of this hour’s lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix C, “Answers to Quiz Questions and Exercises.”

Quiz

1. What is the difference between the = operator and the == operator?

2. In the expression x + - y - - z, which operator(s) are subtraction operators, and which one(s) are unary minus operator(s)?

6

3. Given x = 15 and y = 4, what values do the expressions x / y and

(float)x / y yield, respectively?

4. Is the expression y *= x + 5 equivalent to the expression y = y * x + 5?

Exercises

1. Given x = 1 and y = 3, write a program to print out the results of these expressions: x += y, x += -y, x -= y, x -= -y, x *= y, and x *= -y.

09 067231861x CH06 4.10.2000 11:01 AM Page 104

104

Hour 6

2. Given x = 3 and y = 6, what is the value of z after the statement

z = x * y == 18;

is executed?

3. Write a program that initializes the integer variable x with 1 and outputs results with the following two statements:

printf(“x++ produces: %d\n”, x++);

printf(“Now x contains: %d\n”, x);

4. Rewrite the program you wrote in exercise 3. This time, include the following two statements:

printf(“x = x++ produces: %d\n”, x = x++);

printf(“Now x contains: %d\n”, x);

What do you get after running the executable of the program? Can you explain why you get such a result?

Other books

Colour Me Undead by Mikela Q. Chase
Stepbrother: Impossible Love by Victoria Villeneuve
World of Warcraft: Chronicle Volume 1 by BLIZZARD ENTERTAINMENT
Amerika by Franz Kafka
The Bug - Episode 2 by Barry J. Hutchison