From: L7 Date: 2006-08-19T03:25:03+09:00 Subject: Re: Bug in sprintf? Wes Gamble wrote: > Thanks for the comprehensive response. > > Well, I can't really argue with the C implementation, of course... > > BUT > > it feels like if I declare my intent to interpret the sprintf input as a > _decimal_ number with %d (there's a %o if I wanted octal), that is > should allow for leading zeroes. > > My intent in writing sprintf("%d", '08') is to say take the string 08 > and turn it into the number 8. > > It isn't to say here's an octal number, please convert it to decimal for > me. If I think the string is octal, I'll use %o for that. > > So in the spirit of what the user expects, I still think this smells > bad. > > Feel free to convince me otherwise. > Consider it this way: The format character (%d) is a way to know how the values collected will be displayed. The values, in this case arguments to %d, are not intrepreted (necessarily) at the same time as the format characters and therefor are seperate entities. Taking any [legal] value and passing it to %d will result in a decimal number printed out. However, the parsing aspect is entirely different. There is no format that says: 'this is how to interpret me' - other than the syntax. If you take that away you create ambiguities. If the syntax is longer how to distinguish a value, it must be explicitly tied to a format string/character. Imagine the confusion when you try to #define FORTY_TWO_HEX %x 2a #define FORTY_TWO_OCT %o 52 #define FORTY_TOW_DEC %d 42 (Since the two are now linked, you need the format characters. Or should it be %x 42, %o 42 ... you get the idea) instead of #define FORTY_TWO_HEX 0x2a #define FORTY_TWO_OCT 052 #define FORTY_TWO_DEC 42 While it may be easy/convenient to suggest allowing a format character determine a value, it breaks when not in a specific setting. > Thanks again, > Wes > > -- > Posted via http://www.ruby-forum.com/.