conversion to binary in printf()

  • Follow


I accidentally typed %b instead of %d in a printf format string 
and got a binary representation of the number. Is that standard C 
or a compiler extension?
0
Reply dmarsh3 (44) 1/21/2007 3:21:18 PM

David Marsh wrote:
> I accidentally typed %b instead of %d in a printf format string
> and got a binary representation of the number. Is that standard C
> or a compiler extension?

%b is not a Standard conversion specifier.

Robert Gamble

0
Reply rgamble99 (794) 1/21/2007 3:27:50 PM


David Marsh a �crit :
> I accidentally typed %b instead of %d in a printf format string and got 
> a binary representation of the number. Is that standard C or a compiler 
> extension?

It is used in the lcc-win32 compiler system for binary representation

Maybe other printfs do that too.
0
Reply jacob (2538) 1/21/2007 4:29:52 PM

David Marsh <dmarsh@mail.com> writes:
> I accidentally typed %b instead of %d in a printf format string and
> got a binary representation of the number. Is that standard C or a
> compiler extension?

It's an extension.  It's not likely to be a *compiler* extension,
though; printf is part of the runtime library.  This is an important
distinction; gcc, for example, uses whatever runtime library is
available on the system.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
Reply kst-u (21469) 1/21/2007 5:09:58 PM

David Marsh wrote:
> I accidentally typed %b instead of %d in a printf format string
> and got a binary representation of the number. Is that standard C
> or a compiler extension?

The standard doesn't define this conversion specifier for printf(), so
it's a local extension.

0
Reply santosh.k83 (3969) 1/21/2007 5:55:55 PM

jacob navia wrote:
> David Marsh a �crit :
>
>> I accidentally typed %b instead of %d in a printf format string
>> and got a binary representation of the number. Is that standard
>> C or a compiler extension?
> 
> It is used in the lcc-win32 compiler system for binary
> representation

Unless you have means to disable it, that is another variation from
the standard and a hindrance to creating portable code.

-- 
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

 "A man who is right every time is not likely to do very much."
                           -- Francis Crick, co-discover of DNA
 "There is nothing more amazing than stupidity in action."
                                             -- Thomas Matthews


0
Reply cbfalconer (19183) 1/21/2007 11:37:44 PM

CBFalconer wrote, On 21/01/07 23:37:
> jacob navia wrote:
>> David Marsh a �crit :
>>
>>> I accidentally typed %b instead of %d in a printf format string
>>> and got a binary representation of the number. Is that standard
>>> C or a compiler extension?
>> It is used in the lcc-win32 compiler system for binary
>> representation
> 
> Unless you have means to disable it, that is another variation from
> the standard and a hindrance to creating portable code.

Be fair Chuck, it does not break and standard code so it does not get in 
the way. Although I would not use it myself even if it was available in 
a compiler I use.
-- 
Flash Gordon
0
Reply spam331 (4024) 1/22/2007 12:09:18 AM

In article <45B3F948.6CD22E60@yahoo.com>,
CBFalconer  <cbfalconer@maineline.net> wrote:
....
> "There is nothing more amazing than stupidity in action."
>                                             -- Thomas Matthews

Indeed.  And this is why I wouldn't *dream* of killfiling you.

0
Reply gazelle2 (1306) 1/22/2007 12:44:54 AM

CBFalconer wrote:
> jacob navia wrote:
>> David Marsh a �crit :
>>
>>> I accidentally typed %b instead of %d in a printf format string
>>> and got a binary representation of the number. Is that standard
>>> C or a compiler extension?
>> It is used in the lcc-win32 compiler system for binary
>> representation
> 
> Unless you have means to disable it, that is another variation from
> the standard and a hindrance to creating portable code.

Standard says it's UB. Printing a binary representation is not much
worse than nasal daemons, is it?

Yevgen
0
Reply muntyan.removethis (320) 1/22/2007 1:10:51 AM

CBFalconer <cbfalconer@yahoo.com> writes:
> jacob navia wrote:
> > David Marsh a �crit :
> >
> >> I accidentally typed %b instead of %d in a printf format string
> >> and got a binary representation of the number. Is that standard
> >> C or a compiler extension?
> > 
> > It is used in the lcc-win32 compiler system for binary
> > representation
> 
> Unless you have means to disable it, that is another variation from
> the standard and a hindrance to creating portable code.

C99 7.19.6.1p9:

    If a conversion specification is invalid, the behavior is
    undefined.

An implementation may define the behavior for things that the standard
says are undefined behavior (many valid extensions are implemented
this way).

It might be nice to be able to turn this extension off and have "%b"
cause a runtime failure (and/or a compile-time warning if the format
is a string literal), to detect programs that depend on it, but the
standard doesn't require it.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
Reply kst-u (21469) 1/22/2007 1:29:21 AM

Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
> > jacob navia wrote:
> > > David Marsh a =E9crit :
> > >
> > >> I accidentally typed %b instead of %d in a printf format string
> > >> and got a binary representation of the number. Is that standard
> > >> C or a compiler extension?
> > >
> > > It is used in the lcc-win32 compiler system for binary
> > > representation
> >
> > Unless you have means to disable it, that is another variation from
> > the standard and a hindrance to creating portable code.
>
> C99 7.19.6.1p9:
>
>     If a conversion specification is invalid, the behavior is
>     undefined.
>
> An implementation may define the behavior for things that the standard
> says are undefined behavior (many valid extensions are implemented
> this way).

Yes, however lowercase letters are reserved for future use as
conversion
specifiers (7.26.9). So, an implementation should choose (say) B
instead
of b to support binary output. Otherwise, the implementation is
potentially
allowing (and encouraging) a practice that may one day conflict with
the
C standard.

[Jacob has been told this in the past (in csc) in relation to
supporting
non-standard conversion specifiers.]

--=20
Peter

0
Reply airia (1802) 1/22/2007 1:47:58 AM

On Sun, 21 Jan 2007 17:29:52 +0100, jacob navia
<jacob@jacob.remcomp.fr> wrote:

>David Marsh a �crit :
>> I accidentally typed %b instead of %d in a printf format string and got 
>> a binary representation of the number. Is that standard C or a compiler 
>> extension?
>
>It is used in the lcc-win32 compiler system for binary representation
>
>Maybe other printfs do that too.

OpenWatcom does it too.

Jim
0
Reply sorry6384 (35) 1/22/2007 2:42:59 AM

Flash Gordon wrote:
> CBFalconer wrote, On 21/01/07 23:37:
>> jacob navia wrote:
>>> David Marsh a �crit :
>>>
>>>> I accidentally typed %b instead of %d in a printf format string
>>>> and got a binary representation of the number. Is that standard
>>>> C or a compiler extension?
>>> It is used in the lcc-win32 compiler system for binary
>>> representation
>>
>> Unless you have means to disable it, that is another variation from
>> the standard and a hindrance to creating portable code.
> 
> Be fair Chuck, it does not break and standard code so it does not
> get in the way. Although I would not use it myself even if it was
> available in a compiler I use.

The point is that having such non-standardisms encourages the
unaware to use them, with no indication that they are building
non-portable code.  Around here I run gcc through an alias that
maximizes my protection.  If I need something else (unusual) I have
to think about it.  Similarly my Pascal compiler is set to reject
anything outside ISO 7185.

The general reaction from the newbie is: "great, I can use this"
and he never realizes the penalties until much too late.  A
non-standard specifier in a format string is especially likely to
create baffling problems later, because it is library dependant.

-- 
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

 "A man who is right every time is not likely to do very much."
                           -- Francis Crick, co-discover of DNA
 "There is nothing more amazing than stupidity in action."
                                             -- Thomas Matthews

0
Reply cbfalconer (19183) 1/22/2007 4:15:53 AM

CBFalconer wrote:
> Flash Gordon wrote:
> > CBFalconer wrote, On 21/01/07 23:37:
> >> jacob navia wrote:
> >>> David Marsh a =C3=A9crit :
> >>>
> >>>> I accidentally typed %b instead of %d in a printf format string
> >>>> and got a binary representation of the number. Is that standard
> >>>> C or a compiler extension?
> >>> It is used in the lcc-win32 compiler system for binary
> >>> representation
> >>
> >> Unless you have means to disable it, that is another variation from
> >> the standard and a hindrance to creating portable code.
> >
> > Be fair Chuck, it does not break and standard code so it does not
> > get in the way. Although I would not use it myself even if it was
> > available in a compiler I use.
>
> The point is that having such non-standardisms encourages the
> unaware to use them, with no indication that they are building
> non-portable code.  Around here I run gcc through an alias that
> maximizes my protection.  If I need something else (unusual) I have
> to think about it.  Similarly my Pascal compiler is set to reject
> anything outside ISO 7185.
>
> The general reaction from the newbie is: "great, I can use this"
> and he never realizes the penalties until much too late.  A
> non-standard specifier in a format string is especially likely to
> create baffling problems later, because it is library dependant.

Assuming it's clearly documented as an extension (as required), the
newbie's problem is that he didn't read the documentation. That's not a
compiler problem.

0
Reply truedfx (1926) 1/22/2007 6:42:59 AM

CBFalconer wrote:
> jacob navia wrote:
> 
>>David Marsh a �crit :
>>
>>
>>>I accidentally typed %b instead of %d in a printf format string
>>>and got a binary representation of the number. Is that standard
>>>C or a compiler extension?
>>
>>It is used in the lcc-win32 compiler system for binary
>>representation
> 
> 
> Unless you have means to disable it, that is another variation from
> the standard and a hindrance to creating portable code.
> 

I suppose you could use that logic to argue against *any* 
extension. Isn't it the programmer's job to understand that it's 
non-standard and avoid it in code intended to be portable?

David
0
Reply JSmith2 (131) 1/22/2007 3:04:26 PM

John Smith wrote, On 22/01/07 15:04:
> CBFalconer wrote:
>> jacob navia wrote:
>>
>>> David Marsh a �crit :
>>>
>>>> I accidentally typed %b instead of %d in a printf format string
>>>> and got a binary representation of the number. Is that standard
>>>> C or a compiler extension?
>>>
>>> It is used in the lcc-win32 compiler system for binary
>>> representation
>>
>> Unless you have means to disable it, that is another variation from
>> the standard and a hindrance to creating portable code.
> 
> I suppose you could use that logic to argue against *any* extension. 
> Isn't it the programmer's job to understand that it's non-standard and 
> avoid it in code intended to be portable?

It is also the implementers job to make a clear distinction between what 
is standard and what is an extension. One problem is that Jacob as an 
implementer often fails to make this distinction, although in this 
particular case it could be argued that by saying "It is used in the 
lcc-win32 compiler" he *is* saying it is specific to his compiler. I 
suspect that were it not for his track record the post would have been 
less likely lead to Chuck making the comment, or at least that he might 
have phrased it differently.

Of course, it would be nice if in standards compliant mode it produced a 
diagnostic (or run time message) for it, but that is something that 
probably a lot of implementers would not bother with. After all, there 
is no *requirement* in the standard to diagnose format specifiers not in 
the standard, and in some situations it can only be detected at run time 
in any case.
-- 
Flash Gordon
0
Reply spam331 (4024) 1/22/2007 5:20:30 PM

15 Replies
33 Views

(page loaded in 0.173 seconds)

Similiar Articles:


















7/17/2012 1:10:37 AM


Reply: