📄 unx17.htm
字号:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx17.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unxpt4au.htm">
<LINK REL="Previous" HREF="unx16.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx16.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unxpt4au.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<UL>
<LI>
<A HREF="#I1">17 — The C Programming Language</A></LI>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I3">By James Armstrong</A></LI></UL></UL>
<LI>
<A HREF="#I4">The History of C</A></LI>
<LI>
<A HREF="#I5">Creating, Compiling, and Executing Your First Program</A></LI>
<LI>
<A HREF="#I6">An Overview of the C Language</A></LI>
<UL>
<LI>
<A HREF="#I7">Elementary C Syntax</A></LI>
<LI>
<A HREF="#I8">Expressions</A></LI>
<UL>
<LI>
<A HREF="#I9">Comparison Expressions</A></LI>
<LI>
<A HREF="#I10">Mathematical Expressions</A></LI>
<LI>
<A HREF="#I11">Bitwise Operations</A></LI></UL>
<LI>
<A HREF="#I12">Statement Controls</A></LI></UL>
<LI>
<A HREF="#I13">Creating a Simple Program</A></LI>
<UL>
<LI>
<A HREF="#I14">Writing the Code</A></LI>
<LI>
<A HREF="#I15">Compiling the Program</A></LI>
<LI>
<A HREF="#I16">Executing the Program</A></LI></UL>
<LI>
<A HREF="#I17">Building Large Applications</A></LI>
<UL>
<LI>
<A HREF="#I18">Making Libraries with ar</A></LI>
<LI>
<A HREF="#I19">Building Large Applications with make</A></LI></UL>
<LI>
<A HREF="#I20">Debugging Tools</A></LI>
<LI>
<A HREF="#I21">Summary</A></LI></UL></UL></UL>
<H2 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>17 — The C Programming Language</B>
<BR></FONT></A></CENTER></H2>
<H5 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=3><B>By James Armstrong</B>
<BR></FONT></A></CENTER></H5>
<P>C is the programming language most frequently associated with UNIX. Since the 1970s, the bulk of the operating system and applications have been written in C. This is one of the major reasons why UNIX is a portable operating system.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I4" NAME="I4">
<FONT SIZE=4><B>The History of C</B>
<BR></FONT></A></CENTER></H3>
<P>C was first designed by Dennis Ritchie for use with UNIX on DEC PDP-11 computers. The language evolved from Martin Richard's BCPL, and one of its earlier forms was the B language, which was written by Ken Thompson for the DEC PDP-7. The first book on C
was The C Programming Language by Brian Kernighan and Dennis Ritchie, published in 1978.
<BR></P>
<P>In 1983, the American National Standards Institute established a committee to standardize the definition of C. Termed ANSI C, it is the recognized standard for the language grammar and a core set of libraries. The syntax is slightly different from the
original C language, which is frequently called K&R—for Kernighan and Ritchie.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=4><B>Creating, Compiling, and Executing Your First Program</B>
<BR></FONT></A></CENTER></H3>
<P>The development of a C program is an iterative procedure. Many UNIX tools are involved in this four-step process. They are familiar to software developers:
<BR></P>
<OL>
<LI>Using an editor, write the code into a text file.
<BR>
<BR></LI>
<LI>Compile the program.
<BR>
<BR></LI>
<LI>Execute the program.
<BR>
<BR></LI>
<LI>Debug the program.
<BR>
<BR></LI></OL>
<P>The first two steps are repeated until the program compiles successfully. Then the execution and debugging begin. Many of the concepts presented may seem strange to non-programmers. This chapter endeavors to introduce C as a programming language.
<BR></P>
<P>The typical first C program is almost a cliché. It is the "Hello, World" program, and it prints the simple line Hello, World. Listing 17.1 is the source of the program.
<BR></P>
<UL>
<LH><B>Listing 17.1. Source of </B><B>Hello World</B><B>.</B></LH></UL>
<PRE>main()
{
printf("Hello, World\n");
}</PRE>
<P>This program can be compiled and executed as follows:
<BR></P>
<PRE>$ cc hello.c
$ a.out
Hello, World
$</PRE>
<P>The program is compiled with the cc command, which creates a program a.out if the code is correct. Just typing a.out will run the program. The program includes only one function, main. Every C program must have a main function; it is where the program's
execution begins. The only statement is a call to the printf library function, which passes the string Hello, World\n. (Functions are described in detail later in this chapter.) The last two characters of the string, \n, represent the carriage return-line
feed character.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=4><B>An Overview of the C Language</B>
<BR></FONT></A></CENTER></H3>
<P>As with all programming languages, C programs must follow rules. These rules describe how a program should appear, and what those words and symbols mean. This is the syntax of a programming language. Think of a program as a story. Each sentence must
have a noun and a verb. Sentences form paragraphs, and the paragraphs tell the story. Similarly, C statements can build into functions and programs.
<BR></P>
<P>For more information about programming in C, I recommend the following books from Sams Publishing:
<BR></P>
<UL>
<LI>Teach Yourself C in 21 Days by Peter Aitken and Bradley Jones
<BR>
<BR></LI>
<LI>Programming in ANSI C by Stephen G. Kochan
<BR>
<BR></LI></UL>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Elementary C Syntax</B>
<BR></FONT></A></CENTER></H4>
<P>Like all languages, C deals primarily with the manipulation and presentation of data. BCPL deals with data as data. C, however, goes one step further to use the concept of data types. The basic data types are character, integer, and floating point
numbers. Other data types are built from these three basic types.
<BR></P>
<P>Integers are the basic mathematical data type. They can be classified as long and short integers, and the size is implementation-dependent. With a few exceptions, integers are four bytes in length, and they can range from 2,147,483,648 to 2,147,483,647.
In ANSI C, these values are defined in a header—limit.h—as INT_MIN and INT_MAX. The qualifier unsigned moves the range one bit higher, to the equivalent of INT_MAX-INT_MIN.
<BR></P>
<P>Floating point numbers are used for more complicated mathematics. Integer mathematics is limited to integer results. With integers, 3/2 equals 1. Floating point numbers give a greater amount of precision to mathematical calculations: 3/2 equals 1.5.
Floating point numbers can be represented by a decimal number, such as 687.534, or with scientific notation: 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.
<BR></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, and is found on most U.S. computers.
<BR></P>
<P>An array is used for a sequence of values that are often position-dependent. An array is useful when a range of values of a given type is needed. 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 the data of an array 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 when the value pointed at is required. Here are some sample declarations:
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>int i;</P>
<TD>
<P>Declares an integer</P>
<TR>
<TD>
<P>char c;</P>
<TD>
<P>Declares a character</P>
<TR>
<TD>
<P>char *ptr;</P>
<TD>
<P>Declares a pointer to a character</P>
<TR>
<TD>
<P>double temp[16];</P>
<TD>
<P>Declares an array of double-precision floating point numbers with 16 values</P></TABLE>
<P>Listing 17.2 shows an example of a program with pointers.
<BR></P>
<UL>
<LH><B>Listing 17.2. An example of a program with pointers.</B></LH></UL>
<PRE>int i;
int *ptr;
i=5;
ptr = &i;
printf("%d %x %d\n", i,ptr,*ptr);
output is: 5 f7fffa6c 5</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> A pointer is just a memory address and will tell you the address of any variable.
<BR></NOTE>
<HR ALIGN=CENTER>
<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.
<BR></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 17.1. Format options can be modified with sizes. Check the documentation for
the full specification.
<BR></P>
<UL>
<LH><B>Table 17.1. Format conversions for </B><B>printf</B><B>.</B>
<BR></LH></UL>
<TABLE BORDER>
<TR>
<TD>
<PRE><I>Conversion</I>
<BR></PRE>
<TD>
<PRE><I>Meaning</I>
<BR></PRE>
<TR>
<TD>
<P>%%</P>
<TD>
<P>Percentage sign</P>
<TR>
<TD>
<P>%E</P>
<TD>
<P>Double (scientific notation)</P>
<TR>
<TD>
<P>%G</P>
<TD>
<P>Double (format depends on value)</P>
<TR>
<TD>
<P>%X</P>
<TD>
<P>Hexadecimal (letters are capitalized)</P>
<TR>
<TD>
<P>%c</P>
<TD>
<P>Single character</P>
<TR>
<TD>
<P>%d</P>
<TD>
<P>Integer</P>
<TR>
<TD>
<P>%e</P>
<TD>
<P>Double (scientific notation)</P>
<TR>
<TD>
<P>%f</P>
<TD>
<P>Double of the form mmm.ddd</P>
<TR>
<TD>
<P>%g</P>
<TD>
<P>Double (format depends on value)</P>
<TR>
<TD>
<P>%i</P>
<TD>
<P>Integer</P>
<TR>
<TD>
<P>%ld</P>
<TD>
<P>Long integer</P>
<TR>
<TD>
<P>%n</P>
<TD>
<P>Count of characters written in current printf</P>
<TR>
<TD>
<P>%o</P>
<TD>
<P>Octal</P>
<TR>
<TD>
<P>%p</P>
<TD>
<P>Print as a pointer</P>
<TR>
<TD>
<P>%s</P>
<TD>
<P>Character pointer (string)</P>
<TR>
<TD>
<P>%u</P>
<TD>
<P>Unsigned integer</P>
<TR>
<TD>
<P>%x</P>
<TD>
<P>Hexadecimal</P></TABLE>
<P>Some characters cannot be included easily in a program. New lines, for example, require a special escape sequence, because there cannot be an unescaped newline in a string. Table 17.2 contains a complete list of escape sequences.
<BR></P>
<UL>
<LH><B>Table 17.2. Escape characters for strings.</B>
<BR></LH></UL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -