📄 questions.html
字号:
or vice versa,or for automatically generating prototypes?</p><p><a href="ansi/extensions.html" rel=subdocument>11.32</a>Why won'tthe Frobozz Magic C Compiler,which claims to be ANSI compliant,accept this code?I know that the code is ANSI, because <TT>gcc</TT> accepts it.</p><p><a href="ansi/undef.html" rel=subdocument>11.33</a>People seem to make a point of distinguishingbetweenimplementation-defined,unspecified,andundefinedbehavior.What do these mean?</p><p><a href="ansi/compliance.html" rel=subdocument>11.33b</a>What does it really mean for a program to be``legal''or``valid''or``conforming''?</p><p><a href="ansi/appalled.html" rel=subdocument>11.34</a>I'm appalled that the ANSI Standard leaves so many issues undefined.Isn't a Standard's whole job to standardize these things?</p><p><a href="ansi/experiment.html" rel=subdocument>11.35</a>People keep saying that the behaviorof <TT>i = i++</TT>is undefined,butI just triediton an ANSI-conforming compiler,and got the results I expected.</p><hr><H4>12. Stdio</H4><p><a href="stdio/getcharc.html" rel=subdocument>12.1</a>What's wrong withthis code?<pre>char c;while((c = getchar()) != EOF) ...</pre></p><p><a href="stdio/eofval.html" rel=subdocument>12.1b</a>I have a simple little program that reads characters until EOF,but how do I actually <em>enter</em> that ``EOF'' valuefrom the keyboard?I see that <TT>EOF</TT> is defined by <TT><stdio.h></TT> to be -1;am I supposed to enter -1?</p><p><a href="stdio/feof.html" rel=subdocument>12.2</a>Whydoesthesimple line-copying loop<TT>while(!feof(infp)) {fgets(buf, MAXLINE, infp);fputs(buf, outfp);}</TT>copythe last line twice?</p><p><a href="stdio/linebfdur.html" rel=subdocument>12.3</a>I'm using <TT>fgets</TT> to readlines from a file into anarrayof pointers.Why do all the lines end up containing copies of the last line?</p><p><a href="stdio/fflush.html" rel=subdocument>12.4</a>My program's prompts and intermediate output don't always show upon the screen,especially when I pipe the output through another program.</p><p><a href="stdio/charatatime.html" rel=subdocument>12.5</a>How can I read one character at a time,without waiting for the RETURN key?</p><p><a href="stdio/printfpercent.html" rel=subdocument>12.6</a>How can I print a <TT>'%'</TT> characterin a <TT>printf</TT> format string?I tried <TT>\%</TT>, but it didn't work.</p><p><a href="stdio/printftypes.html" rel=subdocument>12.7</a>Why doesn't<pre>long int n = 123456;printf("%d\n", n);</pre>work?</p><p><a href="stdio/varargproto.html" rel=subdocument>12.8</a>I thought that ANSI function prototypeswere supposed to guard against argument type mismatches.</p><p><a href="stdio/scanfvsprintf.html" rel=subdocument>12.9</a>Someone told me it was wrong to use <TT>%lf</TT> with <TT>printf</TT>.How can<TT>printf</TT> use <TT>%f</TT> for type <TT>double</TT>,if <TT>scanf</TT> requires <TT>%lf</TT>?</p><p><a href="stdio/printftypedef.html" rel=subdocument>12.9b</a>What <TT>printf</TT> format should I use for a typedeflike <TT>size_t</TT>when I don't knowwhether it's <TT>long</TT> or some other type?</p><p><a href="stdio/printfvwid.html" rel=subdocument>12.10</a>How can I implement a variable field width with <TT>printf</TT>?That is, instead ofsomething like<TT>%8d</TT>,I want the width to be specifiedat run time.</p><p><a href="stdio/commaprint.html" rel=subdocument>12.11</a>How can I print numbers with commas separating the thousands?<br>What about currency formatted numbers?</p><p><a href="stdio/scanf1.html" rel=subdocument>12.12</a>Why doesn't the call<TT>scanf("%d", i)</TT>work?</p><p><a href="stdio/scanf1a.html" rel=subdocument>12.12b</a>Why <em>does</em> the call<pre>char s[30];scanf("%s", s);</pre>work?I thought you always needed an <TT>&</TT>on each variable passed to <TT>scanf</TT>.</p><p><a href="stdio/scanf2.html" rel=subdocument>12.13</a>Why doesn'tthis code:<pre>double d;scanf("%f", &d);</pre>work?</p><p><a href="stdio/scanfh.html" rel=subdocument>12.14</a>Why doesn't the code<pre>short int s;scanf("%d", &s);</pre>work?</p><p><a href="stdio/scanfvwid.html" rel=subdocument>12.15</a>How can I specify a variable width in a <TT>scanf</TT> format string?</p><p><a href="stdio/datafmts.html" rel=subdocument>12.16</a>How can I read data fromdata files with particular formats?<br>How can I read ten floats without having to use a jawbreaker <TT>scanf</TT> format<br>like<TT>"%f %f %f %f %f %f %f %f %f %f"</TT>?<br>How can I read an arbitrary number of fields from a line into an array?</p><p><a href="stdio/scanfhang.html" rel=subdocument>12.17</a>When I readnumbersfrom the keyboard with<TT>scanf</TT>and a<TT>"%d\n"</TT>format,like this:<pre> int n; scanf("%d\n", &n); printf("you typed %d\n", n);</pre>it seems to hang until I type one extra line of input.</p><p><a href="stdio/scanfinterlace.html" rel=subdocument>12.18a</a>I'm reading a number with <TT>scanf</TT>and<TT>%d</TT>,and then a string with<TT>gets()</TT>:<pre> int n; char str[80]; printf("enter a number: "); scanf("%d", &n); printf("enter a string: "); gets(str); printf("you typed %d and \"%s\"\n", n, str);</pre>but the compiler seems to beskipping the call to <TT>gets()</TT>!</p><p><a href="stdio/scanfc.html" rel=subdocument>12.18b</a>I'm using <TT>scanf %c</TT> to read a Y/N response,but later input gets skipped.</p><p><a href="stdio/scanfjam.html" rel=subdocument>12.19</a>I figured I could use <TT>scanf</TT> more safelyif I checked its return valueto make sure that the usertyped the numeric values I expect:<pre> int n; while(1) { printf("enter a number: "); if(scanf("%d", &n) == 1) break; printf("try again: "); } printf("you typed %d\n", n);</pre>but sometimes it seems to go into an infinite loop.<a href="http://www.eskimo.com/notfound.html" rel=subdocument>[footnote]</a>Why?</p><p><a href="stdio/scanfprobs.html" rel=subdocument>12.20</a>Why does everyone say not to use <TT>scanf</TT>?What should I use instead?</p><p><a href="stdio/sprintfsize.html" rel=subdocument>12.21</a>How can I tell how much destination buffer space I'll needfor an arbitrary <TT>sprintf</TT> call?How can I avoidoverflowingthe destination buffer with <TT>sprintf</TT>?</p><p><a href="stdio/sprintfret.html" rel=subdocument>12.22</a>What's the deal on <TT>sprintf</TT>'s return value?Is it an <TT>int</TT> or a <TT>char *</TT>?</p><p><a href="stdio/getsvsfgets.html" rel=subdocument>12.23</a>Why does everyone say not to use <TT>gets()</TT>?</p><p><a href="stdio/printferrno.html" rel=subdocument>12.24</a>I thought I'd check <TT>errno</TT>after a long string of <TT>printf</TT> calls,to see if any of them had failed:<pre> errno = 0; printf("This\n"); printf("is\n"); printf("a\n"); printf("test.\n"); if(errno != 0) fprintf(stderr, "printf failed: %s\n", strerror(errno));</pre>Why is it printingsomething strange like``printf failed: Not a typewriter''when I redirect the output to a file?</p><p><a href="stdio/fgetpos.html" rel=subdocument>12.25</a>What's the difference between <TT>fgetpos</TT>/<TT>fsetpos</TT>and <TT>ftell</TT>/<TT>fseek</TT>?<br>What are <TT>fgetpos</TT> and <TT>fsetpos</TT> good for?</p><p><a href="stdio/stdinflush.html" rel=subdocument>12.26a</a>How can I flush pending input so that a user's typeaheadisn't read at the next prompt?Will <TT>fflush(stdin)</TT>work?</p><p><a href="stdio/stdinflush2.html" rel=subdocument>12.26b</a>If <TT>fflush</TT> won't work,what can I use to flush input?</p><p><a href="stdio/fopenfp.html" rel=subdocument>12.27</a>I wrote this routine which is supposed to open a file:<pre> myfopen(char *filename, FILE *fp) { fp = fopen(filename, "r"); }</pre>But when I call it like this:<pre> FILE *infp; myfopen("filename.dat", infp);</pre>the <TT>infp</TT> variablein the caller doesn't get set properly.</p><p><a href="stdio/fopenmodec.html" rel=subdocument>12.28</a>I can't even get a simple <TT>fopen</TT> call to work!What's wrong withthis call?<pre> FILE *fp = fopen(filename, 'r');</pre></p><p><a href="stdio/constrpath.html" rel=subdocument>12.28b</a>How can I open files with names like``<TT>file1</TT>'',``<TT>file2</TT>'',``<TT>file3</TT>'',etc.,where the numeric part is controlled by a variable?Basically I want ``<TT>file%d</TT>'',like <TT>printf</TT>.</p><p><a href="stdio/fopenpath.html" rel=subdocument>12.29</a><TT>fopen</TT> is failing for certain pathnames.</p><p><a href="stdio/fupdate.html" rel=subdocument>12.30</a>I'm trying to update a file in place,by using <TT>fopen</TT> mode <TT>"r+"</TT>,reading a certain string,and writing back a modified string,but it's not working.</p><p><a href="stdio/insdelrec.html" rel=subdocument>12.31</a>How can I insert or delete a line(or record)in the middle of a file?</p><p><a href="stdio/fdfilename.html" rel=subdocument>12.32</a>How can I recover the file name given an openstream?</p><p><a href="stdio/freopen.html" rel=subdocument>12.33</a>How can I redirect <TT>stdin</TT> or <TT>stdout</TT>to a filefrom within a program?</p><p><a href="stdio/undofreopen.html" rel=subdocument>12.34</a>Once I've used <TT>freopen</TT>, how can I get the original<TT>stdout</TT>(or <TT>stdin</TT>)back?</p><p><a href="stdio/redirp.html" rel=subdocument>12.35</a>How can I tell if standard input or output is redirected(i.e. whether ``<TT><</TT>'' or ``<TT>></TT>''was used on the invocation command line)?</p><p><a href="stdio/devtty.html" rel=subdocument>12.36</a>I'm trying to write a program like ``<TT>more</TT>.''How can Igetbacktothe interactive keyboard if <TT>stdin</TT> is redirected?</p><p><a href="stdio/multiway.html" rel=subdocument>12.36b</a>How can I arrange to have output go two places at once,e.g. to the screen and to a file?</p><p><a href="stdio/binaryio.html" rel=subdocument>12.37</a>I want toread and write numbersbetween files and memoryin a byte-at-a-time way,not as formatted charactersthe way <TT>fprintf</TT> and <TT>fscanf</TT> do.Howcan I do this?</p><p><a href="stdio/fopenbinary.html" rel=subdocument>12.38</a>How can I read a binary data file properly?I'm occasionally seeing <TT>0x0a</TT> and <TT>0x0d</TT> values getting garbled,andI seemto hit EOF prematurely if the data contains the value <TT>0x1a</TT>.</p><p><a href="stdio/stdinbinary.html" rel=subdocument>12.39</a>I'm writing a``filter''for binary files,but <TT>stdin</TT> and <TT>stdout</TT> are preopenedas text streams.How can I changetheir modeto binary?</p><p><a href="stdio/textvsbinary.html" rel=subdocument>12.40</a>What's the difference between text and binary I/O?</p><p><a href="stdio/structio.html" rel=subdocument>12.41</a>How can I read/write structures from/todata files?</p><p><a href="stdio/extconform.html" rel=subdocument>12.42</a>How can I write code to conform to these old, binary data file formats?</p><p><a href="stdio/runtimesc2.html" rel=subdocument>12.43</a>I'm readingstringstyped bythe userinto an array,and then printing them out later.When the user typesa sequence like <TT>\n</TT>,whyisn't it beinghandled properly?</p><hr><H4>13. Library Functions</H4><p><a href="lib/itoa.html" rel=subdocument>13.1</a>How can I convert numbers to strings(the opposite of <TT>atoi</TT>)?Is there an <TT>itoa</TT> function?</p><p><a href="lib/strncpy.html" rel=subdocument>13.2</a>Why does <TT>strncpy</TT> not alwaysplace a <TT>'\0'</TT> terminatorin the destination string?</p><p><a href="lib/substr.html" rel=subdocument>13.3</a>Does C have anything like the ``substr''(extract substring)routine present in other languages?</p><p><a href="lib/strupper.html" rel=subdocument>13.4</a>How do I convert a string to all upper or lower case?</p><p><a href="lib/toupper.html" rel=subdocument>13.5</a>Whydo some versions of<TT>toupper</TT> act strangely if given an upper-case letter?<br>Why does some code call <TT>islower</TT> before <TT>toupper</TT>?</p><p><a href="lib/strtok.html" rel=subdocument>13.6</a>How can I split up astringintowhitespace-separated fields?<br>How can I duplicate the processby which <TT>main()</TT>is handed <TT>argc</TT> and <TT>argv</TT>?</p><p><a href="lib/regex.html" rel=subdocument>13.7</a>I need some code to doregular expressionandwildcard matching.</p><p><a href="lib/qsort1.html" rel=subdocument>13.8</a>I'm trying to sort an array of strings with <TT>qsort</TT>,using<TT>strcmp</TT>as the comparison function,but it's not working.</p><p><a href="lib/qsort2.html" rel=subdocument>13.9</a>Now I'm trying to sort an array of structureswith <TT>qsort</TT>.My comparison function takes pointers to structures,but the compilercomplainsthat the function isof the wrong type for <TT>qsort</TT>.How can I cast the function pointer to shut off the warning?</p><p><a href="lib/listsort.html" rel=subdocument>13.10</a>How can I sort a linked list?</p><p><a href="lib/extsort.html" rel=subdocument>13.11</a>How can I sort more data than will fit in memory?</p><p><a href="lib/curtime.html" rel=subdocument>13.12</a>How can I get thecurrent dateortime of day in a C program?</p><p><a href="lib/mktime.html" rel=subdocument>13.13</a>I know that the library function <TT>localtime</TT>will convert a <TT>time_t</TT> into a broken-down <TT>struct tm</TT>,and that <TT>ctime</TT>will convert a<TT>time_t</TT> to a printable string.How can Iperform the inverse operatio
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -