C9X Addition | Boolean Type for C | WG14/N815 |
---|
David Keaton
[email protected]
1998-02-22
1. Introduction1.1 PurposeThis document specifies the form and interpretation of a new type for the C language. 1.2 ScopeThis 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
All references to CD1 will be presented as subclause numbers. For example, §6.4 references constant expressions. 2. RationaleMany 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 editsThe necessary changes are given below, listed by location in C9X CD1. §4. ComplianceParagraph 2: Forward references: §6.1.1 KeywordsParagraph 1: Paragraph 2: §6.1.2.5 TypesAfter paragraph 1, add the following paragraph: 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: §6.2.1.1 Characters and integersChange the title to ``Boolean, characters, and integers.'' Paragraph 1: Paragraph 1, after ``The rank of char . . .'' insert: Paragraph 2: §6.2.1.1+ Boolean typeAdd the following paragraph as a new subclause between 6.2.1.1 and 6.2.1.2: §6.2.1.2 Signed and unsigned integersParagraph 1: §6.2.1.3 Real floating and integerParagraph 1: §6.5.2 Type specifiersParagraph 1: Paragraph 2: Paragraph 4: §6.5.2.1 Structure and union specifiersParagraph 8: After paragraph 8, add the following paragraph: §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: §B.1.3 KeywordsAdd the keyword _Bool. §B.2.2 DeclarationsAdd the typespecifier _Bool. §H.2.1 Boolean typeReplace paragraph 1 with the following: §K.2 Undefined behaviorReplace this: A bitfield 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 bitfield 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 Nonint bitfield typesChange the title to ``Extended bitfield types.'' Replace paragraph 1 with the following: 4. ConclusionThe 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. |