我见过的大多数D语言教程都用来printf
将文本输出到控制台,但这可能不对.我知道D提供了对C/C++库的直接访问,但是不应该使用D的控制台输出功能吗?将文本(格式化或其他方式)输出到控制台窗口的首选方法是什么?
在模块内std.stdio
,你会发现write
,朋友们:writeln
,writef
,和writefln
.
write
只需获取每个参数,将其转换为字符串,然后输出:
import std.stdio;
void main()
{
write(5, " <- that's five"); // prints: 5 <- that's five
}
Run Code Online (Sandbox Code Playgroud)
writef
将第一个字符串视为格式说明符(非常类似于C printf
),并使用它来格式化其余参数:
import std.stdio;
void main()
{
writef("%d %s", 5, "<- that's five"); // prints: 5 <- that's five
}
Run Code Online (Sandbox Code Playgroud)
以" ln
" 结尾的版本等同于没有它的版本,但在打印结束时也附加换行符.所有版本都是类型安全的(因此可扩展).