gpt4 book ai didi

c - 为什么 C 在 C99 之前没有 bool 数据类型?

转载 作者:太空狗 更新时间:2023-10-29 16:20:12 24 4
gpt4 key购买 nike

我知道你可以#define 一些整数,但为什么在 C99 之前 C 没有专用的 bool 数据类型?

这在编程和逻辑中很常见,我不明白缺少显式类型和符号。

最佳答案

如果你在图书馆呆了一点时间,你就不必猜测了。以下是摘自 Dennis Ritchie's paper on the evolution of C 的一些陈述.上下文是 Dennis 正在构建 Ken Thompson 的语言 B,该语言是在非常小的 PDP-7(一种字寻址机器)上实现的。由于越来越感兴趣,该小组得到了最早的 PDP-11 之一。丹尼斯写道,

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.

(强调我的。)

本文接着描述了 Dennis 为发明新的指针语义、使数组工作以及接受这种新奇的 struct 想法所做的努力。直到很久以后,类型安全和区分 bool 值和整数的概念才显得重要 :-)

关于c - 为什么 C 在 C99 之前没有 bool 数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2426000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com