c programming lecture 5. precedence and associativity of operators b rules of associativity and...

26
C Programming C Programming Lecture 5 Lecture 5

Upload: jordan-lester

Post on 24-Dec-2015

239 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

C ProgrammingC Programming

Lecture 5Lecture 5

Page 2: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Precedence and Precedence and Associativity Associativity of Operatorsof Operators

Rules of Rules of associativityassociativity and and precedenceprecedence of operators of operators determine precisely how determine precisely how expressions are operated.expressions are operated.• In the expression In the expression 1 + 2 * 31 + 2 * 3, , the operator * has higher the operator * has higher precedenceprecedence than +, causing the than +, causing the multiplication to be performed multiplication to be performed first.first.

• The result is 7 The result is 7 instead ofinstead of 9. 9.

Page 3: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Associativity of OperatorsAssociativity of Operators

When two operators placed in When two operators placed in proximity in an expression proximity in an expression have the same have the same precedenceprecedence, , their their associativityassociativity is used is used to determine how the to determine how the expression is evaluated.expression is evaluated.• In the expression In the expression 6 / 2 * 36 / 2 * 3, , both both // and and ** have the have the same same precedenceprecedence. Since they both . Since they both have have left to right associa-left to right associa-tivitytivity, the expression has the , the expression has the value value 99 rather than rather than 11..

Page 4: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Partial Table of Operator Partial Table of Operator Precedence and AssociativityPrecedence and Associativity

Operator Associativity() ++ (postfix) -- (postfix) left to right

+(unary) -(unary) ++(prefix) --(prefix) right to left

* / % left to right

+ - left to right

= += -= right to left

Operators on the top line have the highest precedence. Precedence decreases line-by-line going down the table. All operators on the same line have equal precedence.

Page 5: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Parentheses and the Parentheses and the Order of OperationsOrder of Operations

Expressions inside Expressions inside parentheses are evaluated parentheses are evaluated first.first.• This provides for the use of This provides for the use of parenthesesparentheses to clarify or change to clarify or change the order in which operations the order in which operations are performed.are performed.

1 + 2 * 31 + 2 * 3 has a value of has a value of 77.. ((1 + 21 + 2))* 3* 3 has a value of has a value of 99. .

Page 6: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Binary Plus versus Unary Binary Plus versus Unary PlusPlus

Both binary plus and unary Both binary plus and unary plus are represented by a + plus are represented by a + (plus sign).(plus sign).• The same is true of binary and The same is true of binary and unary - (the minus sign).unary - (the minus sign).

Unary + and - have a higher Unary + and - have a higher precedence that binary + and precedence that binary + and - and the unary operators - and the unary operators associate right-to-left associate right-to-left instead of left-to-right.instead of left-to-right.

Page 7: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Example of Unary Example of Unary OperatorsOperators

In the expression In the expression -- a * b a * b -- c c the first minus is the first minus is unaryunary and and the second is the second is binarybinary..

We can We can use parenthesesuse parentheses to to write an equivalent write an equivalent expression that is expression that is less less likely to be misinterpretedlikely to be misinterpreted..

((- a) * b) - c((- a) * b) - c

Page 8: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Example of Unary Example of Unary Operators Using NumbersOperators Using Numbers

-1 * 2 - 3-1 * 2 - 3 has a value of has a value of -5-5it it isis equivalent to equivalent to ((-1) * 2) - 3 ((-1) * 2) - 3 oror (-2) - 3 (-2) - 3 which is which is -5-5it it isis notnot equivalent to equivalent to (-1) * (2 - 3) (-1) * (2 - 3) oror (-1) * (-1) (-1) * (-1) which is which is +1+1

Page 9: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Increment and Decrement Increment and Decrement OperatorsOperators

++++ (the increment operator) and (the increment operator) and ---- (the decrement operator) are (the decrement operator) are unary operators with the same unary operators with the same precedence and right-to-left precedence and right-to-left associativity as the other unary associativity as the other unary operators.operators.

