0461-0464.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 637 行
HTML
637 行
<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="0458-0460.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0465-0467.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-461"><P>Page 461</P></A>
<P>numbers give a greater amount of precision to mathematical calculations than integers
can; with floating-point numbers, 3/2 equals 1.5. Floating-point numbers can be represented by
a decimal number, such as 687.534, or with scientific notation, such as 8.87534E+2. For
larger numbers, scientific notation is preferred. For even greater precision, the type
double provides a greater range. Again, specific ranges are implementation dependent.
</P>
<P>Characters are usually implemented as single bytes, although some international character
sets require two bytes. One common set of character representations is ASCII, which is found
on most U.S. computers.
</P>
<P>You use arrays for sequences of values that are often position dependent. An array is
particularly useful when you need a range of values of a given type. Related to the array is the
pointer. Variables are stored in memory, and a
pointer is the physical address of that memory. In a
sense, a pointer and an array are similar, except when a program is invoked. The space needed for
an array's data is allocated when the routine that needs the space is invoked. For a pointer,
the space must be allocated by the programmer, or the variable must be assigned by
dereferencing a variable. The ampersand (&) is used to indicate dereferencing, and an
asterisk (*) is used to indicate when the value pointed at is required. Here are some sample declarations:
</P>
<!-- CODE //-->
<PRE>
int i; Declares an integer
char c; Declares a character
char *ptr; Declares a pointer to a character
double temp[16]; Declares an array of double-precision floating-point
numbers with 16 values
</PRE>
<!-- END CODE //-->
<P>Listing 23.2 shows an example of a program with pointers.
</P>
<P>Listing 23.2. An example of a program with pointers.
</P>
<!-- CODE //-->
<PRE>
int i;
int *ptr;
i=5;
ptr = &i;
printf("%d %x %d\n", i,ptr,*ptr);
</PRE>
<!-- END CODE //-->
<P>The output of this program is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
5 f7fffa6c 5
</PRE>
<!-- END CODE SNIP //-->
<CENTER>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
NOTE
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The middle value, f7fffa6c, is an address. This might be different on your system
because the pointer variable will change from version to version of the libraries.
A pointer is just a memory address and will tell you the address of any variable.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
<A NAME="PAGENUM-462"><P>Page 462</P></A>
<P>There is no specific type for a string. An array of characters is used to
represent strings. They can be printed using an %s flag, instead of
%c.
</P>
<P>Simple output is created by the printf function.
printf takes a format string and the list of arguments to be printed. A complete set of format options is presented in Table 23.2.
Format options can be modified with sizes. Check the
gcc documentation (man page or info file) for the full specification.
</P>
<P>Table 23.2. Format conversions for printf.
</P>
<HR>
<TABLE WIDTH="360">
<TR><TD>
Conversion
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
%%
</TD><TD>
Percentage sign
</TD></TR>
<TR><TD>
%E
</TD><TD>
Double (scientific notation)
</TD></TR>
<TR><TD>
%G
</TD><TD>
Double (format depends on value)
</TD></TR>
<TR><TD>
%X
</TD><TD>
Hexadecimal (letters are capitalized)
</TD></TR>
<TR><TD>
%c
</TD><TD>
Single character
</TD></TR>
<TR><TD>
%d
</TD><TD>
Integer
</TD></TR>
<TR><TD>
%e
</TD><TD>
Double (scientific notation)
</TD></TR>
<TR><TD>
%f
</TD><TD>
Double of the form mmm.ddd
</TD></TR>
<TR><TD>
%g
</TD><TD>
Double (format depends on value)
</TD></TR>
<TR><TD>
%i
</TD><TD>
Integer
</TD></TR>
<TR><TD>
%ld
</TD><TD>
Long integer
</TD></TR>
<TR><TD>
%n
</TD><TD>
Count of characters written in current printf
</TD></TR>
<TR><TD>
%o
</TD><TD>
Octal
</TD></TR>
<TR><TD>
%p
</TD><TD>
Print as a pointer
</TD></TR>
<TR><TD>
%s
</TD><TD>
Character pointer (string)
</TD></TR>
<TR><TD>
%u
</TD><TD>
Unsigned integer
</TD></TR>
<TR><TD>
%x
</TD><TD>
Hexadecimal
</TD></TR>
</TABLE>
<P>Some characters cannot be included easily in a program. Newlines, for example, require a
special escape sequence because there cannot be an unescaped newline in a string. Table
23.3 contains a complete list of escape sequences.
</P>
<P>Table 23.3. Escape characters for strings.
</P>
<HR>
<TABLE WIDTH="360">
<TR><TD>
Escape Sequence
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
\"
</TD><TD>
Double quote
</TD></TR>
<TR><TD>
\'
</TD><TD>
Single quote
</TD></TR>
</TABLE>
<A NAME="PAGENUM-463"><P>Page 463</P></A>
<TABLE WIDTH="360">
<TR><TD>
Escape Sequence
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
\?
</TD><TD>
Question mark
</TD></TR>
<TR><TD>
\\
</TD><TD>
Backslash
</TD></TR>
<TR><TD>
\a
</TD><TD>
Audible bell
</TD></TR>
<TR><TD>
\b
</TD><TD>
Backspace
</TD></TR>
<TR><TD>
\f
</TD><TD>
Form feed (new page)
</TD></TR>
<TR><TD>
\n
</TD><TD>
Newline
</TD></TR>
<TR><TD>
\ooo
</TD><TD>
Octal number
</TD></TR>
<TR><TD>
\r
</TD><TD>
Carriage return
</TD></TR>
<TR><TD>
\t
</TD><TD>
Horizontal tab
</TD></TR>
<TR><TD>
\v
</TD><TD>
Vertical tab
</TD></TR>
<TR><TD>
\xhh
</TD><TD>
Hexadecimal number
</TD></TR>
</TABLE>
<P>A full program is a compilation of statements. Statements are separated by semicolons,
and they can be grouped in blocks of statements surrounded by curly braces. The simplest
statement is an assignment, in which a variable on the left side is assigned the value of an
expression on the right.
</P>
<H4><A NAME="ch23_ 8">
Expressions
</A></H4>
<P>At the heart of the C programming language are
expressions. These are techniques to combine simple values into new values. The three basic types of expressions are comparison,
numerical, and bitwise.
</P>
<H4><A NAME="ch23_ 9">
Comarison Expressions
</A></H4>
<P>The simplest expression is a comparison. A comparison evaluates to a
true or a false value. In C, true is a nonzero value, and
false is a zero value. Table 23.4 contains a list of
comparison operators.
</P>
<P>Table 23.4. Comparison operators.
</P>
<HR>
<TABLE WIDTH="360">
<TR><TD>
Operator
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
<
</TD><TD>
Less than
</TD></TR>
<TR><TD>
>
</TD><TD>
Greater than
</TD></TR>
<TR><TD>
==
</TD><TD>
Equal to
</TD></TR>
<TR><TD>
<=
</TD><TD>
Less than or equal to
</TD></TR>
<TR><TD>
>=
</TD><TD>
Greater than or equal to
</TD></TR>
</TABLE>
<!-- CODE SNIP //-->
<PRE>
continues
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-464"><P>Page 464</P></A>
<P>Table 23.4. continued
</P>
<HR>
<TABLE WIDTH="360">
<TR><TD>
Operator
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
||
</TD><TD>
Logical OR
</TD></TR>
<TR><TD>
&&
</TD><TD>
Logical AND
</TD></TR>
<TR><TD>
!
</TD><TD>
Logical NOT
</TD></TR>
</TABLE>
<P>You can combine simple comparisons with ANDs and
ORs to make complex expressions. For example, 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. Using
year as the variable, you can define a leap year with the following expression:
</P>
<!-- CODE SNIP //-->
<PRE>
((((year%4)==0)&&((year%100)!=0))||((year%400)==0))
</PRE>
<!-- END CODE SNIP //-->
<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.
</P>
<H4><A NAME="ch23_ 10">
Mathematical Expressions
</A></H4>
<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—even a
simple assignment—has values that can be used in other places as an expression.
</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
and are listed in Table 23.5.
</P>
<P>Table 23.5. Mathematical operators.
</P>
<HR>
<TABLE WIDTH="360">
<TR><TD>
Operator
</TD><TD>
Meaning
</TD></TR>
<TR><TD>
+
</TD><TD>
Addition
</TD></TR>
<TR><TD>
-
</TD><TD>
Subtraction
</TD></TR>
<TR><TD>
*
</TD><TD>
Multiplication
</TD></TR>
<TR><TD>
/
</TD><TD>
Division
</TD></TR>
<TR><TD>
%
</TD><TD>
Integer remainder
</TD></TR>
</TABLE>
<P>There are also unary operators, which affect a single variable. These are
++ (increment by one) and -- (decrement by one). These are shorthand for
var = var + 1 and var = var - 1, respectively.
</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 a new value to
</P>
<P><CENTER>
<a href="0458-0460.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0465-0467.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?