0465-0467.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 412 行
HTML
412 行
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:C and C++ Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=23 //-->
<!-- PAGES=0455-0486 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0461-0464.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0468-0471.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-465"><P>Page 465</P></A>
<P>a, the shorthand a += expr is the same as a=a+expr. The expression can be as complex or
as simple as required.
</P>
<P>
<CENTER>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
NOTE
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Most UNIX functions take advantage of the truth values and return
0 for success. This enables a programmer to write code such as
<!-- CODE //-->
<PRE>
if (function())
{
error condition
}
</PRE>
<!-- END CODE //-->
<BR>The return value of a function determines whether the function
worked.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
<H4>
Bitwise Operations
</H4>
<P>Because a variable is just a string of bits, many operations work on those bit patterns.
Table 23.6 lists the bit operators.
</P>
<P>Table 23.6. Bit operators.
</P>
<HR>
<TABLE WIDTH="360">
<TR><TD>
Operator
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
&
</TD><TD>
Bitwise AND
</TD></TR>
<TR><TD>
|
</TD><TD>
Bitwise OR
</TD></TR>
<TR><TD>
~
</TD><TD>
Negation (one's complement)
</TD></TR>
<TR><TD>
<<
</TD><TD>
Bit shift left
</TD></TR>
<TR><TD>
>>
</TD><TD>
Bit shift right
</TD></TR>
</TABLE>
<P>A bitwise AND compares the individual bits in place. If both are
1, the value 1 is assigned to the expression. Otherwise,
0 is assigned. For a logical OR, 1 is assigned if either value is a
1. Bit shift operations move the bits a number of positions to the right or left. Mathematically, this is
the same as multiplying or dividing by 2, but circumstances exist where the bit shift is preferred.
</P>
<P>Bit operations are often used for masking values and for comparisons. A simple way to
determine whether a value is odd or even is to perform a bitwise
AND with the integer value 1. If it is true, the number
is odd.
</P>
<H4><A NAME="ch23_ 11">
Statement Controls
</A></H4>
<P>With what you've seen so far, you can create a list of statements that are executed only
once, after which the program terminates. To control the flow of commands, three types of
loops exist in C. The simplest is the while loop. The syntax is
</P>
<!-- CODE SNIP //-->
<PRE>
while (expression)
statement
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-466"><P>Page 466</P></A>
<P>As long as the expression between the parentheses evaluates as nonzero—or
true in C—the statement is executed.
statement actually can be a list of statements blocked off with curly
braces. If the expression evaluates to zero the first time it is reached, the statement is never
executed. To force at least one execution of the statement, use a
do loop. The syntax for a do loop is
</P>
<!-- CODE SNIP //-->
<PRE>
do
statement
while (expression);
</PRE>
<!-- END CODE SNIP //-->
<P>The third type of control flow is the for loop. This is more complicated. The syntax is
</P>
<!-- CODE SNIP //-->
<PRE>
for(expr1;expr2;expr3) statement
</PRE>
<!-- END CODE SNIP //-->
<P>When this expression is reached for the first time,
expr1 is evaluated, and then expr2 is evaluated. If
expr2 is nonzero, statement is executed, followed by
expr3. Then expr2 is tested again, followed by the statement and
expr3, until expr2 evaluates to zero. Strictly speaking, this is
a notational convenience because a while loop can be structured to perform the same actions,
as in the following:
</P>
<!-- CODE SNIP //-->
<PRE>
expr1;
while (expr2) {
statement;
expr3
}
</PRE>
<!-- END CODE SNIP //-->
<P>Loops can be interrupted in three ways. A break statement terminates execution
in a loop and exits it. continue terminates the current iteration and retests the loop before possibly
reexecuting the statement. For an unconventional exit, you can
use goto. goto changes the program's execution to a labeled statement. According to many programmers,
goto is poor programming practice, and you should avoid using
it.
</P>
<P>Statements can also be executed conditionally. Again, there are three different formats for
statement execution. The simplest is an if statement. The syntax is
</P>
<!-- CODE SNIP //-->
<PRE>
if (expr) statement
</PRE>
<!-- END CODE SNIP //-->
<P>If the expression expr evaluates to nonzero,
statement is executed. You can expand this with an
else, the second type of conditional execution. The syntax for
else is
</P>
<!-- CODE SNIP //-->
<PRE>
if (expr) statement else statement
</PRE>
<!-- END CODE SNIP //-->
<P>If the expression evaluates to zero, the second statement is executed.
</P>
<CENTER>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
NOTE
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The second statement in an else condition can be another
if statement. This situation might cause the grammar to be indeterminate if the structure
<!-- CODE SNIP //-->
<PRE>
if (expr) if (expr) statement else statement
</PRE>
<!-- END CODE SNIP //-->
<BR>is not parsed cleanly.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
<A NAME="PAGENUM-467"><P>Page 467</P></A>
<TABLE BGCOLOR=#FFFF99><TR><TD></TD></TR><TR><TD><BLOCKQUOTE>
As the code is written, the else is considered applicable to the second
if. To make it applicable with the first if, surround the second
if statement with curly braces, as in the following:
if (expr) {if (expr) statement} else statement
</BLOCKQUOTE></TD></TR></TABLE>
<P>The third type of conditional execution is more complicated. The
switch statement first evaluates an expression. Then it looks down a series
of case statements to find a label that matches the expression's value and executes the statements following the label. A special label
default exists if no other conditions are met. If you want only a set of statements executed for
each label, you must use the break statement to leave the
switch statement.
</P>
<P>This covers the simplest building blocks of a C program. You can add more power by
using functions and by declaring complex datatypes.
</P>
<P>If your program requires different pieces of data to be grouped on a consistent basis, you
can group them into structures. Listing 23.3 shows a structure for a California driver's license.
Note that it includes integer, character, and character array (string) types.
</P>
<P>Listing 23.3. An example of a structure.
</P>
<!-- CODE //-->
<PRE>
struct license {
char name[128];
char address[3][128];
int zipcode;
int height, weight, month, day, year;
char license_letter;
int license_number;
};
struct license newlicensee;
struct license *user;
</PRE>
<!-- END CODE //-->
<P>Because California driver's license numbers consist of a single character followed by a
seven- digit number, the license ID is broken into two components. Similarly, the new licensee's
address is broken into three lines, represented by three arrays of 128 characters.
</P>
<P>Accessing individual fields of a structure requires two different techniques. To read a
member of a locally defined structure, you append a dot to the variable and then the field name, as
in the following example:
</P>
<!-- CODE SNIP //-->
<PRE>
newlicensee.zipcode=94404;
</PRE>
<!-- END CODE SNIP //-->
<P>When using a pointer to a structure, you need
-> to point to the member (to reference the individual members):
</P>
<!-- CODE SNIP //-->
<PRE>
user->zipcode=94404;
</PRE>
<!-- END CODE SNIP //-->
<P><CENTER>
<a href="0461-0464.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0468-0471.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?