Home  >  Article  >  Backend Development  >  Who Defines Operator Precedence and Associativity in C ?

Who Defines Operator Precedence and Associativity in C ?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-01 14:18:29273browse

Who Defines Operator Precedence and Associativity in C  ?

Operator Precedence and Associativity: The Controlling Factors

In the realm of programming, understanding the rules governing operator precedence and associativity is crucial. While many programming textbooks present charts showcasing operator precedence and associativity, the underlying question remains: who defines these rules?

Defining the Standards

Contrary to popular belief, the ANSI C11 standard does not explicitly define operator precedence and associativity. Instead, these concepts are embedded within the grammar of the language itself. For instance, the grammar production rule for addition ( ) and subtraction (-) in C reveals that a multiplicative expression is a subrule of an additive expression. This establishes the precedence between these operators.

Associativity and Order of Evaluation

Associativity determines how multiple uses of the same operator will be grouped. For example, the addition operator is left-to-right associative, meaning expressions like "x y z" will be grouped as "(x y) z."

It is important to differentiate associativity from order of evaluation. While these concepts are related, they are distinct. Associativity dictates grouping, while order of evaluation pertains to the sequence in which expressions are evaluated.

Order of Evaluation in Function Calls

Returning to the example of function call operators, left-to-right associativity implies that "f()()()" would be grouped as "(f())()." This grouping rule does not impact the order of function evaluation, which remains unsequenced. However, operands must be evaluated before the operator is applied.

Sequence Points and Sequenced Before

Historically, sequence points were used to define evaluation order in C and C . However, the terminology has since changed to "sequenced before." This concept signifies that the evaluation of one expression must precede another.

In summary, while operator precedence and associativity tables are commonly found in programming textbooks, these rules are ultimately derived from the language's grammar. Understanding the grammar enables programmers to accurately interpret expressions and determine their evaluation order.

The above is the detailed content of Who Defines Operator Precedence and Associativity in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn