如果我排除库和编译器扩展,C中还剩下什么?(What remains in C if I exclude libraries and compiler extensions?)

编程入门 行业动态 更新时间:2024-10-26 00:18:16
如果我排除库和编译器扩展,C中还剩下什么?(What remains in C if I exclude libraries and compiler extensions?)

想象一下,您不能或不想将编译器提供的任何库用作“标准”,也不想使用任何外部库。 您甚至不能使用编译器扩展(例如gcc扩展 )。

如果你删除C语言中很多人当然使用的所有东西,剩下的部分是什么?

以这种方式,可能任何大型C编译器(不仅仅是ANSI C)开箱即用的每个可调用函数的列表都会令人满意,因为它至少会大致显示该语言的用例。

首先我想到了sizeof()和printf() (这些已经在评论中明确了 - operator + stdio ),所以...还剩下什么? 在线组装看起来也像是一个扩展,所以如果我是对的话,甚至可以选择使用 C组装。

可能在代码方面,它更容易理解。 想象一下,只使用例如gcc main.c (允许输出标志)编译的代码没有#include ,也没有extern 。

int main() { // replace_me return 0; }

除了“无聊”类型的数学和从类型到类型的转换之外,我还能做什么呢?

请注意, switch , goto , if ,loops和其他什么都不做,只允许重复一段代码的构造不是我想要的东西(如果不是很明显)。


(希望编辑澄清wtf我实际上是在询问,但Matteo的回答几乎是这样做的。)

Imagine a situation where you can't or don't want to use any of the libraries provided by the compiler as "standard", nor any external library. You can't use even the compiler extensions (such as gcc extensions).

What is the remaining part you get if you strip C language of all the things a lot of people use as a matter of course?

In such a way, probably a list of every callable function supported by any big C compiler (not only ANSI C) out-of-box would be satisfying as as answer as it'd at least approximately show the use-case of the language.

First I thought about sizeof() and printf() (those were already clarified in the comments - operator + stdio), so... what remains? In-line assembly seem like an extension too, so that pretty much strips even the option to use assembly with C if I'm right.

Probably in the matter of code it'd be easier to understand. Imagine a code compiled with only e.g. gcc main.c (output flag permitted) that has no #include, nor extern.

int main() { // replace_me return 0; }

What can I call to actually do something else than "boring" type math and casting from type to type?

Note that switch, goto, if, loops and other constructs that do nothing and only allow repeating a piece of code aren't the thing I'm looking for (if it isn't obvious).


(Hopefully the edit clarified wtf I'm actually asking, but Matteo's answer pretty much did it.)

最满意答案

如果删除所有库本质上你有一些类似于C的独立实现(它仍然需要提供一些库 - 比如string.h,但这不是你在便携式C中不能轻易实现的),这就是正常情况首先是编程微控制器和其他没有现成操作系统的计算机 - 以及编译操作系统时通常使用的操作系统编写者。

除了“原始”计算之外,你通常有两种做事方式:

装配块(你可以在底层机器上做任何事情); 内存映射IO(您将volatile指针设置为某些硬件相关位置并从中读取/写入;这会影响硬件内容)。

这真的是你需要构建任何东西 - 毕竟,这一切都归结为那些东西,无论如何,常规托管实现的C库通常用C语言编写,一些程序集用于速度或与操作通信系统1 (通常通过某种中断调用系统调用)。

再说一遍,你自己无法实现。 但是,拥有一个标准库的目的是避免不断重新发明轮子,并拥有一套便携式功能,使您不必重新知道每个目标平台的细节。


反过来,主流操作系统通常也用混合或C和汇编语言编写。

If you remove all libraries essentially you have something similar to a freestanding implementation of C (which still has to provide some libraries - say, string.h, but that's nothing you couldn't easily implement yourself in portable C), and that's what normally you start with when programming microcontrollers and other computers that don't have a ready-made operating system - and what operating system writers in general use when they compile their operating systems.

There you typically have two ways of doing stuff besides "raw" computation:

assembly blocks (where you can do literally anything the underlying machine can do); memory mapped IO (you set a volatile pointer to some hardware dependent location and read/write from it; that affects hardware stuff).

That's really all you need to build anything - and after all, it all boils down to that stuff anyway, the C library of a regular hosted implementation is normally written in C itself, with some assembly used either for speed or to communicate with the operating system1 (typically the syscalls are invoked through some kind of interrupt).

Again, it's nothing you couldn't implement yourself. But the point of having a standard library is both to avoid to continuously reinvent the wheel, and to have a set of portable functions that spare you to have to rewrite everything knowing the details of each target platform.


And mainstream operating systems, in turn, are generally written in a mix or C and assembly as well.

更多推荐

本文发布于:2023-08-08 00:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   remains   exclude   extensions   compiler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!