Docsity

C language me code jo jo, Summaries of Programming Languages

Toggle sidebar
bg1
Department of Computer Science and Engineering
UIET, CSJM University, Kanpur
C Operator Precedence and Associativity
A quick reference for solving expressions correctly
Rule: Operators with higher precedence are grouped first. If operators have the same precedence, associativity decides
the grouping. Use parentheses whenever they make your intended calculation clearer.
Complete C precedence sequence - highest to lowest
Operators Associativity What they do
() [] -> . postfix ++ postfix -- Left to right postfix / access
prefix ++ -- + - ! ~ * & sizeof (type) Right to left unary / cast
* / % Left to right multiply, divide, remainder
+ - Left to right add, subtract
<< >> Left to right bit shifts
< <= > >= Left to right relational comparison
== != Left to right equality comparison
& ^ | Left to right bitwise operations
&& || Left to right logical AND, logical OR
?: Right to left conditional expression
= += -= *= /= %= &= ^= |= <<= >>= Right to left assignment
,Left to right comma operator
For the present C course, remember this order first
() -> unary -> * / % -> + - -> comparisons -> == != -> && -> || -> =
Worked examples
Expression Correct grouping and result
result = 2 + 3 * 4; result = 2 + (3 * 4) = 14;
Multiplication has higher precedence than addition.
result = (2 + 3) * 4; result = 5 * 4 = 20;
Parentheses change the grouping.
average = a + b + c / 3.0; Only c is divided by 3.0. Write (a + b + c) / 3.0 for the intended
average.
Best practice: Do not rely on precedence when a reader may misunderstand the expression. Parentheses are free,
clear, and safer.
COPYRIGHT SANDESH GUPTA
UIET, CSJM UNIVERSITY, KANPUR
UIET, CSJM University, Kanpur C Programming - Operator Precedence | Page 1 of 1

Partial preview of the text

Download C language me code jo jo and more Summaries Programming Languages in PDF only on Docsity!

Department of Computer Science and Engineering

UIET, CSJM University, Kanpur

C Operator Precedence and Associativity

A quick reference for solving expressions correctly

Rule: Operators with higher precedence are grouped first. If operators have the same precedence, associativity decides

the grouping. Use parentheses whenever they make your intended calculation clearer.

Complete C precedence sequence - highest to lowest

Operators Associativity What they do

() [] ->. postfix ++ postfix -- Left to right postfix / access

prefix ++ -- + -! ~ * & sizeof (type) Right to left unary / cast

  • / % Left to right multiply, divide, remainder
    • Left to right add, subtract

<< >> Left to right bit shifts

< <= > >= Left to right relational comparison

== != Left to right equality comparison

& ^ | Left to right bitwise operations

&& || Left to right logical AND, logical OR

?: Right to left conditional expression

= += -= *= /= %= &= ^= |= <<= >>= Right to left assignment

, Left to right comma operator

For the present C course, remember this order first

() -> unary -> * / % -> + - -> comparisons -> == != -> && -> || -> =

Worked examples

Expression Correct grouping and result

result = 2 + 3 * 4;

result = 2 + (3 * 4) = 14; Multiplication has higher precedence than addition.

result = (2 + 3) * 4;

result = 5 * 4 = 20; Parentheses change the grouping.

average = a + b + c / 3.0;

Only c is divided by 3.0. Write (a + b + c) / 3.0 for the intended average.

Best practice: Do not rely on precedence when a reader may misunderstand the expression. Parentheses are free,

clear, and safer.

COPYRIGHT SANDESH GUPTA^ UIET, CSJM UNIVERSITY, KANPUR

UIET, CSJM University, Kanpur C Programming - Operator Precedence | Page 1 of 1