www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multichar literals

reply bearophile <bearophileHUGS lycos.com> writes:
Reading a bit about the C-like language used in the LoseThos Operating System,
I have found one thing.

In this language a single quote can encompass multiple characters. 'ABC' is
equal to 0x434241.

So it's not the same thing as D Hex Strings, because 'ABC' represents an
integral value, so it's a more like a base-256 integer literal :-)

So I guess '' is zero, equals to '\0' (currently in D '' gives a "unterminated
character constant" error).

What's the purpose of them?

Bye,
bearophile
Nov 12 2010
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
bearophile wrote:
 What's the purpose of them?
The only thing I can imagine using it for is checking the magic number in a file header. Like 'ZM' for a DOS exe. But I'd prefer to just use a regular string for it... an integer would be easily wrong due to endianness issues. (In the DOS exe the header actually says 'MZ' if you read the file from left to right. The PNG magic number is '.PNG' left to right, but as an integer literal, it would be backward 'GNP.') Seems like more trouble than its worth to me, but maybe I'm missing something too.
Nov 12 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Adam D. Ruppe:

 Seems like more trouble than its worth to me, but maybe
 I'm missing something too.
The author of that little operating system seems a quite odd person (even for the flexible standards of the D newsgroups) so maybe you are missing nothing. Bye, bearophile
Nov 12 2010
prev sibling next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
It doesn't look like he says that 'ABC' is actually an integer.

If he's just expressing a character string as a little endian integer 
(which you see a lot of in e.g. debuggers), then it makes sense.

On 11/12/2010 09:02 PM, bearophile wrote:
 Reading a bit about the C-like language used in the LoseThos Operating System,
I have found one thing.

 In this language a single quote can encompass multiple characters. 'ABC' is
equal to 0x434241.

 So it's not the same thing as D Hex Strings, because 'ABC' represents an
integral value, so it's a more like a base-256 integer literal :-)

 So I guess '' is zero, equals to '\0' (currently in D '' gives a "unterminated
character constant" error).

 What's the purpose of them?

 Bye,
 bearophile
Nov 12 2010
prev sibling next sibling parent reply Rainer Deyke <rainerd eldwood.com> writes:
On 11/12/2010 20:02, bearophile wrote:
 In this language a single quote can encompass multiple characters.
 'ABC' is equal to 0x434241.
I seem to recall that this is feature is also present in C/C++, although I can't say that I've ever used it. -- Rainer Deyke - rainerd eldwood.com
Nov 12 2010
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 11/12/2010 11:28 PM, Rainer Deyke wrote:
 On 11/12/2010 20:02, bearophile wrote:
 In this language a single quote can encompass multiple characters.
 'ABC' is equal to 0x434241.
I seem to recall that this is feature is also present in C/C++, although I can't say that I've ever used it.
Oh wow. #include <stdlib.h> #include <stdio.h> void main(void){ int i = 'ABC'; printf("0x%x\n", i); } yields: 0x414243 And that's backwards from what I would have expected
Nov 12 2010
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Rainer Deyke:

 I seem to recall that this is feature is also present in C/C++, although
 I can't say that I've ever used it.
I see. The stream of C/C++ features that I discover never ends... Bye, bearophile
Nov 13 2010