This 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 ReferencesAll 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:
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).''
Paragraph 1:
Add the keyword _Bool.
Paragraph 2:
Change ``(entirely in lower case)'' to ``(case sensitive).''
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.
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 bitfield of type int'' to ``A bitfield of type _Bool, int.''
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.
Paragraph 1:
Change ``another integer type'' to ``another integer type other than _Bool.''
Paragraph 1:
Change ``converted to integer type'' to ``converted to integer type other than _Bool.''
Paragraph 1:
Add _Bool.
Paragraph 2:
Add _Bool.
Paragraph 4:
Change ``the specified int'' to ``a specified int.''
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 bitfield of type _Bool of any nonzero width (including a one bit bitfield), the original value and the value of the bitfield shall compare equal.
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.
Add the keyword _Bool.
§B.2.2 DeclarationsAdd the typespecifier _Bool.
§H.2.1 Boolean typeReplace 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>.
Replace 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).
Change the title to ``Extended bitfield types.''
Replace paragraph 1 with the following:
Types other than _Bool, unsigned int, or signed int can be declared as bitfields, with appropriate maximum widths (6.5.2.1).
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4