We initialize octal by putting 0 as prefix and hexadecimal as 0x. How can we initialize an int variable in binary number? Is there any access specifier in C for binary number? e.g %o for octal and %x for hexadecimal number.
Asked
Active
Viewed 1.6k times
8
-
[Can I use a binary literal in C or C++?](http://stackoverflow.com/questions/2611764/can-i-use-a-binary-literal-in-c-or-c) and [Is there a printf converter to print in binary format?](http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format) and [Binary constants using the ‘0b’ prefix](http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html) for more info. – Dayal rai Sep 03 '13 at 13:21
2 Answers
12
Recent versions of GCC provide an extension to the C standard. Use 0b
or 0B
to prefix a bit series like:
int i = 0b0101010;

alk
- 69,737
- 10
- 105
- 255
-
I use gcc version 4.7.2. Both 0b and 0B works. But what is the access specifier to print binary number? – Harsh Sep 03 '13 at 13:51
-
@Harsh: As far as I know there is none. However gcc overs you the possibility to roll your own: http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html#Customizing-Printf – alk Sep 03 '13 at 13:55