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: Convert from scientific notation to integer - comp.unix.shell ...> You can avoid awk, use printf(1): % printf "%.4f\n" "3.1415926E+03 ... Convert decimal number in binary number - comp.lang.vhdl ... convert Decimal to Hexa - comp.lang ... Convert to floating point input - comp.soft-sys.matlabFloating Point and printf() - comp.lang.asm.x86 DSS_hMcbsp0); // convert input data to right and left ... me out how to convert from negative fractions to binary. Convert ... awk script to convert dec to hex - comp.lang.awk... The spirit of experimentation is dead. > > The key is the printf call. The ... How to convert decimal to binary, octal and hex String in Java Program ... Can we convert a ... converting double to long - comp.lang.cI need to write a function to do this conversion: long ... That value is not exactly representable in binary ... fact*10); if(digit>=3D5) fact_p_val++; printf ... convert integer to string - comp.lang.perl.miscConvert decimal to 24-bit binary - comp.soft-sys.matlab How to convert decimal to binary ... Converting integers to Roman Numerals using switchcase and printf ... convert ... MASM to NASM conversion? - comp.lang.asm.x86Is there a free program that will automatically convert ... Binary Log Routine - comp.lang.asm.x86 MASM to NASM ... Floating Point and printf() - comp.lang.asm.x86 ... AWK to C converter - comp.lang.awkBitwise comparison of 2 numbers - comp.lang.awk... loop to convert the values to binary ... ... For more carefully formatted output, awk provides a C-like printf statement ... Convert numeric grade to letter grade - comp.databases.ms ...For one, we can use it to convert, say "NY", > "CA ... letter ... to 2**5-1, write the number in binary, then convert 0 ... A printf and rounding question - comp.lang.awk ... c99 uint32_t (etc.) corresponding portable printf format ...... but it is harder to extend a system's printf ... variables correspond to fixed width fields in binary ... track of; you don't have to remember that the conversion ... Converting number to std::string ("itoa()" ) - comp.lang.c++ ...Not to mention the usual problems of the printf family; there are ... code to convert negative fractions to binary - comp.soft-sys ... Converting number to std::string ... How to port 32 bit unsigned char* to 64 bit - comp.lang.c++ ...... error: > XXX.cpp(49) : warning C4267: '=' : conversion ... bit based) unsigned char byte; byte var[] = (...binary ... comp.lang.asm.x86 However, it works for (-)32.0 - printf ... show numeric (octal) representation of permissions - comp.unix ...... case ${perms%$rest} in r|w|x) printf "1" ;; -) printf "0 ... conversion decimal to hex,octal,binary - comp.lang.asm.x86 ... show numeric (octal) representation of ... How to get a .lib from .dll - comp.lang.asm.x86... means the functions are likely to be "strlen", "printf ... type of executable, an object-orientated binary standard ... tools only use OMF but that MS's linker > can convert it ... grep help with control chars - comp.unix.solaris... code. > I use gsar to search for arbitrary binary data ... fgrep $BEL file combine with BEL=`printf '\7'` or ... Can we convert a char to ascii in awk - comp.lang.awk grep ... from std::string to std::istream? - comp.lang.c++Can I convert a std::string to a std::istream object? ... -- -Gernot int main(int argc, char** argv) {printf ("%silto ... stringstream (binary) performance problem - comp.lang ... conversion to binary in printf() - C / C++conversion to binary in printf(). C / C++ Forums on Bytes. ... I accidentally typed %b instead of %d in a printf format string and got a binary representation of the ... c - Is there a printf converter to print in binary format? - Stack ...I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base? I am running gcc. printf("%d %x %o\n", 10, 10, 10 ... 7/17/2012 1:10:37 AM
|