Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why didn't C have a boolean data type prior to C99?

I realise you can just #define some integers, but why didn't C have a dedicated boolean data type before C99?

It's such a common occurence in programming and logic, I don't understand the absense of an explicit type and notation.

like image 336
xyz Avatar asked Mar 11 '10 15:03

xyz


People also ask

Does C99 have Booleans?

The C99 standard for C language supports bool variables. Unlike C++, where no header file is needed to use bool, a header file “stdbool.

When did bool add C?

An introduction to how to use booleans in C C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”.

In which language were Boolean data types introduced?

The language Pascal (1970) introduced the concept of programmer-defined enumerated types. A built-in Boolean data type was then provided as a predefined enumerated type with values FALSE and TRUE . By definition, all comparisons, logical operations, and conditional statements applied to and/or yielded Boolean values.

Is bool a native type in C?

1 Expert Answer. bool exists in the current C - C99, but not in C89/90. In C99 the native type is actually called _Bool , while bool is a standard library macro defined in stdbool. h (which expectedly resolves to _Bool ).


1 Answers

If you spend a little time in the library, you don't have to speculate. Here are some statements taken from Dennis Ritchie's paper on the evolution of C. The context is that Dennis is building on Ken Thompson's language B, which was implemented on the very tiny PDP-7, a word-addressed machine. Because of growing interest, the group got one of the very first PDP-11s. Dennis writes,

The advent of the PDP-11 exposed several inadequacies of B's semantic model. First, its character-handling mechanisms, inherited with few changes from BCPL, were clumsy: using library procedures to spread packed strings into individual cells and then repack, or to access and replace individual characters, began to feel awkward, even silly, on a byte-oriented machine.

The B and BCPL model implied overhead in dealing with pointers: the language rules, by defining a pointer as an index in an array of words, forced pointers to be represented as word indices. Each pointer reference generated a run-time scale conversion from the pointer to the byte address expected by the hardware.

For all these reasons, it seemed that a typing scheme was necessary to cope with characters and byte addressing, and to prepare for the coming floating-point hardware. Other issues, particularly type safety and interface checking, did not seem as important then as they became later.

(Emphasis mine.)

The paper goes on to describe Dennis's struggles to invent a new pointer semantics, to make arrays work, and to come to terms with this newfangled struct idea. Notions of type safety and distinguishing Booleans from integers did not seem important until much later :-)

like image 187
Norman Ramsey Avatar answered Oct 14 '22 20:10

Norman Ramsey