translated

This commit is contained in:
KayGuoWhu 2015-02-07 13:57:47 +08:00
parent e1022991da
commit 55277d746c
2 changed files with 53 additions and 54 deletions

View File

@ -1,54 +0,0 @@
translating by KayGuoWhu
How are C data types “supported directly by most computers”?
---------
#Q:
I am reading K&R's *The C Programming Language*” and came across this statement [Introduction, p. 3]:
>Because the data types and control structures provided by C are supported directly by most computers, the run-time library required to implement self-contained programs is tiny.
What does the bolded statement mean? Is there an example of a data type or a control structure that isn't supported directly by a computer?
#A:
Yes, there are data types not directly supported.
On many embedded systems, there is no hardware floating point unit. So, when you write code like this:
```C
float x = 1.0f, y = 2.0f;
return x + y;
```
It gets translated into something like this:
```C
unsigned x = 0x3f800000, y = 0x40000000;
return _float_add(x, y);
```
Then the compiler or standard library has to supply an implementation of `_float_add()`, which takes up memory on your embedded system. If you're counting bytes on a really tiny system, this can add up.
Another common example is 64-bit integers (`long long` in the C standard since 1999), which are not directly supported by 32-bit systems. Old SPARC systems didn't support integer multiplication, so multiplication had to be supplied by the runtime. There are other examples.
##Other languages
By comparison, other languages have more complicated primitives.
For example, a Lisp symbol requires a lot of runtime support, just like tables in Lua, strings in Python, arrays in Fortran, et cetera. The equivalent types in C are usually either not part of the standard library at all (no standard symbols or tables) or they are much simpler and don't require much runtime support (arrays in C are basically just pointers, nul-terminated strings are almost as simple).
##Control structures
A notable control structure missing from C is exception handling. Nonlocal exit is limited to `setjmp()` and `longjmp()`, which just save and restore certain parts of processor state. By comparison, the C++ runtime has to walk the stack and call destructors and exception handlers.
----
via:[stackoverflow](http://stackoverflow.com/questions/27977522/how-are-c-data-types-supported-directly-by-most-computers/27977605#27977605)
作者:[Dietrich Epp][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://stackoverflow.com/users/82294/dietrich-epp

View File

@ -0,0 +1,53 @@
C语言数据类型是如何被大多数计算机系统所支持
---------
#问题
在读K&R版的*The C Programming Language*一书时,我在[介绍,第3页]看到这样一条说明:
>因为C语言提供的数据类型和控制结构可以直接被大部分计算机系统所支持所以在实现自包含程序时所需要的运行库文件一般很小。
这段黑体说明了什么能找到一个例子来说明C语言中的某种数据类型或控制结构并不能被一种计算机系统所支持呢
#回答:
事实上C语言中确实有不被直接支持的数据类型。
在许多嵌入式系统中,硬件上并没有浮点运算单元。因此,如果你写出下面的代码:
```C
float x = 1.0f, y = 2.0f;
return x + y;
```
可能会被转化成下面这种形式:
```C
unsigned x = 0x3f800000, y = 0x40000000;
return _float_add(x, y);
```
然后编译器或标准库必须提供'_float_add()'的具体实现,这会占用嵌入式系统的内存空间。依此去计算代码在某个微型系统(译者注:也就是指微型嵌入式系统)的实际字节数,也会发现有所增加。
另一个常见的例子是64位整型数C语言标准中'long long'类型是1999年之后才出现的这种类型在32位系统上也不能直接使用。古董级的SPARC系统则不支持整型乘法所以在运行时必须提供乘法的实现。当然还有一些其它例子。
##其它语言
相比起来,其它编程语言有更加复杂的基本类型。
比如Lisp中的symbol需要大量的运行时实现支持就像Lua中的tables、Python中的strings、Fortran中的arrays等等。在C语言中等价的类型通常要么不属于标准库C语言没有标准symbols或tables要么更加简单而且并不需要那么多的运行时支持C语言中的array基本上就是指针以NULL结尾的字符串实现起来也很简单
##控制结构
异常处理是C语言中没有的一种控制结构。非局部的退出只有'setjmp()'和'longjmp()'两种只能提供保存和恢复某些部分的处理器状态。相比之下C++运行时环境必须先遍历函数调用栈,然后调用析构函数和异常处理函数。
----
via:[stackoverflow](http://stackoverflow.com/questions/27977522/how-are-c-data-types-supported-directly-by-most-computers/27977605#27977605)
作者:[Dietrich Epp][a]
译者:[KayGuoWhu](https://github.com/KayGuoWhu)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://stackoverflow.com/users/82294/dietrich-epp