简体   繁体   English

如何根据C11使用二进制前缀?

[英]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 and 1 digits, prefixed by 0b or 0B . 整数常量可以写成二进制常量,由01位的序列组成,前缀为0b0B 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: 关于标准:

  • 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 /“严格ANSI”通常是指C的第一个标准版本,仅在1989年的美国标准化。有时它被称为“C89”。
  • ANSI C/89 became obsolete in 1990 when C became an international C standard, ISO/IEC 9899:1990, referred to as "C90". 当C成为国际C标准ISO / IEC 9899:1990,被称为“C90”时,ANSI C / 89在1990年变得过时。 C89 and C90 are equivalent when it comes to technical details. C89和C90在技术细节方面是等效的。
  • C90 became obsolete in 1999, when ISO C was updated. 1999年,当ISO C更新时,C90已经过时了。 The new standard is referred to as "C99". 新标准称为“C99”。
  • C99 became obsolete in 2011. The new standard is referred to as "C11". C99在2011年已经过时。新标准被称为“C11”。

Regarding your compiler problems: 关于编译问题:

  • C89/C90 does not allow // comments. C89 / C90不允许//评论。 They were introduced in C99. 它们是在C99中引入的。 They have not been removed in C11. 它们尚未在C11中删除。
  • Binary notation has never been part of any C standard. 二进制表示法从未成为任何C标准的一部分。

Conclusion: 结论:

  • You are most likely compiling the code on a C90 compiler, with some non-standard extensions available. 您最有可能在C90编译器上编译代码,并提供一些非标准扩展。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM