Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

printf Example

Part of TutorialFundamentals

It's better to use writef these days, but here's a printf example just for the heck of it.

int main(char[][] args)
{
  printf("Hello World\n");
  return 0;
}

Here's the same thing with writef:

import std.stdio;

int main(char[][] args)
{
  writefln("Hello World");
  return 0;
}

However when using the %s formatting token with printf you must be careful to use it with the embedded length qualifier. This is because the difference between C++ and D is that C++ strings are zero-terminated character arrays referenced by an address, but in D they are a dynamic array object which is really an eight-byte structure containing length and pointer.

int main(char[][] args)
{
  printf("%.*s\n", args[0]);
 //  printf("%s\n", args[0]);  // <<-- This will fail.
  return 0;
}