gpt4 book ai didi

c - 为什么 C 允许隐式函数和无类型变量声明?

转载 作者:太空狗 更新时间:2023-10-29 16:34:55 27 4
gpt4 key购买 nike

为什么一种语言允许函数和无类型变量的隐式声明是明智的?我知道 C 很旧,但允许省略声明并默认为 int() (或 int 在变量的情况下)对我来说似乎不太理智,即使那时。

那么,最初为什么要引入它?它真的有用吗?它实际上(仍然)被使用了吗?

注意:我知道现代编译器会给您警告(取决于您传递给它们的标志),您可以抑制此功能。这不是问题!


例子:

int main() {
static bar = 7; // defaults to "int bar"
return foo(bar); // defaults to a "int foo()"
}

int foo(int i) {
return i;
}

最佳答案

参见 Dennis Ritchie 的“C 语言的发展”:http://web.archive.org/web/20080902003601/http://cm.bell-labs.com/who/dmr/chist.html

例如,

In contrast to the pervasive syntax variation that occurred during thecreation of B, the core semantic content of BCPL—its type structureand expression evaluation rules—remained intact. Both languages aretypeless, or rather have a single data type, the 'word', or 'cell', afixed-length bit pattern. Memory in these languages consists of alinear array of such cells, and the meaning of the contents of a celldepends on the operation applied. The + operator, for example, simplyadds its operands using the machine's integer add instruction, and theother arithmetic operations are equally unconscious of the actualmeaning of their operands. Because memory is a linear array, it ispossible to interpret the value in a cell as an index in this array,and BCPL supplies an operator for this purpose. In the originallanguage it was spelled rv, and later !, while B uses the unary *.Thus, if p is a cell containing the index of (or address of, orpointer to) another cell, *p refers to the contents of the pointed-tocell, either as a value in an expression or as the target of anassignment.

这种无类型一直存在于 C 语言中,直到作者开始将其移植到具有不同字长的机器上:

The language changes during this period, especially around 1977, were largely focused on considerations of portability and type safety,in an effort to cope with the problems we foresaw and observed inmoving a considerable body of code to the new Interdata platform. C atthat time still manifested strong signs of its typeless origins.Pointers, for example, were barely distinguished from integral memoryindices in early language manuals or extant code; the similarity ofthe arithmetic properties of character pointers and unsigned integersmade it hard to resist the temptation to identify them. The unsignedtypes were added to make unsigned arithmetic available withoutconfusing it with pointer manipulation. Similarly, the early languagecondoned assignments between integers and pointers, but this practicebegan to be discouraged; a notation for type conversions (called`casts' from the example of Algol 68) was invented to specify typeconversions more explicitly. Beguiled by the example of PL/I, early Cdid not tie structure pointers firmly to the structures they pointedto, and permitted programmers to write pointer->member almost withoutregard to the type of pointer; such an expression was takenuncritically as a reference to a region of memory designated by thepointer, while the member name specified only an offset and a type.

编程语言随着编程实践的变化而发展。在现代 C 和现代编程环境中,许多程序员从未编写过汇编语言,整数和指针可以互换的概念似乎几乎是深不可测和不合理的。

关于c - 为什么 C 允许隐式函数和无类型变量声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11835001/

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