简体   繁体   English

Windows和Linux中的printf之间的区别

[英]Difference between printf in Windows and Linux

Actually other than the core C language, there is a C library. 实际上,除了核心C语言之外,还有一个C库。 And if my understanding is right, functions like printf are part of C library. 如果我的理解是正确的,则诸如printf类的printf是C库的一部分。 Now I have programmed in C in Turbo C in Windows as well as using gcc in Linux. 现在,我已经在Windows的Turbo C和C的Linux中使用gcc进行了C语言编程。

My question is: Are the code implementations of functions like printf the same in both Windows and Linux? 我的问题是:在Windows和Linux中,像printf这样的printf的代码实现是否相同? Ultimately the printf function has to call a function in core OS (in both cases) that would display ASCII characters onto the screen? 最终, printf函数必须在核心OS中调用一种函数(在两种情况下),该函数会在屏幕上显示ASCII字符? So since both the OS are different, will the implementation of code for printf be also different in both the cases? 因此,由于两个操作系统都不同,因此在两种情况下, printf的代码实现是否也会不同?

Of course the implementation (of printf and all functions in <stdio.h> ) is different (on Linux and on Windows), but the behavior should be conforming to the specification in the C11 or C99 standard. 当然( printf<stdio.h>所有功能)的实现(在Linux和Windows上)是不同的,但是其行为应符合C11C99标准中的规范。

Notice that printf does not show characters on the screen, but send them to the standard output (see printf(3) ). 注意, printf在屏幕上不显示字符,而是将它们发送到标准输出(请参见printf(3) )。 Something else -eg the kernel tty layer and your terminal emulator on Linux- is displaying the characters on your screen (or elsewhere!). 其他内容(例如内核tty层和Linux上的终端仿真器)正在屏幕上(或其他地方!)显示字符。

On Linux and POSIX systems, <stdio.h> is ultimately using system calls to write data to a file descriptor . 在Linux和POSIX系统上, <stdio.h>最终使用系统调用将数据写入文件描述符 It would be write(2) (for printf ) and the list of system calls is available in syscalls(2) . 它将是write(2) (对于printf ),系统调用列表在syscalls(2)中可用。 Be aware that stdout is usually buffered (notably for performance reasons; making a write syscall for every written byte would be too costly). 要知道, stdout通常是缓冲(尤其是性能方面的原因,使一个write每一个字节写会过于昂贵的系统调用)。 See fflush(3) & setvbuf(3) . 参见fflush(3)setvbuf(3) Try using strace(1) on your Linux program to understand the actually used syscalls. 尝试在Linux程序上使用strace(1)来了解实际使用的syscall。

On Windows, there is some equivalent thing (except that the list of syscalls on Windows is less documented and is very different). 在Windows上,有一些等效的东西(除了Windows上的syscall列表记录较少且有很大不同)。

BTW, GNU/Linux is mostly free software . 顺便说一句,GNU / Linux主要是免费软件 So read Advanced Linux Programming then study the source code: the libc is often glibc (but could be musl-libc , etc... so you can have several implementations of printf on Linux, but usually you have one libc.so , even if you could have several ones), the kernel source is available on kernel.org . 因此,请阅读高级Linux编程,然后研究源代码: libc通常是glibc (但可以是musl-libc等),因此您可以在Linux上有几种 printf实现,但是即使您有一个 libc.so ,即使您可能有几个),内核源可以在kernel.org上找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM