括号总是被视为函数调用吗? [英] Are parentheses always considered a function call?

查看:77
本文介绍了括号总是被视为函数调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此页面: https://en.cppreference.com/w/c/language/operator_precedence

引起我注意的是,括号运算符的 only 描述是函数调用.这是否意味着x = a * (b+c)-(d*e)表达式具有两个函数调用?

What caught my attention was that the only description of the parenthesis operator was function call. Does this mean that the expression x = a * (b+c)-(d*e) has two function calls?

我搜索了 C语法 C标准,但是我找不到支持或矛盾的任何东西.

I searched in C grammar and C standard but I was unable to find anything that either supports or contradicts this.

推荐答案

括号可以用作函数调用运算符,但这并不是它们唯一的用途.如您的示例一样,它们也用于表达式分组.

Parenthesis can be used as a function call operator, but that's not the only thing they're used for. They are also used for expression grouping as in your example.

您要查找的内容在

What you're looking for is in section 6.5.1 of the C standard which discusses Primary Expressions:

语法

1

primary-expression:
  identifier
  constant
  string-literal
  ( expression )
  generic-selection

...

5 带括号的表达式是主要表达式.其类型和值与未带括号的表达式相同 表达.它是一个左值,一个函数指示符或一个空值 如果未加括号的表达式分别是 一个左值,一个函数指示符或一个空表达式.

5 A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.

如上所述,圆括号可用于对表达式进行分组.

As stated above, parenthesis can be used to group expressions.

关于Postfix表达式的第6.5.2节详细介绍了如何用作函数调用运算符:

The use as a function call operator is detailed in section 6.5.2 on Postfix Expressions:

postfix-expression:
  ...
  postfix-expression(argument-expression-list opt)
  ...

在您的表情中:

x = a * (b+c)-(d*e)

此处使用括号匹配主表达式,但不匹配后缀表达式.

The use of parenthesis here matches a Primary Expression but not a Postfix Expression.

此外,除表达式分组外,在语言语法的其他部分也使用了括号.关于选择语句的第6.8.4节在ifswitch语句的语法中使用括号:

Also, besides expression grouping, parenthesis are used in other parts of the language grammar. Section 6.8.4 regarding Selection Statements uses parenthesis in the grammar of if and switch statements:

  • 如果(表达式)声明
  • if( expression ) statement else statement
  • switch( expression ) statement
  • if (expression) statement
  • if (expression) statement else statement
  • switch (expression) statement

关于迭代语句的第6.8.5节在whilefor语句的语法中也使用括号.

And section 6.8.5 regarding Iteration Statements also use parenthesis in the grammar of while and for statements.

  • while( expression ) statement
  • 在(表达式)时执行声明
  • 表示( expression opt ; expression opt ; expression opt )声明
  • 表示(声明 expression opt ; expression opt )声明
  • while (expression) statement
  • do statement while (expression);
  • for (expressionopt; expressionopt; expressionopt) statement
  • for (declaration expressionopt; expressionopt ) statement

这篇关于括号总是被视为函数调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