[英]How do I use a binary prefix in accordance with C11?
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. 我目前正在开始使用C30
编程微控制器(基于GCC
C
编译器,用于他们的PIC24
器件的微芯片),我出于好奇而启用了Strict ANSI warnings
。 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. 首先,我不知道在C11评论标记如//是“错误的”而我应该使用/ * blah blah * /,但真正令我感到惊讶的是这一行代码警告。
"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 我已经在线查看了C11(ISO / IEC 9899:2011)的草案之一,并且在C中找不到关于二进制前缀的任何内容.http://www.open-std.org/jtc1/sc22/wg14/www /docs/n1570.pdf
What is the correct binary notation for C according to C11? 根据C11,C的正确二进制表示法是什么?
C does not have binary constants. C没有二进制常量。 (Even in C11 they are not supported.) (即使在C11中也不支持它们。)
They were proposed as an addition to C99 but the proposition was rejected. 他们被提议作为C99的补充,但该提议遭到拒绝。
From C99 Rationale document: 来自C99理由文件:
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. 你说你使用的是基于gcc
的编译器,而gcc
支持二进制常量:它们是C语言的GNU扩展。
Integer constants can be written as binary constants, consisting of a sequence of
0
and1
digits, prefixed by0b
or0B
. 整数常量可以写成二进制常量,由0
和1
位的序列组成,前缀为0b
或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: 有关更多信息,请参阅有关二进制常量的gcc
页面:
http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
C11 does not have binary literals; C11没有二进制文字; it only has decimal, octal, and hexadecimal, as described in section 6.4.4.1 of the standard. 它只有十进制,八进制和十六进制,如标准6.4.4.1节所述。 This is unchanged from C99. 这与C99相同。
6.6 paragraph 10 says: 6.6第10段说:
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. 这与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
.) (当然0b000
可以写成0
)
Regarding standards: 关于标准:
Regarding your compiler problems: 关于编译问题:
Conclusion: 结论:
Binary prefixes are not standard. 二进制前缀不是标准的。 convert them to octal ( 0
) or hexadecimal ( 0x
) instead, which are only prefixes defined in the standard. 将它们转换为八进制( 0
)或十六进制( 0x
),它们只是标准中定义的前缀。
Also, //
comments were introduced in C99 standard, they're not present in C89 ANSI standard. 此外, //
注释是在C99标准中引入的,它们不存在于C89 ANSI标准中。 That's why your compiler gives you a warning. 这就是你的编译器给你一个警告的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.