亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? chapter4.html

?? C programming language. An excellent book on C programming. Recommended to C beginner.
?? HTML
?? 第 1 頁 / 共 4 頁
字號:
<html><head><title>Chapter 4 - Functions and Program Structure</title></head><body><hr><p align="center"><a href="chapter3.html">Back to Chapter 3</a>&nbsp;--&nbsp;<a href="kandr.html">Index</a>&nbsp;--&nbsp;<a href="chapter5.html">Chapter 5</a><p><hr><h1>Chapter 4 - Functions and Program Structure</h1>Functions break large computing tasks into smaller ones, and enable people tobuild on what others have done instead of starting over from scratch.Appropriate functions hide details of operation from parts of the program thatdon't need to know about them, thus clarifying the whole, and easing the painof making changes.<p>C has been designed to make functions efficient and easy to use; C programsgenerally consist of many small functions rather than a few big ones. A programmay reside in one or more source files. Source files may be compiled separatelyand loaded together, along with previously compiled functions from libraries.We will not go into that process here, however, since the details vary fromsystem to system.<p>Function declaration and definition is the area where the ANSI standard hasmade the most changes to C. As we saw first in <a href="chapter1.html">Chapter 1</a>, it isnow possible to declare the type of arguments when a function is declared. Thesyntax of function declaration also changes, so that declarations anddefinitions match. This makes it possible for a compiler to detect many moreerrors than it could before. Furthermore, when arguments are properly declared,appropriate type coercions are performed automatically.<p>The standard clarifies the rules on the scope of names; in particular, itrequires that there be only one definition of each external object.Initialization is more general: automatic arrays and structures may now beinitialized.<p>The C preprocessor has also been enhanced. New preprocessor facilities includea more complete set of conditional compilation directives, a way to createquoted strings from macro arguments, and better control over the macroexpansion process.<h2><a name="s4.1">4.1 Basics of Functions</a></h2>To begin with, let us design and write a program to print each line of itsinput that contains a particular ``pattern'' or string of characters. (This isa special case of the UNIX program <tt>grep</tt>.) For example, searching for thepattern of letters ``<tt>ould</tt>'' in the set of lines<pre>   Ah Love! could you and I with Fate conspire   To grasp this sorry Scheme of Things entire,   Would not we shatter it to bits -- and then   Re-mould it nearer to the Heart's Desire!</pre>will produce the output<pre>   Ah Love! could you and I with Fate conspire   Would not we shatter it to bits -- and then   Re-mould it nearer to the Heart's Desire!</pre>The job falls neatly into three pieces:<pre>while (<em>there's another line</em>)    if (<em>the line contains the pattern</em>)        <em>print it</em></pre>Although it's certainly possible to put the code for all of this in <tt>main</tt>,a better way is to use the structure to advantage by making each part aseparate function. Three small pieces are better to deal with than one bigone, because irrelevant details can be buried in the functions, and thechance of unwanted interactions is minimized. And the pieces may even beuseful in other programs.<p>``While there's another line'' is <tt>getline</tt>, a function that we wrote in<a href="chapter1.html">Chapter 1</a>, and ``print it'' is <tt>printf</tt>, which someone hasalready provided for us. This means we need only write a routine to decidewhether the line contains an occurrence of the pattern.<p>We can solve that problem by writing a function <tt>strindex(s,t)</tt> thatreturns the position or index in the string <tt>s</tt> where the string <tt>t</tt>begins, or <tt>-1</tt> if <tt>s</tt> does not contain <tt>t</tt>. Because C arraysbegin at position zero, indexes will be zero or positive, and so  a negativevalue like <tt>-1</tt> is convenient for signaling failure. When we later needmore sophisticated pattern matching, we only have to replace <tt>strindex</tt>;the rest of the code can remain the same. (The standard library provides afunction <tt>strstr</tt> that is similar to <tt>strindex</tt>, except that itreturns a pointer instead of an index.)<p>Given this much design, filling in the details of the program isstraightforward. Here is the whole thing, so you can see how the pieces fittogether. For now, the pattern to be searched for is a literal string, which isnot the most general of mechanisms. We will return shortly to a discussionof how to initialize character arrays, and in <a href="chapter5.html">Chapter 5</a>will show how to make the pattern a parameter that is set when the program is run.There is also a slightly different version of <tt>getline</tt>; you might find itinstructive to compare it to the one in <a href="chapter1.html">Chapter 1</a>.<pre>   #include &lt;stdio.h&gt;   #define MAXLINE 1000 /* maximum input line length */   int getline(char line[], int max)   int strindex(char source[], char searchfor[]);   char pattern[] = "ould";   /* pattern to search for */   /* find all lines matching pattern */   main()   {       char line[MAXLINE];       int found = 0;       while (getline(line, MAXLINE) > 0)           if (strindex(line, pattern) &gt;= 0) {               printf("%s", line);               found++;           }       return found;   }   /* getline:  get line into s, return length */   int getline(char s[], int lim)   {       int c, i;       i = 0;       while (--lim > 0 && (c=getchar()) != EOF && c != '\n')           s[i++] = c;       if (c == '\n')           s[i++] = c;       s[i] = '\0';       return i;   }   /* strindex:  return index of t in s, -1 if none */   int strindex(char s[], char t[])   {       int i, j, k;       for (i = 0; s[i] != '\0'; i++) {           for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)               ;           if (k > 0 && t[k] == '\0')               return i;       }       return -1;   }</pre>Each function definition has the form<pre><em>return-type function-name</em>(<em>argument declarations</em>){    <em>declarations and statements</em>}</pre>Various parts may be absent; a minimal function is<pre>   dummy() {}</pre>which does nothing and returns nothing. A do-nothing function like this issometimes useful as a place holder during program development. If the returntype is omitted, <tt>int</tt> is assumed.<p>A program is just a set of definitions of variables and functions.Communication between the functions is by arguments and values returned bythe functions, and through external variables. The functions can occur inany order in the source file, and the source program can be split intomultiple files, so long as no function is split.<p>The <tt>return</tt> statement is the mechanism for returning a value from thecalled function to its caller. Any expression can follow <tt>return</tt>:<pre>   return <em>expression</em>;</pre>The <em>expression</em> will be converted to the return type of the functionif necessary. Parentheses are often used around the <em>expression</em>, butthey are optional.<p>The calling function is free to ignore the returned value. Furthermore, thereneed to be no expression after <tt>return</tt>; in that case, no value is returnedto the caller. Control also returns to the caller with no value when execution``falls off the end'' of the function by reaching the closing right brace. Itis not illegal, but probably a sign of trouble, if a function returns a valuefrom one place and no value from another. In any case, if a function fails toreturn a value, its ``value'' is certain to be garbage.<p>The pattern-searching program returns a status from <tt>main</tt>, the number ofmatches found. This value is available for use by the environment that calledthe program<p>The mechanics of how to compile and load a C program that resides on multiplesource files vary from one system to the next. On the UNIX system, forexample, the <tt>cc</tt> command mentioned in <a href="chapter1.html">Chapter 1</a>does the job. Suppose that the three functions are stored in three filescalled <tt>main.c</tt>, <tt>getline.c</tt>, and <tt>strindex.c</tt>. Thenthe command<pre>   cc main.c getline.c strindex.c</pre>compiles the three files, placing the resulting object code in files<tt>main.o</tt>, <tt>getline.o</tt>, and<tt>strindex.o</tt>, then loads them all into an executable file called<tt>a.out</tt>. If there is an error, say in <tt>main.c</tt>, the file can berecompiled by itself and the result loaded with the previous object files,with the command<pre>   cc main.c getline.o strindex.o</pre>The <tt>cc</tt> command uses the ``<tt>.c</tt>'' versus ``<tt>.o</tt>'' namingconvention to distinguish source files from object files.<p><strong>Exercise 4-1.</strong> Write the function <tt>strindex(s,t)</tt> which returnsthe position of the <em>rightmost</em> occurrence of <tt>t</tt> in <tt>s</tt>, or<tt>-1</tt> if there is none.<h2><a name="s4.2">4.2 Functions Returning Non-integers</a></h2>So far our examples of functions have returned either no value (<tt>void</tt>)or an <tt>int</tt>. What if a function must return some other type? manynumerical functions like <tt>sqrt</tt>, <tt>sin</tt>, and <tt>cos</tt> return<tt>double</tt>; other specialized functions return other types. To illustratehow to deal with this, let us write and use the function <tt>atof(s)</tt>, whichconverts the string <tt>s</tt> to its double-precision floating-point equivalent.<tt>atof</tt> if an extension of <tt>atoi</tt>, which we showed versions ofin <a href="chapter2.html">Chapters 2</a> and <a href="chapter3.html">3</a>.It handles an optional sign and decimal point, and the presence or absence ofeither part or fractional part. Our version is <em>not</em> a high-qualityinput conversion routine; that would take more space than we care to use. Thestandard library includes an <tt>atof</tt>; the header <tt>&lt;stdlib.h&gt;</tt>declares it.<p>First, <tt>atof</tt> itself must declare the type of value it returns, sinceit is not <tt>int</tt>. The type name precedes the function name:<pre>   #include &lt;ctype.h&gt;   /* atof:  convert string s to double */   double atof(char s[])   {       double val, power;       int i, sign;       for (i = 0; isspace(s[i]); i++)  /* skip white space */           ;       sign = (s[i] == '-') ? -1 : 1;       if (s[i] == '+' || s[i] == '-')           i++;       for (val = 0.0; isdigit(s[i]); i++)           val = 10.0 * val + (s[i] - '0');       if (s[i] == '.')           i++;       for (power = 1.0; isdigit(s[i]); i++) {           val = 10.0 * val + (s[i] - '0');           power *= 10;       }       return sign * val / power;   }</pre>Second, and just as important, the calling routine must know that <tt>atof</tt>returns a non-int value. One way to ensure this is to declare <tt>atof</tt>explicitly in the calling routine. The declaration is shown in this primitivecalculator (barely adequate for check-book balancing), which reads one numberper line, optionally preceded with a sign, and adds them up, printing therunning sum after each input:<pre>   #include &lt;stdio.h&gt;   #define MAXLINE 100   /* rudimentary calculator */   main()   {       double sum, atof(char []);       char line[MAXLINE];       int getline(char line[], int max);       sum = 0;       while (getline(line, MAXLINE) > 0)           printf("\t%g\n", sum += atof(line));       return 0;   }</pre>The declaration<pre>   double sum, atof(char []);</pre>says that <tt>sum</tt> is a <tt>double</tt> variable, and that <tt>atof</tt> is afunction that takes one <tt>char[]</tt> argument and returns a <tt>double</tt>.<p>The function <tt>atof</tt> must be declared and defined consistently. If<tt>atof</tt> itself and the call to it in <tt>main</tt> have inconsistent types in thesame source file, the error will be detected by the compiler. But if (as ismore likely) <tt>atof</tt> were compiled separately, the mismatch would not bedetected, <tt>atof</tt> would return a <tt>double</tt> that <tt>main</tt> wouldtreat as an <tt>int</tt>, and meaningless answers would result.<p>In the light of what we have said about how declarations must matchdefinitions, this might seem surprising. The reason a mismatch can happen isthat if there is no function prototype, a function is implicitly declared byits first appearance in an expression, such as<pre>   sum += atof(line)</pre>If a name that has not been previously declared occurs in an expression and isfollowed by a left parentheses, it is declared by context to be a functionname, the function is assumed to return an <tt>int</tt>, and nothing is assumedabout its arguments. Furthermore, if a function declaration does not includearguments, as in<pre>   double atof();</pre>that too is taken to mean that nothing is to be assumed about the argumentsof <tt>atof</tt>; all parameter checking is turned off. This special meaningof the empty argument list is intended to permit older C programs to compilewith new compilers. But it's a bad idea to use it with new C programs. If thefunction takes arguments, declare them; if it takes no arguments, use<tt>void</tt>.<p>Given <tt>atof</tt>, properly declared, we could write <tt>atoi</tt> (convert astring to <tt>int</tt>) in terms of it:<pre>   /* atoi:  convert string s to integer using atof */

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日精品一区二区三区| 成人app网站| 国产成a人无v码亚洲福利| 91在线看国产| 精品99久久久久久| 亚洲最新在线观看| 另类综合日韩欧美亚洲| 欧美在线视频日韩| 国产欧美一区二区在线| 男女性色大片免费观看一区二区 | 日本va欧美va欧美va精品| 粉嫩嫩av羞羞动漫久久久| 日韩欧美高清一区| 亚洲电影第三页| 成人国产精品免费观看动漫| 久久久噜噜噜久噜久久综合| 午夜电影网亚洲视频| 成人av电影免费观看| 日韩精品中文字幕在线不卡尤物| 一区二区三区在线观看欧美| 国产精品一品视频| 欧美mv日韩mv国产网站app| 亚洲综合av网| 在线观看日韩毛片| 亚洲综合999| 欧美中文字幕一区二区三区| 亚洲乱码国产乱码精品精小说| 成人一区二区三区| 中文字幕欧美激情| 成人天堂资源www在线| 国产无一区二区| 国产激情视频一区二区三区欧美| 日韩欧美色电影| 精品亚洲成a人| 欧美xxxxx牲另类人与| 毛片av一区二区三区| 91精品久久久久久久99蜜桃| 奇米色一区二区三区四区| 91精品国产全国免费观看| 日本色综合中文字幕| 欧美成人午夜电影| 精品夜夜嗨av一区二区三区| www国产亚洲精品久久麻豆| 久久99最新地址| 精品国产第一区二区三区观看体验| 久久国产精品第一页| 精品国产一二三区| 懂色av一区二区在线播放| 石原莉奈在线亚洲二区| 欧美一级爆毛片| 日本视频一区二区三区| 久久九九久久九九| 成人性生交大片免费看中文网站| 日本一区二区三区在线观看| www.欧美色图| 亚洲狠狠丁香婷婷综合久久久| 欧美日韩精品电影| 精品一区二区三区免费毛片爱| 国产欧美在线观看一区| 99精品久久久久久| 日韩影院免费视频| 久久久久久久久免费| 99精品视频一区二区三区| 亚洲成人动漫av| 久久久久国产一区二区三区四区| 91在线观看免费视频| 日韩在线一区二区| 国产欧美日产一区| 欧美年轻男男videosbes| 美国十次综合导航| 樱花影视一区二区| 欧美成人猛片aaaaaaa| 99视频热这里只有精品免费| 日韩1区2区日韩1区2区| 亚洲国产精品传媒在线观看| 欧美日韩国产一二三| 懂色av一区二区三区免费观看| 夜夜嗨av一区二区三区四季av| 久久久不卡影院| 欧美日韩久久久一区| 国产精品亚洲а∨天堂免在线| 亚洲高清免费在线| 中文字幕乱码久久午夜不卡| 欧美一区二区三区四区高清| 91国偷自产一区二区使用方法| 国产自产v一区二区三区c| 一区二区三区日本| 国产精品日韩成人| 久久奇米777| 欧美日产在线观看| 一本一道波多野结衣一区二区| 国产在线日韩欧美| 日韩av在线免费观看不卡| 国产精品精品国产色婷婷| 久久综合成人精品亚洲另类欧美 | 亚洲综合视频在线| 国产精品家庭影院| 久久久三级国产网站| 欧美一区在线视频| 欧美性三三影院| 91亚洲精品一区二区乱码| 国产69精品久久777的优势| 免费观看在线色综合| 亚洲午夜精品在线| 亚洲美女屁股眼交| 国产精品久久久久久久久搜平片 | 亚洲激情自拍视频| 国产精品久久久一区麻豆最新章节| 精品国偷自产国产一区| 91精品国产综合久久久蜜臀粉嫩 | 久久精品人人做人人综合| 日韩亚洲电影在线| 欧美一区二区三区思思人| 欧美精品国产精品| 7777精品伊人久久久大香线蕉经典版下载| 色综合久久综合网| 色婷婷综合视频在线观看| 91网站最新地址| 色噜噜狠狠色综合欧洲selulu| 91免费看片在线观看| 92精品国产成人观看免费| 91视频一区二区| 色88888久久久久久影院按摩| 色8久久人人97超碰香蕉987| 色偷偷久久一区二区三区| 色婷婷激情综合| 欧美日韩精品一区二区在线播放| 欧美三级韩国三级日本一级| 欧美群妇大交群中文字幕| 337p亚洲精品色噜噜| 日韩欧美一区二区免费| 337p粉嫩大胆色噜噜噜噜亚洲| 久久久久久黄色| 国产精品伦一区二区三级视频| 国产精品久久久久婷婷二区次| 亚洲欧美日韩久久| 丝袜亚洲精品中文字幕一区| 久久国产精品第一页| 成人国产精品免费| 欧美伊人久久久久久久久影院| 欧美高清精品3d| 久久久久久亚洲综合| 国产精品美女一区二区| 亚洲午夜激情av| 久久99久久99小草精品免视看| 国产成人亚洲综合a∨婷婷 | 国产一级精品在线| www.亚洲激情.com| 欧美久久久久久久久中文字幕| 精品国产不卡一区二区三区| 国产精品乱码人人做人人爱| 一区二区免费看| 精品亚洲成av人在线观看| 一本久久a久久精品亚洲| 欧美一级在线观看| 国产精品久久久久久久久搜平片 | 一区二区三区中文字幕电影| 麻豆视频观看网址久久| 成人精品免费网站| 日韩一区二区三区四区| 一区精品在线播放| 久久国产精品免费| 欧美三级电影精品| 欧美激情一区二区三区全黄| 午夜视频在线观看一区二区| 成人深夜在线观看| 欧美成人欧美edvon| 亚洲午夜精品在线| 99久免费精品视频在线观看| 欧美大黄免费观看| 亚洲综合激情小说| 99精品1区2区| 精品国精品国产| 丝袜国产日韩另类美女| 91首页免费视频| 国产精品区一区二区三| 免费在线观看成人| 欧美自拍丝袜亚洲| 亚洲日本中文字幕区| 国产精品资源在线观看| 欧美一区二区三区四区视频| 亚洲一区二区欧美| 91丨porny丨户外露出| 日本一区二区三级电影在线观看| 美女网站在线免费欧美精品| 欧美日韩亚洲不卡| 亚洲欧美国产77777| 成人综合婷婷国产精品久久蜜臀| 欧美zozo另类异族| 美女视频黄免费的久久 | 亚洲线精品一区二区三区| 91网站在线播放| 亚洲天堂久久久久久久| 成人在线视频首页| 欧美国产精品一区二区| 国产一区二区三区精品欧美日韩一区二区三区 | 国内精品免费在线观看| 欧美变态tickle挠乳网站| 美女在线视频一区| 精品国产sm最大网站|