Reputation: 528
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?
Upvotes: 10
Views: 15235
Reputation: 145829
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
and1
digits, prefixed by0b
or0B
. 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
Upvotes: 23
Reputation: 213832
Regarding standards:
Regarding your compiler problems:
Conclusion:
Upvotes: 5
Reputation: 6844
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.
Upvotes: 2
Reputation: 263257
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
.)
Upvotes: 3