|
|
HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions
and Operators Relational Operators (>, >=, <, ==, !=) |
|
A relational expression consists of two expressions separated by one of six relational operators. The relational expression evaluates either to 1 (true) or 0 (false). The equality operator (==) performs the same function as Pascal's = or Fortran's .EQ.; it just looks different. Although the equality operator looks similar to the assignment operator (=), the two operators serve completely different purposes. Use the assignment operator when you want to assign a value to a variable, but use the equality operator when you want to test the value of an expression. One of the most common mistakes made by beginners and experts alike is to use the assignment operator (=) instead of the equality operator (==). For instance: while (j = 5) What is intended, clearly, is that the do_something() function should only be invoked if j equals five. It should be written while (j == 5) The first version is syntactically legal, since all expressions have a value. The value of the expression j = 5 is 5. Since this is a nonzero value, the while expression will always evaluate to true and do_something() will always be invoked. Relational operators have lower precedence than arithmetic operators. The expression a + b * c < d / f is evaluated as if it had been written (a + (b * c)) < (d / f) Among the relational operators, >, >=, <, and <= have the same precedence. The == and != operators have lower precedence. All of the relational operators have left-to-right associativity. The following table illustrates how the compiler parses complex relational expressions. Table 5-12 Examples of Expressions Using the Relational Operators
Relational expressions are often called Boolean expressions, in recognition of the nineteenth-century mathematician and logician, George Boole. Many programming languages, such as Pascal, have Boolean data types for representing true and false. The C language, however, represents these values with integers. Zero is equivalent to false, and any nonzero value is considered true. The value of a relational expression is an integer, either 1 (indicating the expression is true) or 0 (indicating the expression is false). The examples in the following table illustrate how relational expressions are evaluated:
Because Boolean values are represented as integers, you can write if (j) If j is any nonzero value, statement is executed; if j equals 0, statement is skipped. Likewise, the statement if (isalpha(ch)) is exactly the same as if (isalpha(ch) != 0) The practice of using a function call as a Boolean expression is a common idiom in C. It is especially effective for functions that return 0 if an error occurs, since you can use a construct such as if (func()) You may get unexpected results if you compare floating-point values for equality because floating-point representations are inexact for some numbers. For example, the following expression, though algebraically true, will evaluate to false on many computers: (1.0/3.0 + 1.0/3.0 + 1.0/3.0) == 1.0 This evaluates to 0 (false) because the fraction 1.0/3.0 contains an infinite number of decimal places (3.33333...). The computer is only capable of holding a limited number of decimal places, so it rounds each occurrence of 1/3. As a result, the left side of the expression does not equal exactly 1.0. This problem can occur in even more subtle ways. Consider the following code: double divide(double num, double denom) Surprisingly, the value stored in c may not equal the value returned by divide(). This anomaly occurs due to the fact that some computers can represent more decimal places for values stored in registers than for values stored in memory. Because the value returned by divide() is never stored in memory, it may not be equal to the value c, which has been rounded for memory storage.
If you execute this program, you get the following output: The number is: 5 |
|