HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 3 Data Types and Declarations

_Bool

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

This release supports the boolean data type _Bool. The variables of the data type _Bool can only have values true(1) or false(0), where true and false are preprocessor macros defined in the header file stdbool.h. The _Bool data type is a part of C99 standard (ISO/IEC 9899:1999). The C99 standard specifies boolean bitfields. For example: _Bool can be defined as in the following structure declaration:

struct foo {
_Bool boolval:1;
int i;
}

Since _Bool is defined to take only 0 and 1 as values, this type of _Bool declaration has some special properties, such as:

_Bool flip_flop = 0; // flip_flop is now false
++flip_flop; // flip_flop is now true
++flip_flop; // flip_flop is true
--flip_flop; // flip_flop is now false
--flip_flop; // flip_flop is now true

New Header file

The default location for <stdbool.h> is /usr/include. This header file is available in patch PHSS_24204 (for HP-UX 11.00) and PHSS_24205 (for HP-UX 11.11). This header file includes the following four macros:

  • bool expands to _Bool.

  • true expands to integer constant 1.

  • false expands to integer constant 0.

  • __bool_true_false_are_defined expands to decimal constant 1.

The last three macros are suitable for use in #if preprocessor directives.

Usage of _Bool

Observe the following while using _Bool:

  • The rank of _Bool is less than the rank of all other standard integer types.

  • A bit field of type _Bool may be used in an expression where an int or unsigned int is used.

  • When a scalar value is converted to _Bool the result is 0, if the value compares equal to 0, else the result is 1.

Rules for _Bool Conversion

The following conversion rules are applicable while using _Bool.

Scalar to _Bool

Boolval = scalarval ? true : false;

Boolval would be true(1) or false(0) depending whether scalarval is nonzero or zero.

_Bool to scalar

scalarval = Boolval ? 1 : 0;

scalarval would be 1 or 0 depending on Boolval being true(1) or false(0).

© Hewlett-Packard Development Company, L.P.