0

Why can't we print a number in binary like other in C programming.

Example:

int a=9;  
printf("%b",a);   

like we can print in other form:

  • octal printf("%o",a);
  • decimal printf("%d",a);
  • hexadecimal printf("%x",a);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Vit
  • 111
  • 3
  • 3
    Well, it's not C language , it's a printf implementation shortcoming. – Sourav Ghosh Dec 28 '17 at 14:06
  • 1
    @Vitthal It is a drawback of the both Standards. – Vlad from Moscow Dec 28 '17 at 14:09
  • in c++, you can use [`bitset`](http://www.cplusplus.com/reference/bitset/bitset/) and `cout`. – apalomer Dec 28 '17 at 14:10
  • @H.S. Well, that asks _'Can we'_, whereas this presupposes the answer and asks _'Why can't we?'_. So, arguably, they are distinct questions. However, as with all such questions, I'm not sure this one has a clear-cut answer that doesn't depend on a lot of fruitless conjecture about the Committee's reasoning. If I'm wrong, and there's a rationale for why the Standard doesn't include this, we'd have a good answer (and question!) – underscore_d Dec 28 '17 at 14:16
  • @underscore_d: The answer to OP's question is "because there is no format specifier for binary. That is why we can't." – Jongware Dec 28 '17 at 14:27
  • @usr2564301 Obviously, when read literally... but given the context, I infer that they really mean _'Why is there no format specifier for binary, because I think it would be useful and worth including'_. The literal interpretation doesn't justify a question because the answer is a tautology, hence why I presumed the latter, although that's only slightly better. – underscore_d Dec 28 '17 at 14:28
  • 1
    @underscore_d: so OP may want to kown the historical reasoning behind including some formats and excluding others? – Jongware Dec 28 '17 at 14:32
  • 2
    @Vitthal There's no particular reason. The original designer of the `printf` function didn't think it was important enough to include. – Steve Summit Dec 28 '17 at 14:32
  • @usr2564301 Yes, that's what I already said. Normally, it's not useful for us to speculate about what they want, if they didn't explain it clearly - but in this case, it seems quite clear that they know why they can't do it *physically* (because they showed `%b` not working) and instead want to know why the language doesn't support this 'philosophically'. – underscore_d Dec 28 '17 at 14:34
  • As a history question I find it more interesting to ask, why is hex there? It was developed on a heavily octal dominated machine. binary, hex, octal are all equally relevant and useful. Clearly it was a personal or community decision to include some and not the other. They could have just as easily provided a way to choose any base %3x could be base 3, %17x could have been base 17. Why didnt they do that? Why? questions dont have much value at stackoverflow. – old_timer Feb 25 '18 at 06:42

2 Answers2

4

This has nothing to do with C language, per se. It's just that, the C standard does not specify a format specifier for printf() and family to produce a binary representation output, by default.

You can always roll out your own function to get the job done. There are some versions of C library which chose to provide a format specifier (and an integer suffix also) to denote binary representation but once again, that is neither mandated not regulated by the official standard.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    This statement "This has nothing to do with C language, per se" is wrong. The C Standard describes also standard functions. – Vlad from Moscow Dec 28 '17 at 14:10
  • Well, if its the C Standard not specifying this particular conversion specifier, how is this not related to the C language? – alk Dec 28 '17 at 14:10
  • 1
    As per the question title, "why we can't print the value in binary form like decimal, octal, hexadecimal form?"...well, we can. that was the context. – Sourav Ghosh Dec 28 '17 at 14:11
  • If you propose any alternate wording, I'll welcome, you know that. :) – Sourav Ghosh Dec 28 '17 at 14:12
  • 1
    @Vlad: it is perfectly valid to add a binary format specifier to an existing implementation of `printf` and compile your own `stdlib`. Nothing in the *language* prevents that – it would still be perfect standard C. – Jongware Dec 28 '17 at 14:15
  • @usr2564301 It will be a non-standard solution. – Vlad from Moscow Dec 28 '17 at 14:18
  • 2
    @VladfromMoscow: sometimes that's what is necessary ... But OP asks a naive and ambiguous question! "why *can't* we..." Two possible answers are: Because Printf Does Not Do That (per `printf` Standard), or "But You Can" (because C can). – Jongware Dec 28 '17 at 14:23
  • @usr2564301: No, quite the opposite: it's explicitly forbidden (UB) to redefine any of the standard functions. If you want to change any of them, you're making a new C implementation, not writing a program in C. – R.. GitHub STOP HELPING ICE Dec 28 '17 at 14:28
  • 3
    I think they're talking about not redefining the standard function, but writing a custom library that extends what its `printf()` can do. That can achieve results whereby all standard specifiers work as expected, but `%b` is added as an extension. Of course, arguably now `%b` doesn't act like it would in a strictly conforming `printf()`, which is non-standard... – underscore_d Dec 28 '17 at 14:30
1

I'm speculating here, but: C was originally designed and implemented by professional, practicing programmers, with the intent of writing real programs in it. And, while real programs find it necessary and useful to print in decimal and hexadecimal all the time, it's not usually important to print things in binary. It takes a lot of space, and if you're a professional programmer and you have a number that you're thinking about in binary terms, e.g. a bitmask of some kind, it's traditional to print it in the more compact hexadecimal representation, then convert to binary in your head if you need to.

Certainly, printing things in binary is keenly interesting to student programmers, if for no other reason than that their instructors are always assigning it as an exercise. But of course C was never designed with beginning programmers in mind.

If (as I suggested above) it's decimal and hexadecimal that are most common and useful, you might reasonably ask, then why does printf support %o? The answer there, it has been said, is simply that octal was the traditional way of representing machine constants and other binary-ish numbers in machine language on the PDP-11, which was of course C's original platform.

See also question 20.11 in the C FAQ list.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Also, you don't need to be a C wizard genius level programmer to pull off this task They may have taken that in consideration; why burden the standard library with something relatively unused and easy to write? – Jongware Dec 28 '17 at 14:39
  • By that logic, we need not be wizards to print numbers using the other bases, either, so why did they burden the stdlib with those? It's important to provide convenience so that not everyone needs to be a wizard all the time, letting them spend time developing higher-level things. – underscore_d Dec 28 '17 at 14:46
  • Indeed. *If* it's useful to print in binary, adding support for it in `printf` itself is basically trivial. (You can't even claim it would bloat the code; you probably couldn't even claim this back in PDP-11 days when 64K was enough for real programmers.) On the other hand, individually hacking it in to `printf` after the fact isn't trivial (most programmers don't have the standard library source code handy and wouldn't know how to build it if they did), and using `printf` for decimal, octal, and hexadecimal while having to use some off-to-the-side mechanism for binary is a plain nuisance. – Steve Summit Dec 28 '17 at 15:13