⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx17.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<TABLE BORDER>

<TR>

<TD>

<PRE><I>Escape Sequence</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TR>

<TD>

<P>\&quot;</P>

<TD>

<P>Double quote</P>

<TR>

<TD>

<P>\'</P>

<TD>

<P>Single quote</P>

<TR>

<TD>

<P>\?</P>

<TD>

<P>Question mark</P>

<TR>

<TD>

<P>\\</P>

<TD>

<P>Backslash</P>

<TR>

<TD>

<P>\a</P>

<TD>

<P>Audible bell</P>

<TR>

<TD>

<P>\b</P>

<TD>

<P>Backspace</P>

<TR>

<TD>

<P>\f</P>

<TD>

<P>Form feed (new page)</P>

<TR>

<TD>

<P>\n</P>

<TD>

<P>New line</P>

<TR>

<TD>

<P>\ooo</P>

<TD>

<P>Octal number</P>

<TR>

<TD>

<P>\r</P>

<TD>

<P>Carriage return</P>

<TR>

<TD>

<P>\t</P>

<TD>

<P>Horizontal tab</P>

<TR>

<TD>

<P>\v</P>

<TD>

<P>Vertical tab</P>

<TR>

<TD>

<P>\xhh</P>

<TD>

<P>Hexadecimal number</P></TABLE>

<P>A full program is compilation of statements. Statements are separated by semicolons. They can be grouped in blocks of statements surrounded by curly braces. The simplest statement is an assignment. A variable on the left side is assigned the value of an 

expression on the right.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I8" NAME="I8">

<FONT SIZE=3><B>Expressions</B>

<BR></FONT></A></CENTER></H4>

<P>At the heart of the C programming language are expressions. These are techniques to combine simple values into new values. There are three basic types of expressions: comparison, numerical, and bitwise expressions.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=3><B>Comparison Expressions</B>

<BR></FONT></A></CENTER></H5>

<P>The simplest expression is a comparison. A comparison evaluates to a TRUE or a FALSE value. In C, TRUE is a non-zero value, and FALSE is a zero value. Table 17.3 contains a list of comparison operators.

<BR></P>

<UL>

<LH><B>Table 17.3. Comparison operators.</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Operator</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TD>

<PRE><I>Operator</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TR>

<TD>

<P>&lt;</P>

<TD>

<P>Less than</P>

<TD>

<P>&gt;=</P>

<TD>

<P>Greater than or equal to</P>

<TR>

<TD>

<P>&gt;</P>

<TD>

<P>Greater than</P>

<TD>

<P>||</P>

<TD>

<P>Or</P>

<TR>

<TD>

<P>==</P>

<TD>

<P>Equal to</P>

<TD>

<P>&amp;&amp;</P>

<TD>

<P>And</P>

<TR>

<TD>

<P>&lt;=</P>

<TD>

<P>Less than or equal to</P>

<TD><BR>

<TD><BR></TABLE>

<P>Expressions can be built by combining simple comparisons with ANDs and ORs to make complex expressions. Consider the definition of a leap year. In words, it is any year divisible by 4, except a year divisible by 100 unless that year is divisible by 400. 

If year is the variable, a leap year can be defined with this expression.

<BR></P>

<PRE>((((year%4)==0)&amp;&amp;((year%100)!=0))||((year%400)==0))</PRE>

<P>On first inspection, this code might look complicated, but it isn't. The parentheses group the simple expressions with the ANDs and ORs to make a complex expression.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=3><B>Mathematical Expressions</B>

<BR></FONT></A></CENTER></H5>

<P>One convenient aspect of C is that expressions can be treated as mathematical values, and mathematical statements can be used in expressions. In fact, any statement&#151;even a simple assignment&#151;has values that can be used in other places as an 
expression.

<BR></P>

<P>The mathematics of C is straightforward. Barring parenthetical groupings, multiplication and division have higher precedence than addition and subtraction. The operators are standard. They are listed in Table 17.4.

<BR></P>

<UL>

<LH><B>Table 17.4. Mathematical operators.</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Operator</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TD>

<PRE><I>Operator</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TR>

<TD>

<P>+</P>

<TD>

<P>Addition</P>

<TD>

<P>/</P>

<TD>

<P>Division</P>

<TR>

<TD>

<P>-</P>

<TD>

<P>Subtraction</P>

<TD>

<P>%</P>

<TD>

<P>Integer remainder</P>

<TR>

<TD>

<P>*</P>

<TD>

<P>Multiplication</P>

<TD>

<P>^</P>

<TD>

<P>Exponentiation</P></TABLE>

<P>There are also unary operators, which effect a single variable. These are ++ (increment by one) and &#151; (decrement by one). These shorthand versions are quite useful.

<BR></P>

<P>There are also shorthands for situations in which you want to change the value of a variable. For example, if you want to add an expression to a variable called a and assign the new value to a, the shorthand a+=expr is the same as a=a+expr. The 
expression can be as complex or as simple as required.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Most UNIX functions take advantage of the truth values and return 0 for success. This enables a programmer to write code such as

<BR>

<BR>if (function())

<BR>        {

<BR>        error condition

<BR>        }

<BR>

<BR>The return value of a function determines whether the function worked.

<BR></NOTE>

<HR ALIGN=CENTER>

<H5 ALIGN="CENTER">

<CENTER><A ID="I11" NAME="I11">

<FONT SIZE=3><B>Bitwise Operations</B>

<BR></FONT></A></CENTER></H5>

<P>Because a variable is just a string of bits, many operations work on those bit patterns. Table 17.5 lists the bit operators.

<BR></P>

<UL>

<LH><B>Table 17.5. Bit operators.</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Operator</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TD>

<PRE><I>Operator</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TR>

<TD>

<P>&amp;</P>

<TD>

<P>Logical AND</P>

<TD>

<P>&lt;&lt;</P>

<TD>

<P>Bit shift left</P>

<TR>

<TD>

<P>|</P>

<TD>

<P>Logical OR</P>

<TD>

<P>&gt;&gt;</P>

<TD>

<P>Bit shift right</P></TABLE>

<P>A logical 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.

<BR></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 logical AND with the integer value 1. If it is TRUE, the number is odd.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I12" NAME="I12">

<FONT SIZE=3><B>Statement Controls</B>

<BR></FONT></A></CENTER></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

<BR></P>

<PRE>while (expression)

       statement</PRE>

<P>So long as the expression between parentheses evaluates as non-zero&#151;or TRUE in C&#151;the statement is executed. The 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

<BR></P>

<PRE>do

        statement

        while (expression);</PRE>

<P>The third type of control flow is the for loop. This is more complicated. The syntax is

<BR></P>

<PRE>for(expr1;expr2;expr3) statement</PRE>

<P>When the expression is reached for the first time, expr1 is evaluated. Next, expr2 is evaluated. If expr2 is non-zero, the 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, for a while loop can be structured to perform the same actions. For example,

<BR></P>

<PRE>expr1;

while (expr2) {

        statement;

        expr3

        }</PRE>

<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 re-executing the statement. For an unconventional exit, you can use 
goto. goto changes the program's execution to a labelled statement. According to many programmers, goto is poor programming practice, and you should avoid using it.

<BR></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

<BR></P>

<PRE>if (expr) statement</PRE>

<P>If the expression expr evaluates to non-zero, the statement is executed. You can expand this with an else, the second type of conditional execution. The syntax for else is

<BR></P>

<PRE>if (expr) statement else statement</PRE>

<P>If the expression evaluates to zero, the second statement is executed.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -