# Thread: assiging a binary in C

1. ## assiging a binary in C

im trying to figure out how to do it in binary
int foo = b'0011011;

or something whats teh syntax for it

2. You cannot do this in C like you can with decimal, hex, or octal.

3. Originally Posted by kermit
You cannot do this in C like you can with decimal, hex, or octal.

Quzah.

4. Originally Posted by quzah
A very good reminder. I had completely forgot about that function of strtol (Ironically, I just posted a link on another thread regarding the use of strtol to get integer input). What I had been thinking was something along these lines:

Code:
```#include <stdio.h>

int main(void)
{
int x = 0xdd;
int y = 023;
int z = b01001;  /* oops - no such beast! */

printf("x = %d\ny = %d\nz = %d\n", x, y, z);

return 0;
}```
This was something along the lines of what the OP was after I think.

5. Yeah, I had to go look. I couldn't remember if you could do base 2 or not. Apparently anything from 2 to 36 is valid.

Quzah.

6. Is this what you're trying to get at
Code:
`int foo = 0x1b;  /* stored inside the machine as binary 0011011 */`

7. It always seemed kind of strange to me why not offer a direct syntax in C to do what the OP posted

8. That and no %b in printf() for printing in binary numbers. I guess the designers of C would be able to read hex numbers as if they were binary, and convert between them in moments.

9. Originally Posted by dwks
That and no %b in printf() for printing in binary numbers. I guess the designers of C would be able to read hex numbers as if they were binary, and convert between them in moments.
That's what I have to do in those situations.

But it is still strange to me that C didn't include a way to write a binary number.

10. gcc has an extension for binary numbers with a 0b prefix. I haven't tried to use it, though. (And of course printf extensions are up to the library people, etc etc etc. Don't know of any binary printers, but there might be some out there.)

11. Yep! it would be nice to switch the input base kinda like "bc", which is written in C and uses C-style syntax.