writeln()或writefln()?

时间:2011-03-29 06:11:38

标签: d

Hello World for D看起来像这样:

import std.stdio;

void main(string[] args)
{
    writeln("Hello World, Reloaded");
}

来自http://www.digitalmars.com/d/

但是当我用gdc-4.4.5编译它时,我得到:

hello.d:5: Error: undefined identifier writeln, did you mean function writefln?
hello.d:5: Error: function expected before (), not __error of type _error_

这是D1 / D2吗?图书馆的东西?看来,writefln是一个stdio库函数,而writeln则不是。

2 个答案:

答案 0 :(得分:7)

是的,writeln仅适用于D2的标准库。

答案 1 :(得分:4)

正如Cyber​​Shadow所提到的,writeln仅在D2中。它们之间的区别是writeln只是按原样打印其参数,而writefln将其第一个参数解释为格式字符串,如C' printf

示例:

import std.stdio;

void main() {
    // Prints "There have been 44 U.S. presidents."  Note that %s can be used
    // to print the default string representation for any type.
    writefln("There have been %s U.S. presidents.", 44);

    // Same thing
    writeln("There have been ", 44, " U.S. presidents.");
}