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

发布于 2024-12-29 02:38:13 字数 571 浏览 3 评论 0原文

我目前正在开始使用 C30(基于 microchip 的 GCCC 编译器,用于其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘落散花 2025-01-05 02:38:13

C 没有二进制常量。 (即使在 C11 中,它们也不受支持。)

它们被提议作为 C99 的补充,但该提议被拒绝。

来自 C99 基本原理文档:

由于缺乏先例且实用性不足,添加二进制常量的提案被拒绝。

您说您正在使用基于 gcc 的编译器,并且 gcc 支持二进制常量:它们是 C 语言的 GNU 扩展。

整数常量可以写为二进制常量,由 01 数字序列组成,前缀为 0b0B。这在大量位级操作的环境(如微控制器)中特别有用。

有关二进制常量的更多信息,请参阅 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:

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

凉城已无爱 2025-01-05 02:38:13

关于标准:

  • ANSI C /“严格 ANSI”通常指 C 的第一个标准版本,仅于 1989 年在美国标准化。有时也称为“C89”。
  • ANSI C/89 于 1990 年被废弃,当时 C 成为国际 C 标准 ISO/IEC 9899:1990,简称“C90”。 C89和C90在技术细节上是相当的。
  • 1999 年 ISO C 更新后,C90 已过时。新标准简称“C99”。
  • C99 于 2011 年过时。新标准称为“C11”。

关于您的编译器问题:

  • C89/C90 不允许 // 注释。它们是在 C99 中引入的。它们在 C11 中并未被删除。
  • 二进制表示法从未成为任何 C 标准的一部分。

结论:

  • 您很可能在 C90 编译器上编译代码,并提供一些非标准扩展。

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.
童话 2025-01-05 02:38:13

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:

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.)

冧九 2025-01-05 02:38:13

二进制前缀不是标准的。将它们转换为八进制 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文