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

Logical AND Operator

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

The logical AND operator (&&) performs the logical AND function on its operands.

Syntax

logical-AND-expression ::=
inclusive-OR-expression
logical-AND-expression && inclusive-OR-expression

Description

Each of the operands must have scalar type. The type of the left operand need not be related to the type of the right operand. The result has type int and has a value of 1 if both of its operands compare unequal to 0, and 0 otherwise. The result is not an lvalue.

The logical AND operator guarantees left-to-right evaluation. If the first operand compares equal to zero, the second operand is not evaluated.

This feature is useful for pointer operations involving pointers that can be NULL. For example, the following statement:

  if(p!=NULL && *p=='A') *p='B';

The first operand tests to see if pointer p is NULL. If p is NULL, an indirect reference could cause a memory access violation. If p is non-NULL, the second operand is safe to evaluate. The second expression checks to see if p points to the character 'A'. If the second expression is true, the && expression is true and the character that p points to is changed to 'B'. Had the pointer been NULL, the if statement would have failed and the pointer would not be used indirectly to test for the 'A' character.

Example

var1 && var2