C9X Addition Boolean Type for C WG14/N815

David Keaton
[email protected]

1998-02-22

1. Introduction

1.1 Purpose

This document specifies the form and interpretation of a new type for the C language.

1.2 Scope

This document, although extending the C standard, still falls within the scope of that standard, and thus follows all rules and guidelines of that standard except where explicitly noted herein.

1.3 References

  1. C9X CD1, SC22/N2620, Programming Languages -- C.
  2. WG14/N738, Farance. Improved Boolean Wording.
  3. WG14/N743, Keaton. Bit­field Enhancements.

All references to CD1 will be presented as subclause numbers. For example, §6.4 references constant expressions.

2. Rationale

Many C programs use ordinary integer types to hold only a one or zero denoting a Boolean true or false respectively. This leads to code that is difficult to read because its meaning is not apparent from the type being used.

One remedy is for each program to define its own Boolean type. This has in fact happened in a large number of cases. Implementations vary in such details as the spelling of the type name and its definition as a typedef, enumeration type, or preprocessor macro. These differences in syntax and semantics can cause undesirable differences in behavior between programs.

The solution is to standardize a Boolean type. This could have been implemented in any one of the ways that C developers have used in the past, or as a type built into the language. The committee chose the latter because it is linguistically cleaner. It gives the translator direct knowledge of the type, which allows consistent promotion rules to be defined and which may expose new opportunities for optimization as well.

Because C has existed for so long without a Boolean type, however, the new standard must coexist with the old remedies. Therefore, the type name is taken from the reserved identifier space. To maintain orthogonal promotion rules, the Boolean type is defined as an unsigned integer type capable of representing the values 0 and 1. The more conventional names for the type and its values are then made available only with the inclusion of the <stdbool.h> header. In addition, the header defines a feature test macro to aid in integrating new code with old code that defines its own Boolean type.

3. Language edits

The necessary changes are given below, listed by location in C9X CD1.

§4. Compliance

Paragraph 2:
Add <stdbool.h> to the list of headers required in a freestanding implementation.

Forward references:
Add ``boolean type and values <stdbool.h> (7.1.7).''

§6.1.1 Keywords

Paragraph 1:
Add the keyword _Bool.

Paragraph 2:
Change ``(entirely in lower case)'' to ``(case sensitive).''

§6.1.2.5 Types

After paragraph 1, add the following paragraph:
An object declared as type _Bool is large enough to store the values 0 and 1.

Paragraph 5, replace this:

    The unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types.

with the following:

    The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types.

Paragraph 6, before the first sentence, add the following:
The standard signed integer types and standard unsigned integer types are collectively called the standard integer types.

§6.2.1.1 Characters and integers

Change the title to ``Boolean, characters, and integers.''

Paragraph 1:
Delete the third and fifth items in the list.
Before ``The rank of char . . .'' insert:
-- The rank of any unsigned integer type shall equal the rank of its corresponding signed integer type, if such a corresponding type exists.
-- The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.

Paragraph 1, after ``The rank of char . . .'' insert:
-- The rank of _Bool shall be less than the rank of all other standard integer types.

Paragraph 2:
Change ``A bit­field of type int'' to ``A bit­field of type _Bool, int.''

§6.2.1.1+ Boolean type

Add the following paragraph as a new subclause between 6.2.1.1 and 6.2.1.2:
When a value is demoted to a _Bool, the result is 0 if the value equals 0, and 1 otherwise.

§6.2.1.2 Signed and unsigned integers

Paragraph 1:
Change ``another integer type'' to ``another integer type other than _Bool.''

§6.2.1.3 Real floating and integer

Paragraph 1:
Change ``converted to integer type'' to ``converted to integer type other than _Bool.''

§6.5.2 Type specifiers

Paragraph 1:
Add _Bool.

Paragraph 2:
Add _Bool.

Paragraph 4:
Change ``the specified int'' to ``a specified int.''

§6.5.2.1 Structure and union specifiers

Paragraph 8:
Change ``signed int or unsigned int'' to ``_Bool, signed int, or unsigned int.''

After paragraph 8, add the following paragraph:
If the value 0 or 1 is stored into a bit­field of type _Bool of any nonzero width (including a one bit bit­field), the original value and the value of the bit­field shall compare equal.

§7.1.7 Boolean type and values <stdbool.h>

Replace paragraphs 1-3 with the following:

    The header <stdbool.h> defines four macros.

    The macro

      bool

    is defined to be _Bool.

    The remaining three macros are suitable for use in #if preprocessing directives. They are

      true

    which expands to the decimal constant 1,

      false

    which expands to the decimal constant 0, and

      __bool_true_false_are_defined

    which expands to the decimal constant 1.

Footnote 138:
Delete this footnote since it no longer applies now that_Bool is a first class type. Alternately, the contents of the footnote could be adapted for use in the rationale.

§B.1.3 Keywords

Add the keyword _Bool.

§B.2.2 Declarations

Add the type­specifier _Bool.

§H.2.1 Boolean type

Replace paragraph 1 with the following:
The LIA-1 data type Boolean is implemented by the C data type _Bool, with values of true and false from <stdbool.h>.

§K.2 Undefined behavior

Replace this:

    A bit­field is declared with a type other than a qualified or unqualified version of signed int or unsigned int (6.5.2.1).

with the following:

    A bit­field is declared with a type other than a qualified or unqualified version of _Bool, signed int, or unsigned int (6.5.2.1).

§K.5.8 Non­int bit­field types

Change the title to ``Extended bit­field types.''

Replace paragraph 1 with the following:
Types other than _Bool, unsigned int, or signed int can be declared as bit­fields, with appropriate maximum widths (6.5.2.1).

4. Conclusion

The above changes will bring C9X into alignment with the committee's decision by adding a first class Boolean type. This will alleviate past problems due to differing implementations.