The ++ and -- operators can occur The ++ and -- operators can occur in either a in either a prefixprefix or or postfixpostfix position with position with different resultsdifferent results..

Page 10: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Prefix versus Postfix When Using Prefix versus Postfix When Using

Increment and Decrement Increment and Decrement OperatorsOperators

Each of the expressions Each of the expressions ++i++i and and i++i++ causes the causes the stored valuestored value of i of i to be incremented by 1, however:to be incremented by 1, however:• The expression The expression ++i++i causes the causes the stored stored valuevalue of i to be of i to be incremented firstincremented first, , with the expression with the expression thenthen taking as taking as its value the its value the new stored valuenew stored value of i. of i.

• The expression The expression i++i++ has as its value has as its value the the current valuecurrent value of i; of i; thenthen the the stored valuestored value is incremented. is incremented.

Page 11: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Example of the Increment Example of the Increment and Decrement Operatorsand Decrement Operators

int int aa, , bb, , cc = 0; = 0;aa = = ++++cc;;bb = = cc++++;;printf(“%d %d %d\n”, printf(“%d %d %d\n”, aa, , bb, , ++++cc););/* /* 1 1 31 1 3 is printed */ is printed */

cc is incremented making its value 1. is incremented making its value 1.The result assigned to The result assigned to aa making its value making its value

1.1.The value of The value of cc is assigned to is assigned to bb making its value making its value 1.1.

Then Then cc is incremented making its value 2. is incremented making its value 2.Finally, Finally, cc is incremented before it is printed, is incremented before it is printed, making its value 3.making its value 3.

Page 12: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Practice with Operators Practice with Operators and Expressionsand Expressions

Declarations and Initializationsint a = 1, b = 2, c = 3, d = 4;

Expression Equivalent expression Value

a * b / c (a * b) / c 0

a * b % c + 1 ((a * b) % c) + 1 3

++a * b - c -- ((++a) * b) - (c--) 1

7 - -b * ++d 7 - ((-b) * (++d)) 17

Page 13: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Partial Table of Operator Partial Table of Operator Precedence and AssociativityPrecedence and Associativity

Operator Associativity() ++ (postfix) -- (postfix) left to right

+(unary) -(unary) ++(prefix) --(prefix) right to left

* / % left to right

+ - left to right

= += -= right to left

Operators on the top line have the highest precedence. Precedence decreases line-by-line going down the table. All operators on the same line have equal precedence.

Page 14: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Assignment OperatorsAssignment Operators

C treats C treats == as an operator. as an operator.•It’s It’s precedenceprecedence is is lowerlower than almost all of the other than almost all of the other operators.operators.

•It’s It’s associativityassociativity is is right right to leftto left..

Page 15: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Form of an Assignment Form of an Assignment ExpressionExpression

A simple A simple assignment assignment expressionexpression is of the form is of the form variable = right_sidevariable = right_side

The value of The value of right_side right_side is is assigned toassigned to variable, variable, and and that becomes the value of the that becomes the value of the assignment expression assignment expression as a as a whole.whole.

Page 16: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Assignment Assignment ExpressionsExpressions versus Assignment versus Assignment

StatementsStatements

An An assignment expressionassignment expression has has no semicolon at the end.no semicolon at the end.• An An assignment statementassignment statement does. does.

We can use assignment We can use assignment expressions to condense a expressions to condense a sequence of assignment sequence of assignment statements.statements.

Page 17: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Example of Equivalent Example of Equivalent Code Using Assignment Code Using Assignment

ExpressionsExpressions

Assignment statementsAssignment statementsb = 2;b = 2;c = 3;c = 3;a = b + c;a = b + c;

Equivalent statement using Equivalent statement using assignment expressionsassignment expressionsa = (b = 2) + (c = 3);a = (b = 2) + (c = 3);

NoteNote the assignment statement the assignment statement ends with a semicolon, the ends with a semicolon, the expressions don’t.expressions don’t.

