📄 chapter 5 expressions -- valvano.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm -->
<HTML><HEAD><TITLE>Chapter 5: Expressions -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META
content="Power HD:Applications:Microsoft Office 98:Templates:Web Pages:Blank Web Page"
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT
face=Helvetica,Arial size=4>Chapter 5: Expressions </FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 5?</FONT></I></B></P>
<DIR>
<P><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#PA">Precedence
and associativity</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#UNARY">Unary
</A><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#UNARY">operators</A><FONT
face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#BINARY">Binary</A><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#UNARY">
operators</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#ASSIGNMENT">Assignment
operators</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#CASTS">Expression
type and explicit cast</A><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#ASSIGNMENT">ing</A><FONT
face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#SELECTION">Selection
operator</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#OVERFLOW">Arithmetic
overflow and underflow</A></P></DIR>
<P><FONT face="Times New Roman,Times">Most programming languages support the
traditional concept of an expression as a combination of constants, variables,
array elements, and function calls joined by various operators (+, -, etc.) to
produce a single numeric value. Each operator is applied to one or two operands
(the values operated on) to produce a single value which may itself be an
operand for another operator. This idea is generalized in C by including
nontraditional data types and a rich set of operators. Pointers, unsubscripted
array names, and function names are allowed as operands. And, as Tables 5-1
through 5-6 illustrate, many operators are available. All of these operators can
be combined in any useful manner in an expression. As a result, C allows the
writing very compact and efficient expressions which at first glance may seem a
bit strange. Another unusual feature of C is that anywhere the syntax calls for
an expression, a list of expressions, with comma separators, may appear.
</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=PA></A>Precedence and
associativity</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The basic problem in evaluating
expressions is deciding which parts of an expression are to be associated with
which operators. To eliminate ambiguity, operators are given three properties:
<I>operand count</I>, <I>precedence</I>, and <I>associativity</I>. </FONT></P>
<P><FONT face="Times New Roman,Times">Operand count refers to the classification
of operators as unary, binary, or ternary according to whether they operate on
one, two, or three operands. The unary minus sign, for instance, reverses the
sign of the following operand, whereas the binary minus sign subtracts one
operand from another. </FONT></P>
<P><FONT face="Times New Roman,Times">The following example converts the
distance x in inches to a distance y in cm. Without parentheses the following
statement seems ambiguous</FONT></P>
<DIR>
<P><CODE>y=254*x/100;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">If we divide first, then y can only take
on values that are multiples of 254 (e.g., 0 254 508 etc.) So the following
statement is incorrect.</FONT></P>
<DIR>
<P><CODE>y=254*(x/100);</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The proper approach is to multiply first
then divide. To multiply first we must guarantee that the product<B> 254*x</B>
will not overflow the precision of the computer. How do we know what precision
the compiler used for the intermediate result <B>254*x</B>? To answer this
question, we must observe the assembly code generated by the compiler. Since
multiplication and division associate left to right, the first statement without
parentheses although ambiguous will actually calculate the correct answer. It is
good programming style to use parentheses to clarify the expression. So this
last statement has both good style and proper calculation.</FONT></P>
<DIR>
<P><CODE>y=(254*x)/100;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The issues of <A
href="http://www.ece.utexas.edu/~valvano/embed/chap1/chap1.htm#PRECEDENCE">precedence
and associativity</A> were explained in Chapter 1. Precedence defines the
evaluation order. For example the expression </FONT><FONT face=Monaco>3+4*2
</FONT><FONT face="Times New Roman,Times">will be 11 because multiplication as
precedence over addition. Associativity determines the order of execution for
operators that have the same precedence. For example, the expression
</FONT><FONT face=Monaco>10-3-2 </FONT><FONT face="Times New Roman,Times">will
be 5, because subtraction associates left to right. On the other hand, if x and
y are initially 10, then the expression </FONT><FONT
face=Monaco>x+=y+=1</FONT><FONT face="Times New Roman,Times"> will first make
</FONT><FONT face=Monaco>y=y+1</FONT><FONT face="Times New Roman,Times"> (11),
then make </FONT><FONT face=Monaco>x=x+y</FONT><FONT
face="Times New Roman,Times"> (21) because the operator<B> +=</B> associates
right to left. The table from chapter 1 is repeated for your
convenience</FONT>.</P>
<P>
<TABLE cellSpacing=0 border=0>
<TBODY>
<TR>
<TD vAlign=top width="17%">Precedence</TD>
<TD vAlign=top width="55%">Operators</TD>
<TD vAlign=top width="28%">Associativity</TD></TR>
<TR>
<TD vAlign=top width="17%">highest</TD>
<TD vAlign=top width="55%">() <CODE> </CODE>[]<CODE> </CODE>.
<CODE> </CODE>-> <CODE> </CODE>++(postfix) <CODE>
</CODE>--(postfix)</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">++(prefix) <CODE> </CODE>--(prefix)
<CODE> </CODE>!~ <B>sizeof</B>(type)<CODE>
</CODE>+(unary)<CODE> </CODE>-(unary) &(address)<CODE>
</CODE>*(dereference)</TD>
<TD vAlign=top width="28%">right to left</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">*<CODE> </CODE> /
<CODE> </CODE>%</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">+ <CODE> </CODE>-</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%"><< <CODE> </CODE>>></TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">< <CODE> </CODE> <=
<CODE> </CODE>> <CODE> </CODE>>=</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">==<CODE> </CODE>!=</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">&</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">^</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">|</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">&&</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">||</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">? :</TD>
<TD vAlign=top width="28%">right to left</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">= <CODE> </CODE>+=
<CODE> </CODE>-=<CODE> </CODE>*=<CODE>
</CODE>/=<CODE> </CODE>%= <CODE> </CODE><<=<CODE>
</CODE>>>= <CODE> </CODE>|= <CODE> </CODE>&=
<CODE> </CODE>^= </TD>
<TD vAlign=top width="28%">right to left</TD></TR>
<TR>
<TD vAlign=top width="17%">lowest</TD>
<TD vAlign=center width="55%">,</TD>
<TD vAlign=center width="28%">left to right</TD></TR></TBODY></TABLE></P>
<P><I>Table 1-4: Precedence and associativity determine the order of
operation</I></P>
<P><I><B><FONT face=Helvetica,Arial><A name=UNARY></A>Unary
operators</FONT></B></I></P>
<P><FONT face="Times New Roman,Times">We begin with the unary operators, which
take a single input and give a single output. In the following examples, assume
all numbers are 16 bit signed (short). The following variables are
listed</FONT><CODE><BR>short data; /* -32767 to +32767 */ <BR>short
*pt; /* pointer to memory */ <BR>short flag; /* 0 is false, not zero
is true */ </CODE></P>
<P>
<TABLE cellSpacing=0 border=0>
<TBODY>
<TR>
<TD vAlign=top width="5%">
<ADDRESS><CODE>operator</CODE> </ADDRESS></TD>
<TD vAlign=top>
<ADDRESS><CODE>meaning</CODE></ADDRESS></TD>
<TD vAlign=center width="32%"><I>example</I></TD>
<TD vAlign=center width="32%"><I>result</I></TD></TR>
<TR>
<TD vAlign=center width="5%"><CODE>~</CODE></TD>
<TD vAlign=center>binary complement</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>~0x1234 </FONT></TD>
<TD vAlign=center width="32%">0xEDCB</TD></TR>
<TR>
<TD vAlign=top width="5%"><CODE>!</CODE></TD>
<TD vAlign=top>logical complement</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>!flag</FONT></TD>
<TD vAlign=center width="32%">flip 0 to 1 and notzero to 0</TD></TR>
<TR>
<TD vAlign=top width="5%"><CODE>&</CODE></TD>
<TD vAlign=top>address of</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>&data</FONT></TD>
<TD vAlign=center width="32%">address in memory where data is stored</TD></TR>
<TR>
<TD vAlign=top width="5%"><CODE>-</CODE></TD>
<TD vAlign=top>negate</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>-100</FONT></TD>
<TD vAlign=center width="32%">negative 100</TD></TR>
<TR>
<TD vAlign=top width="5%"><CODE>+</CODE></TD>
<TD vAlign=top>positive</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>+100</FONT></TD>
<TD vAlign=center width="32%">100</TD></TR>
<TR>
<TD vAlign=top width="5%"><CODE>++</CODE></TD>
<TD vAlign=top>preincrement</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>++data</FONT> </TD>
<TD vAlign=center width="32%">data=data+1, then result is data</TD></TR>
<TR>
<TD vAlign=top width="5%"><CODE>--</CODE></TD>
<TD vAlign=top>predecrement</TD>
<TD vAlign=center width="32%"><FONT face=Monaco>--data</FONT></TD>
<TD vAlign=center width="32%">data=data-1, then result is data</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -