www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Behaviour of %08X format specifiers on addresses

reply Graham <grahamc001uk yahoo.co.uk> writes:
Being used to printing addresses in "C" as 8 hex digits I though the behavior
of this code a little unexpected:

import std.stdio;

void main(char[][] args) {

	char*		p;
	int		n;
	
	p = cast(char*) 1;
	writefln("p = %08X", p);
	n = 1;
	writefln("n = %08X", n);

	p = cast(char*) 0x12;
	writefln("p = %08X", p);
	n = 0x12;
	writefln("n = %08X", n);

	p = cast(char*) 0x1234;
	writefln("p = %08X", p);
	n = 0x1234;
	writefln("n = %08X", n);

	p = cast(char*) 0x123456;
	writefln("p = %08X", p);
	n = 0x123456;
	writefln("n = %08X", n);

	p = cast(char*) 0x12345678;
	writefln("p = %08X", p);
	n = 0x12345678;
	writefln("n = %08X", n);
}

which displays:
p =     0001
n = 00000001
p =     0012
n = 00000012
p =     1234
n = 00001234
p =   123456
n = 00123456
p = 12345678
n = 12345678

I presume this is intentional (not always displaying the leading zeros on
addresses).

Is it documented anywhere that this is how it should behave ?

I am running the v1.015 compiler on a Windows platform.
Sep 22 2007
parent reply Graham <grahamc001uk yahoo.co.uk> writes:
I realize that the documentation says that the argument should be an integer
type when using %X etc. format specifiers, so it works as I expected with

writefln("p = %08X", cast(int)p);

etc. 

But even so accepting a non-integer type and then supposing small values are
only 16 bits seems a bit odd.
Sep 22 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Graham" <grahamc001uk yahoo.co.uk> wrote in message 
news:fd32ak$255t$1 digitalmars.com...
I realize that the documentation says that the argument should be an 
integer type when using %X etc. format specifiers, so it works as I 
expected with

 writefln("p = %08X", cast(int)p);

 etc.

 But even so accepting a non-integer type and then supposing small values 
 are only 16 bits seems a bit odd.
Try using writefln("p = %08p", p); It might even just be "%p".
Sep 22 2007