2

I know that at least some implementations of printf() (or sprintf() etc.) support a %b format specifier, which formats integral numbers using binary digits. But - this does not seem to be part of the language standard (according to cppreference).

So, which platforms / C-standard-library implementations support "%b"? And - can I test for this support?

I should mention libc6 on (fairly recent) Linux machines doesn't support %b; and that's what you get with gcc and clang on Linux machines. But maybe on other Unix'es, or Windows, or embedded libc's of some kind etc.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • obLink: [Is there a printf converter to print in binary format?](https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format) – Steve Summit Jun 29 '21 at 21:44
  • Formatting for binary output is a C++ feature, not a C feature. You can test it by trying `snprintf()` with a known (non-zero) input and test the return value (might be `-1` to indicate an error) and the formatted string. You should do that at compile time so you can use a sensible fallback method to format binary numbers (or you could simply write and use a method to format binary values). – Jonathan Leffler Jun 30 '21 at 02:24
  • @jonathan: When did the C++ standard diverge from the C specification for printf? Or are you thinking of a specific C++ standard library which implements that feature? It's a go-lang feature. The glibc documentation has an example of how to use glibc's (deprecated) format conversion extension which implements a `%b` conversion, but it doesn't output in binary. – rici Jun 30 '21 at 05:08
  • I've not formally verified that C++ `printf()` supports `%b`, but C++ does support `0b0010'1101` as a valid notation for a binary number (equivalent to hex `0x2D`), including the punctuation (which is optional). – Jonathan Leffler Jun 30 '21 at 06:25
  • 1
    Apparently you tried some major compilers already (please [edit] your question to tell us which), so what do you expect as an answer to "_which platforms_"? Do you want a list of more or less known compilers or libraries? -- At what time do you want to test for the support, compile, link, run time? – the busybee Jun 30 '21 at 07:29
  • @thebusybee: Actually, I misspoke in that comment. Compilers don't support or not-support this - C libraries do (and compilers may or may not use their own bundkled C library). So, I'll rephrase that comment and my answer. – einpoklum Jun 30 '21 at 08:23
  • @JonathanLeffler: 1. I asked about C. 2. I know that, but - you can't "formally verify" that it's supported, because AFAIK it's not. 3. On Linux, libc6 doesn't support `%b` (and that's the library you get with clang and gcc). – einpoklum Jun 30 '21 at 08:29
  • einpoklum, Note: there are standard [alternatives](https://stackoverflow.com/a/34641674/2410359) to using `"%b"`. – chux - Reinstate Monica Jun 30 '21 at 11:28
  • 1
    @chux-ReinstateMonica: Noted. – einpoklum Jun 30 '21 at 11:59

0 Answers0