Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loose endsCS-2301, B-Term 20091 “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.

Similar presentations


Presentation on theme: "Loose endsCS-2301, B-Term 20091 “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd."— Presentation transcript:

1 Loose endsCS-2301, B-Term 20091 “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

2 Loose endsCS-2301, B-Term 20092 Reminder – Relational Operators Relational operators –, >=, ==, != –Return 0 if false, 1 if true Let double pi = 3.141592654; Then pi < 3 returns 0 pi <= 4 returns 1 pi == 3 returns 0

3 Loose endsCS-2301, B-Term 20093 Logical AND and OR operators A > B && C < D is true (i.e., has value 1 ) if and only if both A > B and C < D are both true A > B || C < D is true (i.e., has value 1 ) if either of A > B and C < D is true

4 Loose endsCS-2301, B-Term 20094 So what is the Difference … … between '|' and '||' ? … between '&' and '&&' ? Answer:– –'|' and '&' are bitwise operators Will evaluate both operands before doing bitwise arithmetic –'||' and '&&' are logical operators Will only evaluate one operand, if answer is known after first operand!

5 Loose endsCS-2301, B-Term 20095 Logical AND and OR operators (continued) expr 1 && expr 2 has value 1 if and only if both expr 1 and expr 2 are non-zero Otherwise, it has the value 0 expr 1 || expr 2 has value 1 if either expr 1 and expr 2 is non-zero

6 Loose endsCS-2301, B-Term 20096 Special Property of Logical Operators Logical operators are evaluated left-to-right Including side effects When the result is known, evaluation stops! E.g.:– in expr 1 && expr 2 –expr 1 is evaluated first –If result is zero, the && expression returns 0 expr 2 is not evaluated! –If result is non-zero, then expr 2 is evaluated If expr 2 is zero, && expression returns 0 Otherwise, && expression returns 1

7 Loose endsCS-2301, B-Term 20097 Special Property (continued) In expr 1 || expr 2 –expr 1 is evaluated first –If result is non zero, || expression returns 1 expr 2 is not evaluated! –If result is zero, then expr 2 is evaluated If expr 2 is non-zero, || expression returns 1 Otherwise, || expression returns 0

8 Loose endsCS-2301, B-Term 20098 Why this Special Property? if (n != 0 && x/n > y) {…} –Don’t want to attempt x/n if n is zero  zero- divide fault! Many similar situations in C, both && and || –E.g., following pointers, indexing arrays, etc. –expr 1 checks a limit situation, and then expr 2 tests what you really want to know.

9 Loose endsCS-2301, B-Term 20099 Questions?

10 Loose endsCS-2301, B-Term 200910 Scope Definition:– Scope of an identifier –The region of a program where an identifier is known –Compiler reports error on use of an identifier outside of its scope –An identifier may be used in different ways in different scopes of the same program –An identifier may be redefined for a different purpose in an inner scope

11 Loose endsCS-2301, B-Term 200911 Possible Scopes of Identifiers The entire program (i.e.,.c file) –Starts at point of declaration and continues to end A function –Starts with function name, continues to end of function (i.e., '}' ), includes parameters A compound statement –Starting at point of declaration and continuing to '}' for loop –Expressions and statement (C99) A function prototype –Limited to the prototype itself

12 Loose endsCS-2301, B-Term 200912 Scope – Examples int n; void func(int m){ int k; { double j; …; } }//func Visible throughout program (from this point on) m visible throughout function k visible throughout function (from this point on) j visible only within '{}' (from this point on)

13 Loose endsCS-2301, B-Term 200913 Scope – Name Spaces Different kinds of identifiers have separate name spaces Do not conflict with identifiers of other spaces E.g., Function and variable can share same name in same scope! Stylistically, this is a really, really bad idea!

14 Loose endsCS-2301, B-Term 200914 Scope – Re-using Identifiers Any identifier can be freely re-used in a separate scope Very common E.g., for (int i; …; …) — separate i variables! An identifier can be re-defined in an inner scope Happens often enough – e.g., int i;... for (int i; …; …) { … i … }; Refers to i of inner scope i of outer scope becomes inaccessible in inner scope

15 Loose endsCS-2301, B-Term 200915 Questions?

16 Loose endsCS-2301, B-Term 200916 Order of Evaluation Operator Precedence rules specify the order of operations … … but –don’t specify which operand is evaluated first z = f(x) + g(y) – side effects make a difference! –don’t specify which argument is evaluated first h(f(x), g(y)) – side effects make a difference! –Note: ',' specifies order in expressions, not argument lists!

17 Loose endsCS-2301, B-Term 200917 Questions?


Download ppt "Loose endsCS-2301, B-Term 20091 “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd."

Similar presentations


Ads by Google