Write a C program to Perform Operator Precedence. Here’s a Simple Program for implementing or perform operator precedence C Programming Language.
The C language standard doesn’t specify operator precedence. It specifies the language grammar, and the precedence table is derived from it to simplify understanding.
Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Operator precedence & associativity table
Operator precedence & associativity are listed in the following table and this table is summarized in decreasing Order of priority i.e topmost operator has highest priority and bottommost operator has Lowest Priority.
Caption
OPERATOR
DESCRIPTION
ASSOCIATIVITY
( ) [ ]. -> ++ –
Parentheses (function call) (see Note 1) Brackets (array subscript) Member selection via object name Member selection via pointer Postfix increment/decrement (see Note 2)
left-to-right
++ – + – ! ~ (type) * & sizeof
Prefix increment/decrement Unary plus/minus Logical negation/bitwise complement Cast (convert value to temporary value of type) Dereference Address (of operand) Determine size in bytes on this implementation
right-to-left
* / %
Multiplication/division/modulus
left-to-right
+ –
Addition/subtraction
left-to-right
<< >>
Bitwise shift left, Bitwise shift right
left-to-right
< <= > >=
Relational less than/less than or equal to Relational greater than/greater than or equal to
left-to-right
== !=
Relational is equal to/is not equal to
left-to-right
&
Bitwise AND
left-to-right
^
Bitwise exclusive OR
left-to-right
|
Bitwise inclusive OR
left-to-right
&&
Logical AND
left-to-right
| |
Logical OR
left-to-right
? :
Ternary conditional
right-to-left
= += -= *= /= %= &= ^= |= <<= >>=
Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment
right-to-left
,
Comma (separate expressions)
left-to-right
Below is the source code for C Program to input values into an array and display them which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
#include <stdio.h>
int main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
OUTPUT : :
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Operator Precedence :
Operator precedence & associativity table
Operator precedence & associativity are listed in the following table and this table is summarized in decreasing Order of priority i.e topmost operator has highest priority and bottommost operator has Lowest Priority.
OPERATOR
ASSOCIATIVITY
[ ].
->
++ –
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement (see Note 2)
left-to-right
+ –
! ~
(type)
*
&
sizeof
Unary plus/minus
Logical negation/bitwise complement
Cast (convert value to temporary value of type)
Dereference
Address (of operand)
Determine size in bytes on this implementation
> >=
Relational greater than/greater than or equal to
+= -=
*= /=
%= &=
^= |=
<<= >>=
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
,
Below is the source code for C Program to input values into an array and display them which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
need an explanation for this answer? contact us directly to get an explanation for this answer