CS代考程序代写 compiler OSU CSE 2421

OSU CSE 2421
Required Reading:
Computer Systems: A Programmer’s Perspective, 3rd Edition,
Chapter 1 thru Section 1.3
Pointers on C,
Chapter 5 thru Section 5.1.3, 5.3 through the end of the chapter
J.E.Jones

OSU CSE 2421
 lvalue/Lvalue/L-value: That operand found on the left side of the assignment operator.
◦ All L-values must be modifiable since they are being assigned a value.
 rvalue/Rvalue/R-value: That expression found on the right side of the assignment operator.
◦ R-values can be constants, expressions, be a return value from a function, etc.
J. E. Jones

OSU CSE 2421
Operator =
+

*
/
%
>
>=
< <= == != && || ! ++ Category Assignment Mathematical Mathematical Mathematical Mathematical Mathematical Comparison Comparison Comparison Comparison Comparison Comparison Logical Logical Logical Mathematical Duty Operator Category ‐‐ Mathematical Duty Decrement by 1 Equals Addition & Subtraction | Multiplication ^ Division << Modulo >>
Bitwise Bitwise Bitwise Bitwise Bitwise Bitwise Unary Unary Unary Unary Unary
AND
Inclusive OR
Exclusive OR
Shift bits left
Shift bits right
One’s Complement Positive
Negative
Pointer
Address
Returns size of an object Element Access
Pointer element Access odd ‘if’ expression;
not often used
Greater than
Greater than or equal to Less than
Less than or equal to
is equal to
is not equal to
AND
OR
NOT
Increment by 1
~
+

* & sizeof
. Structure ‐> Structure ?: Conditional
J. E. Jones

OSU CSE 2421
 C operators can be classified according to the number of operands which they take.
 C has unary operators, binary operators, and one ternary operator (the conditional operator ? : )
 The operands of C operators are expressions, which can be constants, variables, or expressions which contain one or more operators.
 Expressions will always be evaluated by the code which the compiler generates; that is, an expression has a value (which has a type, of course).
 There is a table of operators posted on Piazza which shows which operators are unary, binary or ternary, and the precedence and associativity of each operator (precedence/associativity covered below).
J. E.4Jones

OSU CSE 2421
 Precedence refers to the relationship between two operators in terms of the order in which the operations are performed.
 Precedence is a binary relation, that is, it is defined with respect to pairs of (adjacent) operators. Binary operators are adjacent if they have one operand in common; unary operators are adjacent if they have the same operand.
 We can always enforce a precedence different from the precedence specified by the language for 2 operators by using parentheses, because operations inside parentheses are done first (Have the highest precedence).
 If two adjacent operators have the same precedence, then associativity is relevant.
 L-R associativity means that the operation specified by the leftmost operator is done first, and then the one specified by the rightmost operator. R-L associativity, of course, means the opposite order.
Bottom Line: P&A in C is just like math class, but there are a few “new” operators
J. E. Jones

OSU CSE 2421
#include
/* program to show associativity of the “/” operator */ int main() {
}
float num1;
float num2;
float num3;
num1 = 2.0 / 1.0 / 2.0;
num2 = (2.0 / 1.0) / 2.0;
num3 = 2.0 / (1.0 / 2.0); printf(“num1 is: %f
”, num1); printf(“num2 is: %f
”, num2); printf(“num3 is: %f
”, num3);
 What is printed?
J. E.6Jones

OSU CSE 2421
#include
/* program to show associativity of the “/” operator */ int main() {
}
float num1;
float num2;
float num3;
num1 = 2.0 / 1.0 / 2.0;
num2 = (2.0 / 1.0) / 2.0;
num3 = 2.0 / (1.0 / 2.0); printf(“num1 is: %f
”, num1); printf(“num2 is: %f
”, num2); printf(“num3 is: %f
”, num3);
/* num1 is: 1.000000 */
J. E.7Jones

OSU CSE 2421
#include
/* program to show associativity of the “/” operator */ int main() {
float num1;
float num2;
float num3;
num1 = 2.0 / 1.0 / 2.0;
num2 = (2.0 / 1.0) / 2.0;
num3 = 2.0 / (1.0 / 2.0); printf(“num1 is: %f
”, num1); printf(“num2 is: %f
”, num2);
/* num1 is: 1.000000 */
/* num2 is: 1.000000
Result with L-R associativity */
printf(“num3 is: %f
”, num3); }
J. E.8Jones

