Why does scanf ("% i", a) not accept binary code, for example, 0b101?
I got confused in a binary expression like 0b10101 :
#include <stdio.h> int main(void) { int a,b; b = 0b101; scanf("%i",&a); printf("the value of a is %d\n", a); printf("the value of b is %d\n", b); } when I 0b101 ,
conclusion gives me
the value of a is 0; the value of b is 5; instead of two 5, as it should be.
Is there a way to make scanf take binary input?
Standard C does not have a special prefix for annotations to represent binary notation. Only decimal, octal, and hexadecimal are supported. Although the 0b prefix was proposed for C99 (and rejected), it is therefore not uncommon to see how it is implemented in some compilers.
Even if you saw the 0b prefix supported by some specific C implementation as an extension (GCC?), It would be pretty surprising to see that it is recognized by the scanf %i format specifier, as it would break compatibility with the standard implementation. The latter should stop reading at b .
Judging by your test results, although it seems that the compiler supports 0b... integral literals, and the standard library knows nothing about them. Perhaps the documentation for this library has something to say about it, for example, about the extended specifier / flag of a non-standard format for scanf , which allows you to recognize the prefixes 0b... However, I do not see it in the GCC docs.