473,610 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 85731
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.co mwrites:
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_Keit h) 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********@mai neline.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
representati on

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
representati on

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********@yah oo.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_Keit h) 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

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

Similar topics

103
48580
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 says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
7
2604
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 has something to do with the strequal() function... Btw, thanks to all of you who answered last time! code: ------------------------------------------------------------------------- #include <stdio.h>
9
2274
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 memory, but it should certainly handle 64-bit cpus :) #include <stdio.h> #include <stdlib.h> char * to_binary (unsigned long value) {
6
7562
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 binary data also I cant use memmem. Is there any other available function to do this. Regards Tarun
14
12732
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 runs fine if the user enters a number, but if the user enters a letter it loops. I have been working on this for 3 hours now, trying different things
3
17860
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 (binary) or to A2 (hexadecimal) and vice versa. Thanks.
36
35458
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. printf("Some random text\n");
7
19208
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
11
2693
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> #include <string.h> #define LENGTH 20 int temp, m, n, i, r, base10, true;
0
8146
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8096
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8591
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8411
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7031
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4104
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1691
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.