OSU CSE 2421
#include
/* program to show associativity of the “/” operator */ int main() {
}
float num1;
float num2;
float num3;
num1 = 2.0 / 1.0 / 2.0;
num2 = (2.0 / 1.0) / 2.0;
num3 = 2.0 / (1.0 / 2.0); printf(“num1 is: %f
”, num1); printf(“num2 is: %f
”, num2);
/* num1 is: 1.000000 */
/* num2 is: 1.000000
Result with L-R associativity */
printf(“num3 is: %f
”, num3);
/* num3 is: 4.000000
Result with R-L associativity */
J. E.9Jones

OSU CSE 2421
 Let’s see how an expression is evaluated, using the precedence and associativity in C.
 Suppose, before this statement is executed, ◦ a=1,b=3,andc=5,then
d = ++a * c + b++;
 How does the compiler determine the order of operations?
J. E. Jones

OSU CSE 2421
 How does the compiler determine the order of operations?
 We can take the view that the compiler does the binary operation with the highest precedence first, then next highest, but expressions with unary operators are not evaluated until they need to be, in order to evaluate a larger expression of which they are a part.
 We will also suppose that operands of binary operators are evaluated left to right (this is true for most compilers, and it is true for ours).
 A good practice is to use parentheses to show the order of evaluation, starting with the binary operator which has the highest precedence, and going to the lowest, considering associativity where necessary.
 So, let’s try to parenthesize the binary operators in the expression above, after parenthesizing all unary operator expressions (unary operators have higher precedence than all binary operators generally).
J. E. Jones

OSU CSE 2421
Parenthesize unary operators:
d = (++a) * c + (b++);
Precedence of binary operators: * first, then +, then = Now we can add the rest of the parentheses:
d = ((++a) * c) + (b++);
What is d after execution of this statement (Remember, before this statement, a = 1, b = 3, and c = 5)?
J. E. Jones

OSU CSE 2421
Value of expression 253
(d = ((++a) * c) + (b++)); d = (2 * 5) + 3
So, d has the value 13 and after the statement is executed
(a = 2, b = 4, and c = 5)
J. E. Jones

OSU CSE 2421
void main() {
[jones.5684@cse-fac2 test]$ precedence d = 13, when no parens are used.
a=2, b=4, c=5
d = 13, when some parens are used. a=2, b=4, c=5
}
int a, b, c, d;
a=1;
b=3;
c=5;
d = ++a * c + b++;
printf(“d = %d, when no parens are used.
”, d); printf(“a=%d, b=%d, c=%d
”, a, b, c);
d = 13, when all parens are used. a=2, b=4, c=5 [jones.5684@cse-fac2 test]$
a=1;
b=3;
c=5;
d = (++a) * c + (b++);
printf(“d = %d, when some parens are used.
”, d); printf(“a=%d, b=%d, c=%d
”, a, b, c);
a=1;
b=3;
c=5;
d = (((++a) * c) + (b++));
printf(“d = %d, when all parens are used.
”, d); printf(“a=%d, b=%d, c=%d
”, a, b, c);
J. E. Jones

OSU CSE 2421
 In C, assignments are expressions. This means that an assignment expression, just as any expression, has a value, which is the value of the rightmost expression.
 Assignment operator has lowest precedence (except for the comma operator)  Embedded assignments – legal anywhere an expression is legal.
◦ This allows multiple assignment: a = b = 1; /*R-L associativity */
◦ We’ll see how these are used in C a bit later.
◦ Other assignment operators (compound assignment operators) – same associativity – R-L
+= , –= , *= , /= , %=
e.g., a += 6; equivalent to a = a + 6;
 NOTE: Using an assignment operator (=) is legal anywhere it is legal to compare for equality (==), so it is not a syntax error (some compilers may give a warning, although stdlinux compiler will not!!!).
J. E. Jones

