Text in D article

Pierre Rouleau prouleau at impathnetworks.com
Sat Nov 18 10:19:54 PST 2006


Daniel Keep wrote:

> Here's a draft of an article which, hopefully, will explain some of the
> details of how text in D works.  Any constructive criticism is welcomed,
> along with edits or corrections.
> 

As someone who has not been coding in D except for trying out some D 
every so often, I find:

- the discussion of Unicode and its support of D clear and useful
- the description of the use of printf and string confusing:

You wrote::

    Back before D had the std.stdio.writefln method, most examples used
    the old C function printf. This worked fine until you tried to output
    a string::

       printf(“Hello, World!\n”);

    The above statement was very likely to print out garbage that left
    many people scratching their heads. The reason is that C uses
    NUL-terminated strings, whereas D uses true arrays. In other words:

    - Strings in C are a pointer to the first character. A string ends at
      the first NUL character.
    - Strings in D are a pointer to the first character, followed by a
      length. There is no terminating character.

    And that's the problem: printf is looking for a terminator that
    doesn't necessarily exist.


That would lead me to believe that I could not use printf to print a 
string litteral.  But then I just wrote and compiled the following D code::

   int
   main()
   {
      printf("Hello!\n");
      printf("Bye!\n");
      return 1;
   }

But it prints just fine.  So, something must be missing in your 
explanation or my understanding.  I'll have to read more about D to 
understand.

Just my 2 cents,

--
P.R.





More information about the Digitalmars-d mailing list