HPlogo HP C/HP-UX Reference Manual: Workstations and Servers > Chapter 5 Expressions

Relational Operators

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

The relational operators compare two operands to determine if one operand is less than, greater than, less than or equal to, or greater than or equal to the other.

Syntax

relational-expression ::=
shift-expression
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression

Description

The usual arithmetic conversions are performed on the operands if both have arithmetic type. Both operands must be arithmetic or both operands must be pointers to the same type. In general, pointer comparisons are valid only between pointers that point within the same aggregate or union.

Each of the operators < (less than), > (greater than), <= (less than or equal) and >= (greater than or equal) yield 1 if the specified relation is true; otherwise, they yield 0. The resulting type is int and is not an lvalue.

When two pointers are compared, the result depends on the relative locations in the data space of the objects pointed to. Pointers are compared as if they were unsigned integers.

Because you can use the result of a relational expression in an expression, it is possible to write syntactically correct statements that appear valid but which are not what you intended to do. An example is a<b<c. This is not a representation of "a is less than b and b is less than c." The compiler interprets the expression as (a<b)<c. This causes the compiler to check whether a is less than b and then compares the result (an integer 1 or 0) with c.

Examples

var1 < var2

var1 > var2

var1 <= var2

var1 >= var2