473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

conversion to binary in printf()

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?
Jan 21 '07 #1
15 85654
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

Jan 21 '07 #2
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.
Jan 21 '07 #3
David Marsh <dm****@mail.comwrites:
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) ks***@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.
Jan 21 '07 #4
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.

Jan 21 '07 #5
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
Jan 22 '07 #6
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
....
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Indeed. And this is why I wouldn't *dream* of killfiling you.

Jan 22 '07 #7
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
Jan 22 '07 #8
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
Jan 22 '07 #9
CBFalconer <cb********@yahoo.comwrites:
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) ks***@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.
Jan 22 '07 #10
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
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).
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.]

--
Peter

Jan 22 '07 #11
On Sun, 21 Jan 2007 17:29:52 +0100, jacob navia
<ja***@jacob.remcomp.frwrote:
>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
Jan 22 '07 #12
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

Jan 22 '07 #13
CBFalconer wrote:
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.
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.

Jan 22 '07 #14
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
Jan 22 '07 #15
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
Jan 22 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
7
by: spike | last post by:
Im writing a program to search for a string in a binary file. And it works. The problem is: It is sooo slow! how can i make it faster? It takes 27 seconds just to search a 5 meg file. I guess it...
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
14
by: Just starting out | last post by:
I am very new to C code and I'm having a lot of trouble with a homework assignment. This program is supposed to take the amount of Euros that the user enters and convert it to US dollars. It...
3
by: Amjad | last post by:
Hi, Are there any built-in methods in VB.NET that convert one number in one format to its equivalent in another format? For example I want to convert the number 162 (decimal) to 10100010...
36
by: Debaser | last post by:
I've recently read in one of my old C books that puts() is a better function call with regard to performance than printf() in the following situation: puts("Some random text"); vs. ...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.