Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!dayton!viper!john From: john@viper.UUCP Newsgroups: comp.lang.c Subject: Re: Printf format strings Message-ID: <789@viper.UUCP> Date: Tue, 7-Apr-87 16:27:34 EST Article-I.D.: viper.789 Posted: Tue Apr 7 16:27:34 1987 Date-Received: Sat, 11-Apr-87 00:38:40 EST References: <782@kodak.UUCP> Reply-To: john@viper.UUCP (John Stanley) Distribution: world Organization: DynaSoft Systems Lines: 39 Keywords: printf, print formatting. In article <782@kodak.UUCP> grodberg@kodak.UUCP (Jeremy Grodberg) writes: > > In our Sun C compiler, printf("%#07x", 27); prints "0x000001b" (9 characters) >while printf("%#7x", 27); prints " 0x1b" (7 characters). Is this a) common, >b) "correct", c) discussed in the ANSI standard? My feeling is that the two >format strings should always generate 7 characters (unless more are necessary >to represent the number), and losing this battle I would at least hope they >would both generate 9 characters (either the "0x" is part of the field width >or it isn't). No printf() function I've used in the last 5 years supports the "#" designator. Since, by your example, I'd guess it's "suppost" to close-prefix the number by the "0x" designator, my primary suggestion would be that unless you find this to be in the ANSI standard, that you stop using it in any code you expect to be able to port anyplace. You can take a look at K&R page 145 to see that this is Not included in the "K&R standard" and thus, is probably not avaiable on a large number of machines. I have no idea if "#" is supported by the ANSI standard. If it is, then the standard is the place to look (I have no idea which way they'd define it...) If not, you'll need to use a different method of getting a right justified hex constant with-close-preceding "0x" if you expect to be able to port it to anyplace... If I had to, I'd probably use: static char cphbuf[20] = "0x"; ..... sprintf(&cphbuf[2], "%x", 27); printf("%7s", cphbuf); ..... which should work the same way on any system.... --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john