Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are round parentheses used for grouping considered an operator?

In C the round brackets are used to make more things like function call and type cast.

However they are also used to group many sub-expressions to change order-evalutation of operators.

My doubt is: when used as grouping operators are they considered as operator?

Many say they are, many say no...

like image 896
xdevel2000 Avatar asked Jun 16 '14 07:06

xdevel2000


3 Answers

6.5 Expressions chapter (N1570) paragraph 3

The grouping of operators and operands is indicated by the syntax. 85)

has following note:

85) The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of the major subclauses of this subclause, highest precedence first. Thus, for example, the expressions allowed as the operands of the binary + operator (6.5.6) are those expressions defined in 6.5.1 through 6.5.6. The exceptions are cast expressions (6.5.4) as operands of unary operators (6.5.3), and an operand contained between any of the following pairs of operators: grouping parentheses () (6.5.1), subscripting brackets [] (6.5.2.1), function-call parentheses () (6.5.2.2), and the conditional operator ?: (6.5.15).

So it would seem that C standard considers them as operators.

like image 196
user694733 Avatar answered Nov 16 '22 17:11

user694733


Grouping parentheses are not an operator proper, they are more like a meta-operator - something that influence the results of applying other operators, without being an operator itself. In a similar sense, curly braces are not statements by themselves, yet they influence composition of other statements.

One reason to consider round parentheses a meta-operator is that by the time an expression is parsed, grouping parentheses are usually gone* from the expression tree produced by the parser.

* It does not mean that all parsers remove parentheses - in some situations they must be kept. For example, in code formatting tools all tokens must be preserved, including comments and whitespace. When an expression is parsed for evaluation, though, grouping parentheses are not preserved directly.

like image 2
Sergey Kalinichenko Avatar answered Nov 16 '22 16:11

Sergey Kalinichenko


I am fairly sure, that the grouping parentheses should not be regarded as an operator.

Note that footnotes are not part of the Standard (i.e. they are not normative, or in other words informative-only).

Let's start with definition of operator (emphasis mine going forward):

C111 6.4.6 Punctuators2

A punctuator is a symbol that has independent syntactic and semantic significance. Depending on context, it may specify an operation to be performed (which in turn may yield a value or a function designator, produce a side effect, or some combination thereof) in which case it is known as an operator (other forms of operator also exist in some contexts3). An operand is an entity on which an operator acts.

Next, let's take a look at definition of parenthesized expression:

C11 6.5.1/5 Primary expressions

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.

The void expression does not yield a value or a function designator. Its evaluation may produce a side effect, but it is not obligated to do so, as in the following exemplification of (void expr):

((void) 5); // here, no side effects are produced

In my opinion the above proves (by contradiction), that grouping parantheses are not an operator. In this context, ( and ) are just terminals, that are used to denote a parenthesized expression.


1) According to publicly available N1570 draft version.

2) The relevant citation is essentially the same as in C90, though it was located in 6.1.5 Operators subclause, which is no longer present.

3) This phrase was added in C99. I believe that its intent was to reflect new _Pragma operator.

like image 1
Grzegorz Szpetkowski Avatar answered Nov 16 '22 17:11

Grzegorz Szpetkowski