Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are parentheses always considered a function call?

I was looking at this page: https://en.cppreference.com/w/c/language/operator_precedence

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?

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

like image 211
klutt Avatar asked Dec 13 '22 11:12

klutt


1 Answers

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:

Syntax

1

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

...

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.

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

So in your expression:

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

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

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
  • if (expression) statement else statement
  • switch (expression) statement

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

  • while (expression) statement
  • do statement while (expression);
  • for (expressionopt; expressionopt; expressionopt) statement
  • for (declaration expressionopt; expressionopt ) statement
like image 102
dbush Avatar answered Dec 29 '22 04:12

dbush