亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
成人性生交大片免费看中文| av在线播放不卡| 国产精品久久久久久久久免费桃花| 色哟哟精品一区| 国产69精品一区二区亚洲孕妇| 国内外成人在线| 久久99热这里只有精品| 蜜桃久久av一区| 韩国视频一区二区| 91久久国产最好的精华液| 欧美在线看片a免费观看| 在线精品视频一区二区三四| 久久久噜噜噜久噜久久综合| 亚洲精品一区二区三区蜜桃下载| 2020国产精品自拍| 国产精品国产三级国产普通话蜜臀| 午夜伊人狠狠久久| 久久66热re国产| 北岛玲一区二区三区四区| 日韩视频免费观看高清完整版在线观看 | 性久久久久久久久| 国产suv精品一区二区883| 日韩欧美成人午夜| 中文字幕av一区二区三区高 | 欧美疯狂做受xxxx富婆| 欧美一区二区三区婷婷月色| 久久美女艺术照精彩视频福利播放| 亚洲影院理伦片| 蜜桃免费网站一区二区三区| 欧美精品一二三| 亚洲va欧美va天堂v国产综合| 色综合天天天天做夜夜夜夜做| 欧美自拍偷拍一区| 一区二区中文视频| 亚洲综合小说图片| 日本精品一区二区三区四区的功能| 国产精品久久久久久久久图文区| 国产美女在线观看一区| 色婷婷香蕉在线一区二区| 亚洲免费观看高清完整版在线观看| 蜜臀av性久久久久蜜臀aⅴ流畅 | 韩国精品久久久| 日韩亚洲欧美综合| 精品一区二区三区免费视频| 久久综合色婷婷| 丁香激情综合国产| 亚洲国产精品ⅴa在线观看| 国产成人午夜99999| 成人免费毛片片v| 国产精品久久久久久久午夜片| 99久久夜色精品国产网站| 精品久久久久久久一区二区蜜臀| 一区二区在线免费| 高清在线观看日韩| 亚洲欧美二区三区| 7777女厕盗摄久久久| 中文字幕视频一区二区三区久| 91浏览器在线视频| 中文字幕精品在线不卡| 成人午夜碰碰视频| 亚洲高清免费观看高清完整版在线观看| 国产另类ts人妖一区二区| 在线成人免费视频| 国产伦精品一区二区三区免费| 国产精品超碰97尤物18| 欧美三级在线看| 亚洲欧美日韩在线| 欧美一区二区观看视频| 欧美日韩一区视频| 精品在线亚洲视频| 亚洲精品日韩专区silk| 欧美岛国在线观看| 一本在线高清不卡dvd| 麻豆国产91在线播放| 亚洲女同一区二区| 欧美xxxxxxxxx| 欧洲精品一区二区三区在线观看| 久久av老司机精品网站导航| 亚洲欧美偷拍卡通变态| 欧美精品一区二区久久久| 欧美亚洲尤物久久| 国产精品久久久久影院老司| 欧美一区二区三区视频在线观看| www.爱久久.com| 理论电影国产精品| 亚洲国产一区二区视频| 中文字幕不卡三区| 久久这里只有精品视频网| 欧美又粗又大又爽| 东方aⅴ免费观看久久av| 日韩国产欧美在线播放| 欧美日韩精品欧美日韩精品一| 亚洲国产一区视频| 国产精品视频线看| 色综合久久久久综合体桃花网| 久久国产麻豆精品| 丝袜美腿成人在线| 精品国产免费人成在线观看| 欧美性色欧美a在线播放| www..com久久爱| 国产成人a级片| 久久激情五月婷婷| 日韩成人伦理电影在线观看| 亚洲一区二区三区视频在线 | 夜夜亚洲天天久久| 欧美精品在线视频| 色妹子一区二区| 色婷婷av一区二区三区大白胸| 国产成人av一区二区| 国产一区欧美一区| 国产一区二三区好的| 美脚の诱脚舐め脚责91 | 精品久久一区二区三区| 欧美日韩mp4| 在线不卡的av| 在线播放国产精品二区一二区四区| 色综合久久88色综合天天6| 日本黄色一区二区| 在线免费一区三区| 欧美私模裸体表演在线观看| 91黄色在线观看| 欧美在线观看视频一区二区三区| 91蝌蚪porny| 欧美在线免费视屏| 91精品免费观看| 日韩一区二区精品| 日韩欧美第一区| 久久综合久久综合久久| 国产目拍亚洲精品99久久精品| 色悠悠久久综合| 在线观看亚洲一区| 欧美理论在线播放| 日韩一区二区三区观看| 久久综合精品国产一区二区三区 | 色香色香欲天天天影视综合网| 成人h动漫精品| 九九在线精品视频| 国产高清不卡一区| 91视频国产资源| 欧美乱妇23p| 国产人伦精品一区二区| 综合分类小说区另类春色亚洲小说欧美| 日韩午夜在线播放| 国产三级精品视频| 亚洲精品一卡二卡| 蜜臀91精品一区二区三区| 韩国女主播一区| 91麻豆国产在线观看| 91精品国产乱| 中文字幕一区二区三区四区 | 欧美日韩精品欧美日韩精品| 日韩一区二区在线看片| 欧美国产精品v| 天天影视网天天综合色在线播放| 亚洲美女一区二区三区| 秋霞电影网一区二区| 日韩专区在线视频| 国产成人免费在线观看不卡| 91精品办公室少妇高潮对白| 精品国产一区二区国模嫣然| 国产精品网友自拍| 日韩av中文字幕一区二区| 成人av网站在线观看免费| 欧美一区二区三区免费| 中文字幕在线一区| 精品一区免费av| 在线免费观看成人短视频| 久久久久久久电影| 日韩高清不卡一区| www.色综合.com| 久久日一线二线三线suv| 亚洲线精品一区二区三区八戒| 岛国一区二区三区| 欧美大度的电影原声| 亚洲超丰满肉感bbw| 成人国产亚洲欧美成人综合网| 日韩三级精品电影久久久| 亚洲品质自拍视频网站| 国产不卡视频一区| 精品久久人人做人人爽| 一区二区三区精品在线| 成人av网址在线| 久久久久久久久岛国免费| 麻豆国产欧美一区二区三区| 欧美午夜电影网| 亚洲精品国产品国语在线app| 国产精品一二三区| 欧美tk丨vk视频| 日本怡春院一区二区| 欧美日韩综合一区| 亚洲另类中文字| 色婷婷av一区二区三区之一色屋| 中文字幕精品—区二区四季| 国产制服丝袜一区| 欧美本精品男人aⅴ天堂| 琪琪久久久久日韩精品| 欧美精品一二三| 五月天激情综合网| 欧美高清dvd| 日本怡春院一区二区|