9

I am currently starting out with programming micro controllers using C30 (A C compiler based on GCC from microchip for their PIC24 devices) and I enabled Strict ANSI warnings out of curiosity. First off, I did not know that in C11 comment markings like // are "wrong" and instead I should use /* blah blah */, but what really surprised me is this warning for a line of code.

"warning: use of non-standard binary prefix"

The line of code is:

OSCCONbits.COSC = 0b000;

I have looked online at one of the drafts of C11 (ISO/IEC 9899:2011) and can't find anything about binary prefixes in C. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

What is the correct binary notation for C according to C11?

5
  • 1
    Convert it to a hexadecimal number. As for setting something to (binary) 0000 why not just use a normal decimal 0 or hexadecimal 0x00? Jan 26, 2012 at 7:22
  • 9
    Who told you // comments are "wrong"? Jan 26, 2012 at 8:14
  • I think I may have worded my question wrong, so here are some corrections. I know that I can just use hex or decimal instead of binary, I just wanted to know what was the "right" way to use binary literals. Regarding me saying // comments are "wrong", I got that from a compiler warning, no one told me that but the compiler. Anyways, thanks for all the help guys! I got some awesome answers and I wish I could mark them all as answers but I could do that to only one sadly.
    – hak8or
    Jan 26, 2012 at 16:21
  • @hak8or: then it was not a c99 or c11 compiler. Jan 27, 2012 at 20:17
  • 2
    @JoachimPileborg: To be completely pedantic, 0 is an octal constant, not decimal(!) Feb 1, 2012 at 1:04

4 Answers 4

22

C does not have binary constants. (Even in C11 they are not supported.)

They were proposed as an addition to C99 but the proposition was rejected.

From C99 Rationale document:

A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.

You said you are using a compiler based gcc and gcc supports binary constants: they are a GNU extension to the C language.

Integer constants can be written as binary constants, consisting of a sequence of 0 and 1 digits, prefixed by 0b or 0B. This is particularly useful in environments that operate a lot on the bit-level (like microcontrollers).

See gcc page about binary constants for more information:

http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html

1
  • 9
    Bummer they felt it wasn't of sufficient utility. I use 0b all the time in code that does bit-twiddling for high-speed integer operations (having nothing to do with microcontrollers). 0b10101010 is much more readable to me than 0xAA, for example. Also a bummer they felt there wasn't precedent; Java has had 0b for a while now. Aug 9, 2015 at 3:30
4

Regarding standards:

  • ANSI C / "Strict ANSI" typically refers to the first standard version of C, standardized only in the USA 1989. Sometimes it is referred to as "C89".
  • ANSI C/89 became obsolete in 1990 when C became an international C standard, ISO/IEC 9899:1990, referred to as "C90". C89 and C90 are equivalent when it comes to technical details.
  • C90 became obsolete in 1999, when ISO C was updated. The new standard is referred to as "C99".
  • C99 became obsolete in 2011. The new standard is referred to as "C11".

Regarding your compiler problems:

  • C89/C90 does not allow // comments. They were introduced in C99. They have not been removed in C11.
  • Binary notation has never been part of any C standard.

Conclusion:

  • You are most likely compiling the code on a C90 compiler, with some non-standard extensions available.
0
3

C11 does not have binary literals; it only has decimal, octal, and hexadecimal, as described in section 6.4.4.1 of the standard. This is unchanged from C99.

6.6 paragraph 10 says:

An implementation may accept other forms of constant expressions.

which, if I understand it correctly, permits the kind of extension that your compiler provides; this is also unchanged from C99.

The usual workaround is to use hexadecimal literals; each hexadecimal digit corresponds to four binary digits. (And of course 0b000 can be written simply as 0.)

2
  • 1
    This is not a correct interpretation of C11§6.6p10. §6.6 is intended to carve out a subset of valid expressions that are guaranteed to evaluate to constants. It does not extend the parser or lexer (which would be needed to support binary literals). Dec 4, 2019 at 14:30
  • 1
    @SamElliott I'm inclined to agree. I'll have to think about this a bit more. If you're right, it implies that binary literals can be supported as an extension under C11 4p6, but a conforming compiler must still issue a diagnostic for 0b000 because it violates a syntax rule. Dec 4, 2019 at 16:35
2

Binary prefixes are not standard. convert them to octal (0) or hexadecimal (0x) instead, which are only prefixes defined in the standard.

Also, // comments were introduced in C99 standard, they're not present in C89 ANSI standard. That's why your compiler gives you a warning.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.