为什么C不是动态语言?
据我所知,在 C 中,我们可以使用函数指针,这在运行时调用(1)。
根据维基百科的定义:
动态编程语言是一个术语 广泛应用于计算机科学领域 描述一类高级 执行于的编程语言 运行时许多常见的行为 其他语言可能会执行 编译,如果有的话......
所以我的问题是,为什么 C 不是一种提供 (1) 中功能的动态语言?
As I know that in C, we can use function pointer and this is called during run-time (1).
By definition from wikipedia:
Dynamic programming language is a term
used broadly in computer science to
describe a class of high-level
programming languages that execute at
runtime many common behaviors that
other languages might perform during
compilation, if at all ...
So my question is, why C is not a dynamic language providing the feature in (1) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为你的函数指针中的函数仍然是在编译时编译的。
您无法在运行时“即时”添加新函数或修改函数。
Because the function in your function pointer is still compiled at compile time.
You cannot add a new function or modify a function "on the fly" at runtime.
多态行为不是动态语言造成的。一般来说,有人提到“动态语言”时指的是它的类型系统。 PHP 是一种动态语言,因为任何变量都可以包含任何类型的数据,并且数据类型之间的转换是隐式处理的。转换内容和时间的确定是在运行时确定的。
另外,我认为 C 不符合您引用的定义的“高级编程语言类”部分。
Polymorphic behavior does not a dynamic language make. Generally speaking, someone referring to a "dynamic language" is referring to it's type system. PHP is a dynamic language, because any variable can contain any kind of data, and conversions between data types are handled implicitly. The determination of what and when to convert is made at run time.
Also, I think C fails the "class of high level programming languages" portion of the definition you cite.
您只能将指针分配给编译时存在的函数。您无法在 C 程序中编译新函数并分配指向该函数的指针。
You can only assign a pointer to a function that exists at compile-time. You can not compile a new function in the middle of a C program and assign a pointer to that function.
由于其类型系统,语言通常被认为是动态的。 C 是静态类型的。这意味着每个变量在创建时都会分配一个类型,并且该类型无法更改。不能将 float 数据放入 int 指针*。在像 Python 这样的动态语言中,数据有类型,但变量没有。我可以将字符串分配给与分配整数相同的变量。
您引用的定义似乎也在谈论动态编程,即在运行时添加代码。在 C 中,如果不考虑汇编,就不可能在编译后添加新代码。您的程序在执行时无法决定它需要新函数。在像Scheme这样的语言中,它可以。
*转换不算数,因为您正在显式转换类型。
A language is usually considered dynamic because of its type system. C is statically typed. This means that every variable has a type assigned to it at creation and that type cannot change. You cannot put float data into an int pointer*. In a dynamic language like Python, data has a type but variables do not. I can assign a string to the same variable as I assign an integer to.
The definition you refer to also seems to talk about dynamic programming, that is adding code at runtime. In C it is impossible, without thinking in terms of assembly, to add new code after compilation time. Your program cannot decide that it needs a new function while it is executing. In a language like Scheme, it can.
*Casting doesn't count because you are explicitly converting the type.
C 不是高级语言,它是非常低级的。
它缺乏类和其他高级功能,而是提供直接位翻转和直接操作内存的能力。
通常,动态语言运行在某些 VM 之上,例如 JVM 或 CLR,被解释为 Python,或者使用可执行堆栈(即 Haskell)来提供功能。相比之下,C 直接编译为机器代码。
有很多方法可以使 C 进行自我修改,但这是一项非常困难的技术,并且不能真正称为“动态语言”。
C is not a high-level language, it is very low level.
It lacks classes and other high level features and instead offers direct bit-flipping and the ability to manipulate memory directly.
Typically, dynamic languages run on top of some VM, such as the JVM or the CLR, are interpreted, a la Python, or use an executable stack, i.e. Haskell, to provide functionality. In contrast, C is compiled directly to machine code.
There are ways to make C self-modify, but that is a very difficult technique and doesn't really qualify as a "dynamic language."