【问题标题】:writeln() or writefln()?writeln() 还是 writefln()?
【发布时间】:2011-03-29 06:11:37
【问题描述】:

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 不是,这似乎很奇怪。

【问题讨论】:

    标签: d


    【解决方案1】:

    是的,writeln 仅在 D2 的标准库中可用。

    【讨论】:

    • 而且,据我所知,gdc 目前只支持 D1——这就解释了为什么这是一个问题。
    • 为什么你认为它只支持D1?
    • @Jonathan:我的错。我正在查看较旧的 gdc 网站,该网站自 2007 年以来就没有更新过。
    【解决方案2】:

    正如 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.");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多