Proposal: clean up semantics of array literals vs string literals

Don Clugston dac at nospam.com
Tue Oct 2 06:17:36 PDT 2012


On 02/10/12 13:18, Tobias Pankrath wrote:
> On Tuesday, 2 October 2012 at 11:10:46 UTC, Don Clugston wrote:
>> The problem
>> -----------
>>
>> String literals in D are a little bit magical; they have a trailing
>> \0. This means that is possible to write,
>>
>> printf("Hello, World!\n");
>>
>> without including a trailing \0. This is important for compatibility
>> with C. This trailing \0 is mentioned in the spec but only
>> incidentally, and generally in connection with printf.
>>
>> But the semantics are not well defined.
>>
>> printf("Hello, W" ~ "orld!\n");
>>
> If every string literal is \0-terminated, then there should be two \0 in
> the final string. I guess that's not the case and that's actually my
> preferred behaviour, but the spec should make it crystal clear in which
> situations a
> string literal gets a terminator and in which not.

The \0 is *not* part of the string, it lies after the string.
It's as if all memory is cleared, then the string literals are copied 
into it, with a gap of at least one byte between each. The 'trailing 0' 
is not part of the literal, it's the underlying cleared memory.

At least, that's how I understand it. The spec is very vague.



More information about the Digitalmars-d mailing list