Who defines C operator precedence and associativity?

前端 未结 5 1478
灰色年华
灰色年华 2020-12-05 04:28

Introduction

In every textbook on C/C++, you\'ll find an operator precedence and associativity table such as the following:

5条回答
  •  情话喂你
    2020-12-05 05:11

    Precedence and associativity are defined in the standard, and they decide how to build the syntax tree. Precedence works by operator type(1+2*3 is 1+(2*3) and not (1+2)*3) and associativity works by operator position(1+2+3 is (1+2)+3 and not 1+(2+3)).

    Order of evaluation is different - it does not define how to build the syntax tree - it defines how to evaluate the nodes of operators in the syntax tree. Order of evaluation is defined not to be defined - you can never rely on it because compilers are free to choose any order they see fit. This is done so compilers could try to optimize the code. The idea is that programmers write code that shouldn't be affected by order of evaluation, and yield the same results no matter the order.

提交回复
热议问题