括号围绕常数或铸造表达式周围涉及常数

发布于 2025-01-24 06:49:36 字数 545 浏览 0 评论 0原文

我不时看到此代码:

#define X1   (13)
#define X2   ((size_t)13)

据我了解,(外)()在这里是多余的。那是对的吗?


UPD:某些软件开发指南可能需要它。

例如: Misra C:2004年,规则19.4(要求):

c宏只能扩展到支撑的初始评估器,常数,括号的表达式,类型的限定符,存储类规范符或do-while-while-while-while-while-while-while-while-while-while-while-while-while-while-while-while-while-while-while-while-per构建体。

但是,Misra C:2012有:

#define MY_NULL_2 ( void * ) 0
#define sqrt( x ) ( _BUILTIN_sqrt ( x ) )

From time to time I see this code:

#define X1   (13)
#define X2   ((size_t)13)

As I understand, the (outer) () are redundant here. Is that correct?


UPD: Some software development guidelines may require it.

For example:
MISRA C:2004, Rule 19.4 (required):

C macros shall only expand to a braced initialiser, a constant, a parenthesised expression, a type qualifier, a storage class specifier, or a do-while-zero construct.

However, MISRA C:2012 has:

#define MY_NULL_2 ( void * ) 0
#define sqrt( x ) ( _BUILTIN_sqrt ( x ) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

眼波传意 2025-01-31 06:49:36

我不认为第一个例子需要括号,但始终括号宏观的宏观是一个合理的习惯。

如果您从定义中删除外括号,然后在上下文x2 [a]中调用它,则第二个将产生意外的分组。

I don't believe the first example needs parentheses, but always parenthesizing macro bodies is a reasonable habit.

The second one will produce unexpected grouping if you remove the outer parentheses from the definition and then invoke it in the context X2[a].

何以笙箫默 2025-01-31 06:49:36

据我了解,(outer)()()在这里是多余的。这是正确的吗?

在第一种情况下,(13)可能会引起问题。参见下文。
在第二种情况下((size_t)13)倾向于防止问题,因为它可以确保铸件(没有顶部优先顺序)仅应用于13,而不是以下代码。


()#define x1(13)有缺点。考虑弦乐

作为(80),形成了错误的格式字符串。

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define BUFFER_LEN_WITH (80)
#define BUFFER_LEN_WITHOUT 80

// Could have used either BUFFER_LEN_WITH, BUFFER_LEN_WITHOUT here.
#define BUFFER_SIZE (BUFFER_LEN_WITHOUT + 1) 

int main(void) {
  char buffer[BUFFER_SIZE];
  printf("Format: <%s>\n", "%" TOSTRING( BUFFER_LEN_WITH) "s");
  printf("Format: <%s>\n", "%" TOSTRING( BUFFER_LEN_WITHOUT) "s");
  int count = scanf("%" TOSTRING( BUFFER_LEN_WITHOUT) "s", buffer);
  if (count == 1) {
    printf("<%s>\n", buffer);
  }
}

输出

Format: <%(80)s>  <-- Wrong format
Format: <%80s>
zvxcx
<zvxcx>

As I understand, the (outer) () are redundant here. Is that correct?

In the first case, (13) can cause a problem. See below.
In the the 2nd case ((size_t)13) tends to prevent problems as it insures the cast (which does not have top order of precedence) is applied to only 13 and not some following code.


The () in #define X1 (13) have a disadvantage. Consider stringification.

As (80), the incorrect format string is formed.

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define BUFFER_LEN_WITH (80)
#define BUFFER_LEN_WITHOUT 80

// Could have used either BUFFER_LEN_WITH, BUFFER_LEN_WITHOUT here.
#define BUFFER_SIZE (BUFFER_LEN_WITHOUT + 1) 

int main(void) {
  char buffer[BUFFER_SIZE];
  printf("Format: <%s>\n", "%" TOSTRING( BUFFER_LEN_WITH) "s");
  printf("Format: <%s>\n", "%" TOSTRING( BUFFER_LEN_WITHOUT) "s");
  int count = scanf("%" TOSTRING( BUFFER_LEN_WITHOUT) "s", buffer);
  if (count == 1) {
    printf("<%s>\n", buffer);
  }
}

Output

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