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

Unary Arithmetic Operators

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

A unary arithmetic operator combined with a single operand forms a unary expression used to negate a variable, or determine the ones complement or logical complement of the variable.

Syntax

+ cast-expression
- cast-expression
~ cast-expression
! cast-expression

Description

The unary plus operator operates on a single arithmetic operand, as is the case of the unary minus operator. The result of the unary plus operator is defined to be the value of its operand. For example, just as -2 is an expression with the value negative 2, +2 is an expression with the value positive 2.

In spite of its definition, the unary plus operator is not purely a no-op. According to the ANSI standard, an unary plus operation is an expression that follows the integral promotion rule. For example, if i is defined as a short int, then sizeof (i ) is 2. However, sizeof (+i) is 4 because the unary plus operator promotes i to an int. The result of the unary - operator is the negative value of its operand. The operand can be any arithmetic type. The integral promotion is performed on the operand before it is used. The result has the promoted type and is not an lvalue.

The result of the unary ~ operator is a one's (bitwise) complement of its operand. The operand can be of any integral type. The integral promotion is performed on the operand before it is used. The result has the promoted type and is not an lvalue.

The result of the unary ! operator is the logical complement of its operand. The operand can be of any scalar type. The result has type int and is not an lvalue. If the operand had a zero value, the result is 1. If the operand had a nonzero value, the result is 0.