delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/25/10:56:57

From: Hans-Bernhard Broeker <broeker AT acp3bf DOT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Weird output of printf()
Date: 25 Jan 2000 09:28:12 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 40
Message-ID: <86jqbc$49u$1@nets3.rz.RWTH-Aachen.DE>
References: <388B549C DOT A2C038D AT ou DOT edu> <Pine DOT SUN DOT 3 DOT 91 DOT 1000124092228 DOT 10104F AT is> <e8ip8s8u3pqm4qv1k5m9f42t408l8f372g AT 4ax DOT com>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 948792492 4414 137.226.32.75 (25 Jan 2000 09:28:12 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 25 Jan 2000 09:28:12 GMT
User-Agent: tin/1.4-19991113 ("No Labels") (UNIX) (Linux/2.0.0 (i586))
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Jason Green <news AT jgreen4 DOT fsnet DOT co DOT uk> wrote:
> Eli Zaretskii <eliz AT is DOT elta DOT co DOT il> wrote:

>> If you want the leading zero, use "%#0x" as your format specifier.  

> This does not work (although I don't know that it should.)

> Apparent behaviour of printf is to ignore the 0 flag and the width
> field whenever the # flag is used.

Not really, I think. You're just a tid bit off in your format spec,
preventing it from doing what you want:

> If it should work then I would expect "%#02x" to be the required

Make that "%#04x", and it'll work. The '2' is too small, as it has to
count total output field width, including the "0x" added by that '#'
option in the format.

------ shell excerpt --------
E:\>type tt.c
#include <stdio.h>

int main(void) {
  printf("%#04x\n", 0x0f);
  return 0;
}

E:\>tt
0x0f
------- end ----------------

Alternatively, you could 

	printf("0x%02x\n", 0x0f);

Same result, easier to understand.
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019