0% found this document useful (0 votes)
21 views

Operator precedence in C

Uploaded by

kasithangamcse92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Operator precedence in C

Uploaded by

kasithangamcse92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Operator precedence in C

The sequence in which operations are carried out in an expression in C is determined by


operator precedence. It specifies which operators are evaluated first and which are evaluated
last when multiple operators are present in an expression. The following is a list of operators
in C sorted by precedence, from highest to lowest:

o Parentheses () - Expressions enclosed in parentheses are evaluated first.


o Postfix increment/decrement (++/--): These operators increase or decrease the value
of a variable by 1 after the expression is evaluated.
o Prefix increment/decrement (++/--): These operators increase or decrease the value
of a variable by 1 before the expression is evaluated.
o Unary plus/minus (+/-): These operators are used to specify the sign of a value.
o Logical negation (!): The logical negation (!) operator is used to make a boolean
expression's logical meaning the opposite.
o Bitwise complement (~): This operator is used to flip the bits of a value.
o Casts: These operators are employed to change an expression's type into another type.
o Multiplication/division/modulo (* / %): These operators perform multiplication,
division, and modulo operations.
o Addition/subtraction (+ -): These operators perform addition and subtraction
operations.
o Bitwise shift (<< >>): These operators are employed to left- or right-shift the bits of a
number.
o Relational operators (< <= > >=): These operators perform a comparison between
two numbers and provide a boolean result as a result.
o The equality operators (==!=): These operators compare two values to see if they
are equal and then deliver a boolean result based on the comparison
o Bitwise AND (&): This operator combines two numbers in a bitwise manner.
o Bitwise XOR (): Using this operator, two numbers are bitwise XORed.
o Bitwise OR (|): This operator combines two numbers in a bitwise manner.
o Logical AND (&&): The logical AND operator (&&) applies the logical AND
function to two boolean values.
o Logical OR (||): Using this operator, two boolean numbers are logically combined.
Ternary conditional operator (?:): This operator allows for a conditional statement that
is used to evaluate one of two expressions based on the result of a boolean expression.
o Assignment (= += -= *= /= %= <<= >>= &= ^= |=): These operators are used to
assign a value to a variable, or to modify the value of a variable.
It's important to note that the order of evaluation is not always intuitive or straightforward,
and it's often necessary to use parentheses to clarify the intended order of operations. For
example, consider the expression "2 + 3 * 4". Without parentheses, it's not clear whether the
multiplication or the addition should be performed first. However, if we write "(2 + 3) * 4",
it's clear that the addition should be performed first, and then the multiplication.

In addition to operator precedence, C also has rules for operator associativity, which
determine the order in which operators of the same precedence are evaluated. For example,
the addition and subtraction operators have the same precedence, but they are left-associative,
which means that they are evaluated from left to right. So the expression "2 + 3 - 4" is
evaluated as "(2 + 3) - 4", which is equal to 1.
It's important to understand operator precedence and associativity in C, as they can affect the
outcome of an expression and lead to unexpected results if not used correctly. By
understanding these rules, you can write more efficient and correct code.

Operators in C have associativity, which defines the order in which operators with the same
precedence are evaluated. There are two kinds of associativity: left-to-right and right-to-left.

Here is a table of operator associativity in C:

Note that operators with left-to-right associativity are evaluated from left to right, while
operators with right-to-left associativity are evaluated from right to left. This can affect the
order of operations when multiple operators of the same precedence are used in an
expression.

To comprehend operator precedence in C, consider the following instance

C Program:

1. #include <stdio.h>
2.
3. int main() {
4. int x = 2, y = 3, z = 4;
5. int result = x + y * z; // result = 14
6. printf("Result: %d\n", result);
7.
8. int a = 5, b = 6, c = 7;
9. int result2 = (a + b) * c; // result2 = 77
10. printf("Result2: %d\n", result2);
11.
12. int d = 8, e = 9, f = 10;
13. int result3 = d + e % f; // result3 = 17
14. printf("Result3: %d\n", result3);
15.
16. int g = 11, h = 12, i = 13;
17. int result4 = g * h / i; // result4 = 10
18. printf("Result4: %d\n", result4);
19.
20. int j = 14, k = 15, l = 16;
21. int result5 = j == k || l > k; // result5 = 1 (true)
22. printf("Result5: %d\n", result5);
23.
24. return 0;
25. }
OutputResult: 14 Result2: 77 Result3: 17 Result4: 10 Result5: 1

Expression: An expression is a combination of operators, constants and variables. An


expression may consist of one or more operands, and zero or more operators to produce a
value.

Types of Expressions:
Expressions may be of the following types:

 Constant expressions: Constant Expressions consists of only constant values. A


constant value is one that doesn’t change.
Examples:
5, 10 + 5 / 6.0, 'x’
 Integral expressions: Integral Expressions are those which produce integer results after
implementing all the automatic and explicit type conversions.
Examples:
x, x * y, x + int( 5.0)
where x and y are integer variables.
 Floating expressions: Float Expressions are which produce floating point results after
implementing all the automatic and explicit type conversions.
Examples:
x + y, 10.75
where x and y are floating point variables.
 Relational expressions: Relational Expressions yield results of type bool which takes a
value true or false. When arithmetic expressions are used on either side of a relational
operator, they will be evaluated first and then the results compared. Relational
expressions are also known as Boolean expressions.
Examples:
x <= y, x + y > 2
 Logical expressions: Logical Expressions combine two or more relational expressions
and produces bool type results.
Examples:
x > y && x == 10, x == 10 || y == 5
 Pointer expressions: Pointer Expressions produce address values.
Examples:
&x, ptr, ptr++
where x is a variable and ptr is a pointer.
 Bitwise expressions: Bitwise Expressions are used to manipulate data at bit level. They
are basically used for testing or shifting bits.
Examples:
x << 3
shifts three bit position to left
y >> 1
shifts one bit position to right.
Shift operators are often used for multiplication and division by powers of two.
Note: An expression may also use combinations of the above expressions. Such
expressions are known as compound expressions.

#include <stdio.h>

void main()

int a=2,b=3,c=4,z;

z=a+b-(a*c);

printf("Result= %d",z);

You might also like