answersLogoWhite

0


Best Answer

Operator precedence is often associated with order of evaluation, however order of evaluation and operator precedence are actually undefined in C. Expressions may be evaluated in any order while operator precedence is derived from the language grammar. The concept of operator precedence merely simplifies our understanding.

When we think of operator precedence we can imagine a table with 15 rows. When parsing an expression, any operator on a given row takes precedence over those on lower rows. If we also number the rows in ascending order then we can associate each row with a precedence level, such that level 1 has the highest precedence and lower rows have lower precedence.

In addition to precedence, each row also has an associativity. Rows 2, 13 and 14 have right-to-left associativity while all others have left-to-right. For instance, the assignment operator appears on row 14 thus the expression a=b=c is evaluated as if it were actually written a=(b=c) rather than (a=b)=c.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

By way of an example, consider the following compound expression:

5 + 4 * 3

Languages such as APL that have no operator precedence will evaluate this expression from right-to-left:

5 + 4 * 3 = 5 + 12 = 17

Conversely, other languages such as SmallTalk, which also has no operator precedence, will evaluate the expression from left-to-right:

5 + 4 * 3 = 9 * 3 = 27

If we reversed the original expression (3 * 4 + 5), these two languages would produce their opposing results (27 and 17, respectively). But in C++, which supports operator precedence, it makes no difference which expression is used:

5 + 4 * 3 = 17

3 * 4 + 5 = 17

This is because multiplication always takes precedence over addition.

The ellipses operator has a higher precedence than multiplication, so if we expect the evaluation to be 27 we can use ellipses to remove the ambiguity, regardless of which expression is used:

(5 + 4) * 3 = 27

3 * (4 + 5) = 27

Operator precedence in C++ (and most other languages based upon C) ensures consistent evaluation of compound expressions, and is largely consistent with the standard operator precedence used in mathematics.

The following list shows all the C++ operators and their relative precedence (1 being the highest precedence). Note that operators with equal precedence are evaluated from right-to-left.

  1. Ellipses, element, indirection, member and scope: () [] -> . ::
  2. NOT, complementary, unary minus, unary plus, pointer, addressof, sizeof, type, cast, increment and decrement: ! ~ - + * & sizeof type cast ++ --
  3. Multiply, divide and modulo: * / %
  4. Addition and subtraction: + -
  5. Bitwise shift left and right: << >>
  6. Comparison: < <= > >=
  7. Equality: C)

    If there's ever any doubt, use ellipses to remove all ambiguity from compound expressions. Even if they're not actually required, your code will be that much easier to read.

    An even better solution is to break compound expressions down into a series of separate operations. This not only removes ambiguity, it also eliminates the need for automatic variables (a consequence of compound expressions), it also allows you to re-use the result of an expression without having to re-evaluate it.

    Thus:

    int x = 5, y = 10, z;

    z = 1 + x * y; // x * y creates an automatic variable which is then added to 1.

    Becomes:

    int x = 5, y = 10, z;

    z = x * y; // automatic variable eliminated by assignment

    ++z;

    Example of expression re-use:

    int x=3;

    int y = x * 2 + 5; // x * 2 creates an automatic variable which is then added to 5.

    int z = x * 2 + 6; // x * 2 creates another automatic variable which is then added to 6.

    Becomes:

    int x = 3;

    int t = x * 2; // eliminates automatic variables and permits re-use of result

    int y = t + 5;

    int z = t + 6;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is operator precedence in c programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you compare precedence of operators in programming of conversion of infix to postfix?

Each operator has a certain precedence level, usually some numeric value. As you parse the expression, you compare the precedence of each operator with the precedence of the last operator, and you either generate code or you push the operator and its operand(s) on two stacks.


Which arithmetic operator have highest precedence?

In all popular high-level programming languages, the order in which operators are interpreted ("operator precedence") is vital to ensuring that all compilers execute instructions in precisely the same manner, as the "order of operations" rule is vital in mathematics. In the case of C and C++, arithmetic operators are executed prior to logic operators. For a detailed description of operator precedence, see the related links below.


What is precedence between relational operator and arithmetic operator?

Arithmetic operators (+, -, *, /, % ) have greater precedence over relational operators (&lt;, &gt;, &lt;=, &gt;=, ==, !=) in C language.