OSU CSE 2421
In Spring 1993, in the Operating System development group at SunSoft, we had a “priority-one” bug report come in describing a problem in the asynchronous I/O library. The bug was holding up the sale of $20 million worth of hardware to a customer who specifically needed the library functionality, so we were extremely motivated to find it. After some intensive debugging sessions, the problem was finally traced to a statement that read:
x==2;
It was a typo for what was intended to be an assignment statement. The programmer’s finger had bounced on the “equals’ key, accidentally pressing it twice instead of once. The statement as written compared x to 2, generated true or false, and discarded the result.
C is enough of an expression language that the compiler did not complain about a statement which evaluated an expression, had no side-effects, and simply threw away the result. We didn’t know whether to bless our good fortune at locating the problem, or cry with frustration at such a common typing error causing such an expensive problem
J. E. Jones

OSU CSE 2421
• Mathematical Symbols
• +-*/%
• addition, subtraction, multiplication, division, modulus • Works for both integers and float
• +-*/
• / operator performs
integer division if both operands are integer, i.e., truncates answer So that there is only an integer result;
otherwise, if at least one operand is float, performs floating point division (i.e., implicit casting is used) result contains whole number and fractional decimal result.
• %operatordividestwointegeroperandsandgivesintegerresultofthe remainder
• Associativity – L-R.
• Precedence:
1. Anything inside () 2. * / %
3. + –
J. E. Jones

OSU CSE 2421
 ++aanda++havethebehaviorofa=a+1 ◦ (difference is WHEN increment occurs)
 –aanda–havethebehaviorofa=a–1 ◦ (difference is WHEN decrement occurs)
[Postfix operators have higher precedence than prefix operators]
 NOTEPOSITIONOFOPERATORANDWHATITMEANS
◦ ++a
◦ –a
◦ a++
◦ a–
◦ ++a–
a is incremented BEFORE a is evaluated in the expression
a is decremented BEFORE a is evaluated in the expression
 a is incremented AFTER a is evaluated in the expression
 a is decremented AFTER a is evaluated in the expression
most compilers will accept, but behavior is undefined/inexact (pick a compiler and roll the dice.)
In both examples below, the final value of a is 2
int main() int main() {{
int a = 1;
printf (“ a is %d”, ++a); return 0;
int a = 1;
printf (“ a is %d”, a++); return 0;
}}
/* 2 will be printed */ /* 1 will be printed */
J. E. Jones

OSU CSE 2421
 What values occur here? ◦ If a=4, c=4, b=3,
◦ z=(a++>c)||(++b<=a) J. E. Jones OSU CSE 2421  What values occur here? ◦ If a=4, c=4, b=3, ◦ z=(a++>c)||(++b<=a) ◦ z=(4>4)||(4<=5)  (4 > 4) evaluates to 0 (or false), a increments to 5 after evaluation
 b increments to 4 before evaluation, (4 <= 5) evaluates to 1 (or true) J. E. Jones OSU CSE 2421  What values occur here? ◦ If a=4, c=4, b=3, ◦ z=(a++>c)||(++b<=a) ◦ z=(4>4)||(4<=5)  (4 > 4) evaluates to 0 (or false), a increments to 5 after evaluation
 b increments to 4 before evaluation, (4 <= 5) evaluates to 1 (or true) ◦ z = 0 || 1 J. E. Jones OSU CSE 2421  What values occur here? ◦ If a=4, c=4, b=3, ◦ z=(a++>c)||(++b<=a) ◦ z=(4>4)||(4<=5)  (4 > 4) evaluates to 0 (or false), a increments to 5 after evaluation
 b increments to 4 before evaluation, (4 <= 5) evaluates to 1 (or true) ◦ z = 0 || 1 ◦ z=1,a=5,c=4,b=4 J. E. Jones OSU CSE 2421  What values occur here? ◦ If a=4, c=4, b=3, ◦ z=(a++<=c)||(++b<=a) ◦ z=(4<=4)||(4<=5)  (4 > 4) evaluates to 1 (or true), a increments to 5 after evaluation  2nd expression doesn’t execute because of short circuit
◦ z = 1 || unknown (don’t care)
◦ z=1,a=5,c=4,b=3
J. E. Jones

Leave a Reply

Your email address will not be published. Required fields are marked *