如何根据 C11 使用二进制前缀?
我目前正在开始使用 C30
(基于 microchip 的 GCC
的 C
编译器,用于其 PIC24
对微控制器进行编程> 设备),出于好奇,我启用了严格 ANSI 警告
。首先,我不知道在 C11 中,像 // 这样的注释标记是“错误的”,而我应该使用 /* blah blah */,但真正令我惊讶的是对一行代码的警告。
“警告:使用非标准二进制前缀”
代码行是:
OSCCONbits.COSC = 0b000;
我在网上查看了 C11 (ISO/IEC 9899:2011) 的草案之一,并且在 C 中找不到任何有关二进制前缀的信息。 http://www.open-std.org/jtc1/sc22 /wg14/www/docs/n1570.pdf
根据 C11,C 的正确二进制表示法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C 没有二进制常量。 (即使在 C11 中,它们也不受支持。)
它们被提议作为 C99 的补充,但该提议被拒绝。
来自 C99 基本原理文档:
您说您正在使用基于 gcc 的编译器,并且 gcc 支持二进制常量:它们是 C 语言的 GNU 扩展。
有关二进制常量的更多信息,请参阅
gcc
页面:http: //gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
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:
You said you are using a compiler based
gcc
andgcc
supports binary constants: they are a GNU extension to the C language.See
gcc
page about binary constants for more information:http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
关于标准:
关于您的编译器问题:
结论:
Regarding standards:
Regarding your compiler problems:
Conclusion:
C11 没有二进制文字;它只有十进制、八进制和十六进制,如标准第 6.4.4.1 节中所述。这与 C99 相比没有变化。
6.6 第 10 段说:
如果我理解正确的话,它允许您的编译器提供的扩展类型;这与 C99 相比也没有变化。
通常的解决方法是使用十六进制文字;每个十六进制数字对应四个二进制数字。 (当然
0b000
可以简单地写成0
。)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:
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 as0
.)二进制前缀不是标准的。将它们转换为八进制 (
0
) 或十六进制 (0x
),这只是标准中定义的前缀。另外,
//
注释是在 C99 标准中引入的,它们在 C89 ANSI 标准中不存在。这就是编译器向您发出警告的原因。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.