What is the function of symboland in C programming?

The symbol and (ampersand, &amp;) in C and C++ programming is the bitwise inclusive or operator. If there are two ampersands (&amp;&amp;) it is a relational inclusive or operator. As a unary operator, it means to take the address of something. In C++, it can also be overridden in a class method to mean nearly anything else.


Logical or operator can be compared to what in terms of precedence?

The logical OR operator can be compared to ____ in terms of precedence.

Related questions

What is the order of precedence with regard to the operator used in embedded C program?

Operator precedence in embedded C is exactly the same as in standard C.


How can you compare precedence of operators in programming of conversion of infix to postfix?

Each operator has a certain precedence level, usually some numeric value. As you parse the expression, you compare the precedence of each operator with the precedence of the last operator, and you either generate code or you push the operator and its operand(s) on two stacks.


Which arithmetic operator have highest precedence?

In all popular high-level programming languages, the order in which operators are interpreted ("operator precedence") is vital to ensuring that all compilers execute instructions in precisely the same manner, as the "order of operations" rule is vital in mathematics. In the case of C and C++, arithmetic operators are executed prior to logic operators. For a detailed description of operator precedence, see the related links below.


What is precedence between relational operator and arithmetic operator?

Arithmetic operators (+, -, *, /, % ) have greater precedence over relational operators (&lt;, &gt;, &lt;=, &gt;=, ==, !=) in C language.


Use of scope resolution operator in C programming?

:: operator can not be used in C.


What is the function of symboland in C programming?

The symbol and (ampersand, &amp;) in C and C++ programming is the bitwise inclusive or operator. If there are two ampersands (&amp;&amp;) it is a relational inclusive or operator. As a unary operator, it means to take the address of something. In C++, it can also be overridden in a class method to mean nearly anything else.


Logical or operator can be compared to what in terms of precedence?

The logical OR operator can be compared to ____ in terms of precedence.


What does the job of c operators consist of?

A C operator is not a job or profession but rather a coding language. C operators perform certain tasks in programming such as a "+ " operator performs addition.


How do you Know if a grammar is an operator precedence grammar?

The grammar is said to be operator precedence grammar, if its right hand side of its production should not have the Empty production or two non-terminal should not be adjacent to each other, then we call it as operator precedence grammar The grammar is said to be operator precedence grammar, if its right hand side of its production should not have the Empty production or two non-terminal should not be adjacent to each other, then we call it as operator precedence grammar


What is the C plus plus expression and operator precedence?

Precedence is determined by operators only. Every operator has a precedence in the range 1 through 17, where 1 has the highest precedence. All precedences have left-to-right associativity except 3 and 15 which are right-to-left. Precedence 1: scope-resolution operator Precedence 2: postfix increment/decrement, function-style type cast, function call, array subscripting and selection by reference or pointer. Precedence 3: prefix increment/decrement, unary plus/minus, logical and bitwise NOT, C-style cast, dereferencing, address-of, sizeof, new/new [] and delete/delete []. Precedence 4: pointer to member. Precedence 5: multiplication, division and modulo. Precedence 6: addition and substraction. Precedence 7: bitwise left/right shift. Precedence 8: relational operators (&lt;, &lt;=, &gt; and &gt;=). Precedence 9: equal/not equal operators (= and !=) Precedence 10: bitwise AND Precedence 11: bitwise XOR Precedence 12: bitwise OR Precedence 13: logical AND Precedence 14: llogical OR Precedence 15: ternary conditional, assignment and compound assignment. Precedence 16: throw Precedence 17: comma


How do you evaluate an expression in hierarchy of operations?

Expressions are evaluated according to the language grammar. Operator precedence and associativity are derived from the grammar in order to aid our understanding, however the order of evaluation is independent of both because the C language standard does not specify operator precedence. The general arithmetic rules of precedence hold for most expressions such that parenthesised operations take precedence over orders followed by multiplication/division operations and finally addition/subtraction operations (as per the PODMAS acronym). Many of the more complex expressions we encounter can generally be evaluated according to the operator precedence table, which includes the associativity, such that operations with higher precedence are bound more tightly (as if with parenthesis) than those with lower precedence.


What pair of brackets can be used to override the precedence of the operators in Excel?

Parentheses or round brackets ( and ) override operator precedence.