c-iaq.html

来自「稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现」· HTML 代码 · 共 1,251 行 · 第 1/5 页

HTML
1,251
字号
#pragma induction add</PRE>		<H4><A NAME="question-9.5"></A>9.5: Shouldn't the following code: </H4>		<PRE>	#define ROSE 1	#define CHRYSANTHEMUM 2	#define RHODODENDRON 3	#define WATER_LILY 4	printf(&quot;%d\n&quot;, CHRYSATHNEMUM);</PRE>		<P>print ``2''? </P>		<P>You misspelled CHRYSANTHEMUM. Use abbreviations for long flower		names in C code. </P>		<H3><A NAME="section-10"></A>Section 10: ANSI C</H3>		<H4><A NAME="question-10.1"></A>10.1: What is the ``ANSI C Standard?'' </H4>		<P>A whiny bunch of lusers who haven't written as many books as Herbert Schildt.   [<a href="c-iaq-a.html#question-10.1">a</a>]</P>		<H4><A NAME="question-10.2"></A>10.2: How can I get a copy of the Standard? </H4>		<P>		Use a copier. 		<H4><A NAME="question-10.3"></A>10.3: Does anyone have a tool for converting old-style C programs		to ANSI C, or vice versa, or for automatically generating prototypes?		</H4>		<P>A router helps, but your best bet is still the band saw. Quick,		efficient, and powerful. </P>		<H4><A NAME="question-10.4"></A>10.4: I'm trying to use the ANSI ``stringizing'' preprocessing		operator <CODE>#</CODE> to insert the value of a symbolic constant into a message, but		it keeps stringizing the macro's name rather than its value. </H4>		<P>This is because <CODE>&quot;3&quot;</CODE> is not a legal integral constant in C - it's a string constant.		</P>		<H4><A NAME="question-10.5"></A>10.5: I don't understand why I can't use const values in initializers		and array dimensions, as in </H4>		<PRE>	const int n = 7;	int a[n];</PRE>		<P>Because you're not using C++. </P>		<H4><A NAME="question-10.6"></A>10.6: What's the difference between ``char const *p'' and ``char		* const p''? </H4>		<P>One `` '' character. There are some trivial differences having		to do with the distinction between a pointer to a constant, and		a constant pointer, but since you can cast either to a (char *)		it hardly matters. </P>		<H4><A NAME="question-10.7"></A>10.7: Can I declare main as void, to shut off these annoying ``main		returns no value'' messages? (I'm calling exit(), so main doesn't		return.) </H4>		<P>Certainly. You can also declare it as double. It may not compile,		or it may crash, but who cares? No lousy bunch of whining lusers		is going to tell *you* what to do. </P>		<H4><A NAME="question-10.8"></A>10.8: Why does the ANSI Standard not guarantee more than six monocase		characters of external identifier significance? </H4>		<P>Because none of the members of the committee had names over six letters, or   in which letters other than the first were capitalized. [<a href="c-iaq-a.html#question-10.8">a</a>]	</P>		<H4><A NAME="question-10.9"></A>10.9: What is the difference between memcpy and memmove? </H4>		<P>memmove moves memory, and memcpy copies it. memmove may not be		supported on machines without internal robot arms. Do not use		memmove while the machine is powered up - you can destroy your		memory. </P>		<H4><A NAME="question-10.10"></A>10.10: Why won't the Frobozz Magic C Compiler, which claims to		be ANSI compliant, accept this code? I know that the code is ANSI,		because gcc accepts it. </H4>		<P>The Frobozz Magic Company lies through its teeth. Consider: does Flood Control   Dam #3 actually control floods? Didn't think so. The wands are excellent for   making useless via casts of Float, though. [<a href="c-iaq-a.html#question-10.10">a</a>]</P>		<H4><A NAME="question-10.11"></A>10.11: Why can't I perform arithmetic on a void * pointer? </H4>		<P>You're too big and clumsy. When you try to push the numbers together,		you lose your balance. Perhaps you should get some angels from		the rave over on pin 3. </P>		<H4><A NAME="question-10.12"></A>10.12: What are #pragmas and what are they good for? </H4>		<P>They are useful ways to eliminate compiler features which are		not helpful to your goals; contrast #utility, which introduces		useful compiler features, and #absolutist, which introduces those		compiler features believed to be right. #relativist is supported		by some compilers. </P>		<H4><A NAME="question-10.13"></A>10.13: What does ``#pragma once'' mean? I found it in some header		files. </H4>		<P>It means that your program will only run once; it's used to create ``crippled   demos''. [<a href="c-iaq-a.html#question-10.13">a</a>]</P>		<H4><A NAME="question-10.14"></A>10.14: People seem to make a point of distinguishing between implementation-defined,		unspecified, and undefined behavior. What's the difference? </H4>		<P>There isn't really one; people just enjoy flaming over nits. (To		be technical, one has a hyphen, one has a space, and one is a		single word.) </P>		<H4><A NAME="question-10.15"></A>10.15: Is C an acronym? </H4>		<P>Yes, it stands for ``C''. It's another of those funky recursive		acronyms. </P>		<H3><A NAME="section-11"></A>Section 11: Stdio</H3>		<H4><A NAME="question-11.1"></A>11.1: What's wrong with this code: </H4>		<PRE>	char c;	while((c = getchar()) != EOF)...</PRE>		<P>You forgot to include space for the terminating NUL character,		so the compiler can't find the end of c without overwriting other		memory. In all probability, after the user types ``n&lt;return&gt;'',		your code will look like </P>		<PRE>char cnwhile((c = getchar()) != EOF)...</PRE>		<P>which won't compile. </P>		<P>Also, the ellipsis is not legal outside of function protoypes.		</P>		<P>Try </P>		<PRE>char c[2]; /* include space for terminating NUL */while ((c = getchar()) != EOF)	;</PRE>		<P>Note the use of the null statement to absorb the NUL. (See <A HREF="#section-4">Section   4</A>.)</P>		<H4><A NAME="question-11.2"></A>11.2: How can I print a ``%'' character in a printf format string?		I tried ``\%'' but it didn't work. </H4>		<P>Break the '%' sign out. i.e., fprintf(&quot;foo &quot; &quot;%&quot; &quot;%d\n&quot;, foo);		</P>		<P>Alternatively, try </P>		<P>sprintf(&quot;o&quot; &quot;/&quot; &quot;o&quot;) to get a &quot;%&quot;. </P>		<P>The astute reader will notice that the latter example uses sprintf, and the   former fprintf - this is because sprintf() works by characters, or strings,   while fprintf (``fast printf'') works on files. [<a href="c-iaq-a.html#question-11.2">a</a>]</P>		<H4><A NAME="question-11.3"></A>11.3: Why doesn't the code scanf(&quot;%d&quot;, i); work? </H4>		<P>You need to do this a bit differently; you should always check		for the return from scanf, so try something like </P>		<PRE>i = 1;if ((scanf, &quot;%d&quot;, i) == 1)</PRE>		<P>to make sure you're reading correctly. (The assignment to i is		so that, if scanf fails, you still have a legal value in i.) </P>		<H4><A NAME="question-11.4"></A>11.4: Once I've used freopen, how can I get the original stdout		(or stdin) back? </H4>		<P>Call main() - the environment will be restored. </P>		<H4><A NAME="question-11.5"></A>11.5: Why won't the code </H4>		<PRE>	while(!feof(infp)) {		fgets(buf, MAXLINE, infp);		fputs(buf, outfp);	}</PRE>		<H4>work? </H4>		<P>Because the end of file character is not detected on files named		``infp''. (Introverted-iNtuitive-Feeling-Perceptive, that is.)		Also, it may be that the file was opened in text mode, where an		end of file is read as a capital 'Z' on most machines, and feof()		only looks for 'control Z'. </P>		<H4><A NAME="question-11.6"></A>11.6: Why does everyone say not to use gets()? </H4>		<P>Because they're trying to spoil your fun. gets() can make an otherwise		droll and predictable program a lot more exciting. </P>		<H4><A NAME="question-11.7"></A>11.7: Why does errno contain ENOTTY after a call to printf? </H4>		<P>Because stdout is not a mammal. [<a href="c-iaq-a.html#question-11.7">a</a>]</P>		<H4><A NAME="question-11.8"></A>11.8: My program's prompts and intermediate output don't always		show up on the screen, especially when I pipe the output through		another program. </H4>		<P>Have you turned your monitor on? If not, try hitting the ``PrtSc''		key, which will re-enable the electron guns. </P>		<H4><A NAME="question-11.9"></A>11.9: How can I read one character at a time, without waiting		for the RETURN key? </H4>		<P>Ask the user to press enter after hitting a single character. [<a href="c-iaq-a.html#question-11.9">a</a>]	</P>		<H4><A NAME="question-11.10"></A>11.10: People keep telling me that <code>getch()</code>   is not standard, but my C compiler has it. Are they wrong? </H4>		<P>They've been programming more than ten years. You haven't. Draw		your own conclusions. That's right! They hadn't noticed it. No		doubt their compilers have it too, and its behavior is identical		everywhere else in the world, also. That would explain everything.		</P>		<H4><A NAME="question-11.11"></A>11.11: What does it matter that <code>getch()</code>   isn't standard; it works, doesn't it? </H4>		<P>Well, that would depend on the definition you're using for ``works''.		</P>		<H4><A NAME="question-11.12"></A>11.12: I tried to port some code from a PC to   a unix machine, and now it crashes immediately on startup. It isn't using <code>getch()</code>   - it's reading directly from the keyboard. How can this be wrong? </H4>		<P>The chances are you forgot to run the Unix linker; currently your		code is linked to your PC hardware, and won't run anywhere else		until it's linked to the new hardware. It may also need to be		linked to someone with a brain. </P>		<H4><A NAME="question-11.13"></A>11.13: How can I redirect stdin or stdout to a file from within		a program? </H4>		<P>execlv(&quot;main()&quot; &quot;&gt; file&quot;, argv); </P>		<H4><A NAME="question-11.14"></A>11.14: How can I recover the file name given an open file descriptor?		</H4>		<P>You will have to search the filesystem for files of the same size		as the file you're reading, and compare information in them to		find the file you're working on. </P>		<H4><A NAME="question-11.15"></A>11.15: How do I open Flood Control Dam #3? </H4>		<P>PUSH THE YELLOW BUTTON. TURN THE BOLT WITH THE WRENCH. (You must have the wrench,   first.) [<a href="c-iaq-a.html#question-11.15">a</a>]</P>		<H3><A NAME="section-12"></A>Section 12: Library Subroutines</H3>		<H4><A NAME="question-12.1"></A>12.1: How can I convert numbers to strings (the opposite of atoi)?		Is there an itoa function? </H4>		<P>There's frequently an itoa function. Better yet, write your own;		it'll be good practice. On some implementations, (char *) x; will		convert x to a string. </P>		<H4><A NAME="question-12.2"></A>12.2: How can I get the current date or time of day in a C program?		</H4>		<P>fprintf(stderr, &quot;please enter the current time and date...&quot;);		fflush(stderr); gets(stdin); </P>		<H4><A NAME="question-12.3"></A>12.3: I need a random number generator. </H4>		<P>Count errors in Herbert Schildt's C books. No one has detected		any consistent pattern. </P>		<H4><A NAME="question-12.4"></A>12.4: How can I get random integers in a certain range? </H4>		<P>random(n) returns random numbers between n and INT_MAX. </P>		<H4><A NAME="question-12.5"></A>12.5: Each time I run my program, I get the same sequence of numbers		back from rand(). </H4>		<P>This is so your results will be reproducible. </P>		<H4><A NAME="question-12.6"></A>12.6: I need a random true/false value, so I'm taking rand() %		2, but it's just alternating 0, 1, 0, 1, 0... </H4>		<P>That seems pretty random to me. </P>		<H4><A NAME="question-12.7"></A>12.7: I need some code to do regular expression matching. </H4>		<P>So do I. Let me know if you find some. </P>		<H4><A NAME="question-12.8"></A>12.8: I read through the standard library, but there's no function		to multiply two floating point numbers! Help! </H4>		<P>Many C compilers offer an extension ``mult'' to do just this.		If your compiler doesn't, just hang tight; ANSI is likely to add		it in the next revision. </P>		<P>For now, you can try </P>		<PRE>float mult(float m, n){	float i = 0, j = 0;	for (i = 0; i &lt; n; ++i)		j += m;	return j;}</PRE>		<P>which is fine as long as n is an integer. </P>		<H4><A NAME="question-12.9"></A>12.9: How do I get past the snake? </H4>		<P>Release the bird. You will have to drop the rod to get the bird in the cage.   [<a href="c-iaq-a.html#question-12.9">a</a>]</P>		<H3><A NAME="section-13"></A>Section 13: Floating Point</H3>		<H4><A NAME="question-13.1"></A>13.1: My floating-point calculations are acting strangely and		giving me different answers on different machines. </H4>		<P>One of the machines is probably a Pentium. Scrap it and get a real machine.   [<a href="c-iaq-a.html#question-13.1">a</a>]</P>		<H4><A NAME="question-13.2"></A>13.2: I'm trying to do some simple trig, and I am #including &lt;math.h&gt;,		but I keep getting ``undefined: _sin'' compilation errors. </H4>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?