System.out、stdout 和 cout 是完全相同的东西吗?
System.out、stdout 和 cout 在 Java、C 和 C++ 中分别是完全相同的东西吗?
为什么同一事物有三个不同的名称(特别是当 C、C++ 和 Java 有很多共同点时)?
另外,我知道它们的用途,但我的意思是,它们到底是什么?
Are System.out, stdout and cout the EXACT same thing in Java, C and C++ respectively?
Why have three different names for the same thing (especially when C, C++ and Java have much in common)?
Also, I know what they are used for but what are they exactly, under the hood, I mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它们是同一件事,但类型不同。例如,
stdout
是一个FILE*
,cout
是一个std::ostream
。由于 C++ 支持两者,因此需要不同的名称。在底层,所有这些变量都引用调用进程的标准输出。它是操作系统在生成新进程时始终打开的三个文件描述符(
stdin
、stdout
、stderr
)之一。写入此文件描述符的所有内容最终都会显示在屏幕上或stdout
重定向到的任何位置(使用>
或>>
shell 运算符)。They are the same thing, but they don't have the same type. For instance,
stdout
is aFILE*
andcout
is anstd::ostream
. Since C++ supports both, different names are necessary.Under the hood, all these variables reference the standard output of the calling process. It's one of the three file descriptors (
stdin
,stdout
,stderr
) that are always open by the OS when it spawns a new process. Everything written to this file descriptor ends up on the screen or whereverstdout
was redirected to (using the>
or>>
shell operators).它们是每种语言特定的写入程序“标准输出”文件的方式,这个概念起源于 C/UNIX。它们的不同之处在于它们为执行输出提供的确切函数/方法。
还值得一提的是,
cout
和stdout
在 C++ 中都可用,因为它半途而废地试图成为 C 语言的超集,但混合使用两者可能会除非您完全禁用两者的缓冲,否则这是一个坏主意。我不知道两者有任何共享缓冲区的要求,因此如果混合它们,输出可能会出现乱序。They are each language-specific ways for writing to the program's "standard output" file, a concept which originated with C/UNIX. They differ in the exact functions/methods they provide for performing output.
It's also worth mentioning that both
cout
andstdout
are available in C++, since it halfway tries to be a superset of the C language, but mixing the use of the two may be a bad idea unless you disable buffering entirely on both. I'm not aware of any requirement for the two to share a buffer, so it's possible that output will come out misordered if you mix them.理论上它们是同一件事,都发送到标准输出。
但在 C 和 C++ 中,cout 构建在 stdout 之上,以添加 System.out 提供的一些功能,例如格式化。由于java没有指针的概念,System,out被重新设计为使用PrintStream来完成与cout类似的任务。
PritnStream 提供了一些额外的功能,例如,PrintStream 不会抛出 IOException,而是设置一个内部错误标志,然后可以使用 checkError 访问该错误标志。
我认为之所以遵循命名约定,是因为每种语言的设计者都不同。 C、C++ 与 Unix 密切相关,因此它们使用标准输出和控制台等术语。Java 被设计为更加面向对象,因此 Java 的创建者决定将其命名为有点不同。
In theory they are the same thing, all of them sent to the standard output.
But in C and C++, cout builds on top of stdout to add some features that System.out provides like formatting. Since java has no concept of pointers, System,out was redesigned to use the PrintStream to do a similar kind of task as cout.
PritnStream provides some extra features for example, PrintStream does not throw an IOException but instead sets an internal error flag that can then be accessed using checkError.
I think the naming conventions were followed because the designers of each language were different. C, C++ is closely associated with Unix and hence they used terms like standard output and console, etc. Java was designed to be more object oriented, and thus the creator of Java decided to name it a bit different.
cout
本质上与stdout
相同,但不同之处在于cout
是ostream
类型(这本质上意味着您可以使用<<
输入格式化数据,或使用write
方法输入未格式化的数据stdout
附加到文件描述符(stdout 是一个文件描述符) 。FILE*
)。stdout
文件描述符是1
,因为它返回对文件描述符的引用,因此可以在fputs 中使用。
和fprintf
。JavaSystem.out
本质上类似于stdout
(它使用java.io.FileDescriptor
)。 > 带有句柄1
),并传递到FileOutputStream
中,最后包装在BufferedOutputStream
中,这就是
java.lang.System
的方式 。 > 初始化:FileDescriptor.out
为:源:
System.out
、cout
和stdout
cout
is essentially the same asstdout
but the difference is thatcout
is of typeostream
(which essentially means that you can enter formatted data using<<
or unformatted data with thewrite
method.stdout
is attached to a file descriptor (stdout is aFILE*
).stdout
file descriptor is1
. Because it returns a reference to a file descriptor, it can be used infputs
andfprintf
.Java
System.out
is essentially likestdout
(it usesjava.io.FileDescriptor
with handle1
) and passed intoFileOutputStream
and finally wrapped insideBufferedOutputStream
.This is how
java.lang.System
is initialized:FileDescriptor.out
is:Source:
System.out
,cout
andstdout