Operator Precedence and Associativity in C Programming - Notre Dame University, Study notes of Computer Science

An overview of operator precedence and associativity in the c programming language. It covers various operators such as arithmetic, logical, and relational, and explains their position, associativity, and precedence. The document also discusses the use of parentheses to control the order of evaluation and the difference between prefix and postfix increment/decrement. Students of the university of notre dame taking the cse 20211 - fundamentals of computing i course will find this information useful for understanding the basics of c programming.

Typology: Study notes

Pre 2010

Uploaded on 02/24/2010

koofers-user-znu
koofers-user-znu ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 20211 โ€“ Fundamentals of Computing I โ€“ Univ. of Notre Dame
09/09/09
Read Chapter 3
Operator Precedence
Different operators in C are +, *, -, /, %,++, --. The latter two take either prefix or postfix form.
Compound operators include +=, /=, *=, -=, %=. For instance, i+=2 implies i = i + 2;
Generally parenthesis can be used to control the precedence order in operators. Expressions in a
parenthesis are evaluated first. When an expression contains more than one parentheses, the
operators located in the innermost pair of parentheses have the highest precedence. For example,
the + has the highest precedence in the statement below.
x = ((a+b)-c/d);
Also we cannot use two consecutive arithmetic operators in an expression between two variables,
lest parentheses are used to separate them. For instance a*-b should be written as a*(-b)
Operator Name # operands Position Associativity Precedence
( parentheses unary prefix L to R 1
) parentheses unary postfix L to R 1
+ positive sign unary prefix R to L 2
- negative sign unary prefix R to L 2
++ post-inc. unary postfix L to R 2
-- post-dec. unary postfix L to R 2
++ pre-inc. unary prefix R to L 2
-- pre-dec unary prefix R to L 2
+= addition and assign binary infix R to L 2
-= sub and assign binary infix R to L 2
*= mult. and assign binary โ€œ โ€œ โ€œ
/= div and assign binary โ€œ โ€œ โ€œ
%= remainder and assign binary โ€œ โ€œ โ€œ
% remainder binary infix L to R 3
* multiply binary โ€œ โ€œ โ€œ
/ division binary โ€œ โ€œ โ€œ
+ add binary โ€œ โ€œ 4
- sub binary โ€œ โ€œ 4
= assignment binary โ€œ R to L 5
pf3

Partial preview of the text

Download Operator Precedence and Associativity in C Programming - Notre Dame University and more Study notes Computer Science in PDF only on Docsity!

CSE 20211 โ€“ Fundamentals of Computing I โ€“ Univ. of Notre Dame 09/09/ Read Chapter 3 Operator Precedence Different operators in C are +, , - , /, %,++, --. The latter two take either prefix or postfix form. Compound operators include +=, /=, =, - =, %=. For instance, i+=2 implies i = i + 2; Generally parenthesis can be used to control the precedence order in operators. Expressions in a parenthesis are evaluated first. When an expression contains more than one parentheses, the operators located in the innermost pair of parentheses have the highest precedence. For example, the + has the highest precedence in the statement below. x = ((a+b)-c/d); Also we cannot use two consecutive arithmetic operators in an expression between two variables, lest parentheses are used to separate them. For instance a-b should be written as a(-b) Operator Name # operands Position Associativity Precedence ( parentheses unary prefix L to R 1 ) parentheses unary postfix L to R 1

  • positive sign unary prefix R to L 2
  • negative sign unary prefix R to L 2 ++ post-inc. unary postfix L to R 2 -- post-dec. unary postfix L to R 2 ++ pre-inc. unary prefix R to L 2 -- pre-dec unary prefix R to L 2 += addition and assign binary infix R to L 2
  • = sub and assign binary infix R to L 2 *= mult. and assign binary โ€œ โ€œ โ€œ /= div and assign binary โ€œ โ€œ โ€œ %= remainder and assign binary โ€œ โ€œ โ€œ % remainder binary infix L to R 3
  • multiply binary โ€œ โ€œ โ€œ / division binary โ€œ โ€œ โ€œ
  • add binary โ€œ โ€œ 4
  • sub binary โ€œ โ€œ 4 = assignment binary โ€œ R to L 5

Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression ( i.e., z++ evaluates to z ) and z only incremented after all else is done. Logical operators !: logical not (unary) &&: logical and (binary) ||: logical or (binary) (condition)? exp1: exp When the C program encounters the ?: operator, it first evaluates the condition. If the conditions turns to be true, then exp1 is evaluated and used as the value of entire expression, if the condition is false the value is the result of evaluating exp2. max = (x > y)? x : y A B && || !A !B True True True True False False True False False True False True False False False False True True False True False True True False Relational Operators == !=

<

= <= Character variables The characters are defined using char. For example, char a; To initialize, a to a character value, do a = โ€˜yโ€™;