Page 18: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Other Assignment Other Assignment OperatorsOperators

C has operators that combine C has operators that combine assignment with other operations. assignment with other operations. These are considered assignment These are considered assignment operators and have the operators and have the same same precedence and right-to-left precedence and right-to-left associativity as =.associativity as =.

ExampleExamplek = k + 2;k = k + 2;

is equivalent tois equivalent tok += 2;k += 2;

Page 19: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

The Assignment OperatorsThe Assignment Operators

= += -= *= /= %= >>= <<= &= ^= |== += -= *= /= %= >>= <<= &= ^= |=The semantics of the other assignment The semantics of the other assignment operators is specified byoperators is specified by

variable op= expressionvariable op= expressionwhich is equivalent towhich is equivalent to

variable = variable op (expression)variable = variable op (expression)j *= k + 3 j *= k + 3

is equivalent tois equivalent to j = j * (k + 3)j = j * (k + 3)

and notand notj = j * k + 3j = j * k + 3

Page 20: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Preprocessor DirectivesPreprocessor Directives

Include files (header files)Include files (header files)• #include #include <<filenamefilename>>

– Causes the preprocessor to look for Causes the preprocessor to look for filenamefilename in system defined places in system defined places and replace the #include line with and replace the #include line with a copy of contents of a copy of contents of filenamefilename..

• #include #include ““filenamefilename””– Same as above, but the preprocessor Same as above, but the preprocessor looks in the looks in the current directorycurrent directory beforebefore looking in the system looking in the system defined directory locations.defined directory locations.

Page 21: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Where are these Where are these “System Defined” Places?“System Defined” Places?

On UNIX systems, the standard On UNIX systems, the standard header files such as header files such as stdio.hstdio.h can typically be found in the can typically be found in the directory directory /usr/include/usr/include..• You can use any editor to look You can use any editor to look at what is in these files.at what is in these files.

Page 22: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Header Files Must be Header Files Must be Included for Functions in the Included for Functions in the

Standard LibraryStandard Library

The The system knowssystem knows where to where to find the find the codecode for functions for functions in the standard library.in the standard library.

However, it is the However, it is the responsibility of the responsibility of the programmerprogrammer to include the to include the header filesheader files that provide the that provide the prototypesprototypes for library for library functions.functions.

Page 23: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

The Libraries versusThe Libraries versusthe Header Filesthe Header Files

The The standard librarystandard library contains contains object code (already compiled object code (already compiled library functions).library functions).

The The standard header filesstandard header files are are text files that you can read text files that you can read using a text editor. They using a text editor. They provide information needed by provide information needed by the library functions.the library functions.

Page 24: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

StyleStyle

Do not condense code just for Do not condense code just for the sake of using less spacethe sake of using less space y = 2;y = 2; z = 3;z = 3; x = y + z;x = y + z;is more readable thanis more readable than x = (y = 2) + (z = 3);x = (y = 2) + (z = 3);andand a += 7;a += 7; versus versus a = a + 7;a = a + 7;is a matter of choice.is a matter of choice.

Page 25: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

Common Programming Common Programming ErrorsErrors

Warning and Error Messages Warning and Error Messages usually refer to a line usually refer to a line number.number.• The problem may be prior to that The problem may be prior to that line -- such as not properly line -- such as not properly closing a comment or a string closing a comment or a string constant.constant.

Page 26: C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions

System ConsiderationsSystem Considerations

ANSI C has both a unary + and a ANSI C has both a unary + and a unary -unary -• Traditional C has only unary -, so Traditional C has only unary -, so you should not use the unary + you should not use the unary + operator if your are writing code operator if your are writing code that needs to be portable to a that needs to be portable to a machine that uses traditional C.machine that uses traditional C.

If you are writing code for a If you are writing code for a spectrum of systems, limitations spectrum of systems, limitations of all of the systems must be of all of the systems must be respected.respected.