⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-iaq.html

📁 稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现
💻 HTML
📖 第 1 页 / 共 5 页
字号:
		<P>You forgot to define the sin() function. Most math texts should		cover it in some detail. The easiest way to fix this should be:		</P>		<PRE>double sin(double x) {	return sqrt(1 - cos(x) * cos(x));}</PRE>		<P>Warning: You <EM>must not</EM> declare this function as ``extern'', or you will still have link		problems. </P>		<H4><A NAME="question-13.3"></A>13.3: Why doesn't C have an exponentiation operator? </H4>		<P>It does. It looks like the multiplication operator, but you use		it more. For instance, the C way of expressing ``x squared'' is		``x*x''. ``x cubed'' would be ``x*x*x''. Easy, isn't it? </P>		<H4><A NAME="question-13.4"></A>13.4: How do I round numbers? </H4>		<P>Multiply by 10. <CITE>Numerical Recipies in C</CITE> has a section on this, but there's reputedly a bug in their algorithm.		</P>		<H4><A NAME="question-13.5"></A>13.5: How do I test for IEEE NaN and other special values? </H4>		<P>Using an electron microscope; the patterns are obvious once you		know them. </P>		<H4><A NAME="question-13.6"></A>13.6: I'm having trouble with a Turbo C program which crashes		and says something like ``floating point formats not linked.''		</H4>		<P>Turbo C is notoriously buggy. Get a compiler with floating point		support. </P>		<H4><A NAME="question-13.7"></A>13.7: What is so ``unsafe'' about floating point? </H4>		<P>Have you tried <CODE>EXAMINE STICK</CODE>? The stick has a sharp point, which punctures the raft, which		no longer floats. Don't bring the stick into the raft with you.		</P>		<H4><A NAME="question-13.8"></A>13.8: Which is larger, ``2'' or ``2.0''? </H4>		<P><CITE>Numerical Recipes in C</CITE> has a function for comparing two values to see which is greater.		It may have a slight bug, where it would report incorrect results		if the numbers differ by less than FLOAT_MAX / INT_MAX. </P>		<H4><A NAME="question-13.9"></A>13.9: When I try to compile the following code, I get the error		``invalid use of floating point'', what does this mean? </H4>		<PRE>	x=663608941*y%pow(2,32);</PRE>		<P>Remember that * is the indirection operator, as well as the multiplication		operator; try putting spaces before and after the ``*'' so the		compiler knows what you mean. Do the same with the % operator.		</P>		<H4><A NAME="question-13.10"></A>13.10: How can I copy a float into a string? </H4>		<P>strcpy(string_var, float_var); </P>		<H4><A NAME="question-13.11"></A>13.11: What are float variables, anyway? </H4>		<P>The term ``float variable'' is actually redundant; they are simply		variables whose value can ``float'' during execution. For instance:		</P>		<PRE>float f, g = 3;f = g; /* f ``floats'' to g */</PRE>		<P>Easy! </P>		<H3><A NAME="section-14"></A>Section 14: Variable-Length Argument Lists</H3>		<H4><A NAME="question-14.1"></A>14.1: How can I write a function that takes a variable number		of arguments? </H4>		<P>By declaring it with a variable number of arguments in the prototype.		Use only the arguments declared at any given time. </P>		<H4><A NAME="question-14.2"></A>14.2: How can I write a function that takes a format string and		a variable number of arguments, like printf, and passes them to		printf to do most of the work? </H4>		<P>Redefine printf; the call to ``printf'' inside yours will be resolved		to the library version, because the C language doesn't allow recursion.		</P>		<H4><A NAME="question-14.3"></A>14.3: How can I discover how many arguments a function was actually		called with? </H4>		<P>_args is an external integer constant. It evaluates to three times		the number of arguments the current function was called with.		You can then look at _argdata[args] to get the address of the		last arg, _argdata[args - 1] to get the size of the last arg,		and _argdata[args - 2] to get the type of the last arg (as an		int). </P>		<P>N.B. You *MUST* not refer to _args or _argdata between the ()'s		of a function call; their value will be indeterminate. Use temporary		storage. </P>		<H4><A NAME="question-14.4"></A>14.4: Why doesn't </H4>		<PRE>	printf(&quot;hello, &quot;, &quot;world!&quot;, '\n');</PRE>		<H4>work? I thought printf() took a variable number of arguments.		</H4>		<P>It will probably work some of the time; the number of arguments		used by printf() may vary, as it is a variadic function. </P>		<H3><A NAME="section-15"></A>Section 15: Lint</H3>		<H4><A NAME="question-15.1"></A>15.1: I just typed in this program, and it's acting strangely.		Can you see anything wrong with it? </H4>		<P>Yes. There's too much lint in it. You should get a shop vac. </P>		<H4><A NAME="question-15.2"></A>15.2: How can I shut off the ``warning: possible pointer alignment		problem'' message lint gives me for each call to malloc? </H4>		<P>Don't run lint. Alternatively, provide a prototype of ``extern		double * malloc()'' to make the return from malloc() be more strongly		aligned. </P>		<H4><A NAME="question-15.3"></A>15.3: Where can I get an ANSI-compatible lint? </H4>		<P>You may wish to check your spouse's navel occasionally, especially		if your spouse works for a standards committee. </P>		<H4><A NAME="question-15.4"></A>15.4: What does LINT stand for, anyway? </H4>		<P>Lexeme Interpreter aNd Tester. </P>		<H3><A NAME="section-16"></A>Section 16: Strange Problems</H3>		<H4><A NAME="question-16.1"></A>16.1: Something really strange happened when I ran this code!		</H4>		<P>No, it didn't. [<a href="c-iaq-a.html#question-16.1">a</a>]</P>		<H3><A NAME="section-17"></A>Section 17: Style</H3>		<H4><A NAME="question-17.1"></A>17.1: Here's a neat trick: </H4>		<PRE>	if(!strcmp(s1, s2))</PRE>		<H4>Is this good style? </H4>		<P>Not really; it's too similar to </P>		<PRE>	if (!strncmp(s1, s2))</PRE>		<P>which invokes undefined behavior, so it might be confusing. </P>		<H4><A NAME="question-17.2"></A>17.2: Here's an even neater trick: </H4>		<PRE>	volatile int True_Tester = 3;	#define TRUE (!True_Tester == !True_Tester)	#define FALSE ((!TRUE) != (!TRUE))</PRE>		<PRE>	#define STR_DISSIMILAR(x, y) (strcmp((x), (y)) != FALSE)</PRE>		<H4>Isn't this cool? </H4>		<P>Very impressive. The volatile int type assures that even seemingly		redundant calculations involving True_Tester will be performed,		making sure that if the compiler's ANSI-compliant values of 0		for false and 1 for true vary during runtime, your program will		detect it - and producing meaningful error messages if this change		occurs during a boolean computation! Similarly, the STR_DISSIMILAR		macro allows you to make quite clear what the real effects of		strcmp() are. </P>		<P>However, you must be careful; if this code is included twice,		it may produce errors, due to the multiple definitions of the		``True_Tester'' variable. You may wish to declare it ``extern''		(See <A HREF="#question-1.5">Question 1.5</A>.) </P>		<H4><A NAME="question-17.3"></A>17.3: What's the best style for code layout in C? </H4>		<P>There are many systems of indentation advocated, but all of them		have the same basic flaw; they will mislead the reader when the		actual code logic does not follow the indentation. It is better		to avoid indentation entirely, so the reader will not be misled.		</P>		<H4><A NAME="question-17.4"></A>17.4: Is goto a good thing or a bad thing? </H4>		<P>Yes. </P>		<H4><A NAME="question-17.5"></A>17.5: No, really, should I use goto statements in my code? </H4>		<P>Any loop control construct can be written with gotos; similarly,		any goto can be emulated by some loop control constructs and additional		logic. </P>		<P>However, gotos are unclean. For instance, compare the following		two code segments: </P>		<PRE>do {				foo();	foo();			if (bar())	if (bar())			goto SKIP;		break;		baz();	baz();			quux();	quux();			} while (1 == 0);		SKIP:buz();				buz();</PRE>		<P>Note how the loop control makes it quite clear that the statements		inside it will be looped on as long as a condition is met, where		the goto statement gives the impression that, if bar() returned		a nonzero value, the statements baz() and quux() will be skipped.		</P>		<H4><A NAME="question-17.6"></A>17.6: What's this ``white space'' I keep hearing about? </H4>		<P>White space is a racist, segregational term. Implicitly, ``dark''		or ``colored'' space (i.e., the '_' character) is not good enough		to separate tokens. More interestingly, the white space characters		keep the other tokens apart. They say it's for parsing, but there's		ample evidence the goal of white space is to keep the other characters		from ``taking over'' the program. This is disguised by the description		of C as ``white space insensitive'' - a simple ploy for sympathy.		</P>		<H3><A NAME="section-18"></A>Section 18: System Dependencies</H3>		<H4><A NAME="question-18.1"></A>18.1: How can I read a single character from the keyboard without		waiting for a newline? </H4>		<P>Try 'stty eol ^M' to wait for a carriage return. </P>		<H4><A NAME="question-18.2"></A>18.2: How can I find out if there are characters available for		reading (and if so, how many)? Alternatively, how can I do a read		that will not block if there are no characters available? </H4>		<P>The buffer is normally at ``&amp;main - 0100''. Lower if you have		more than 256 characters of typeahead. </P>		<H4><A NAME="question-18.3"></A>18.3: How can I clear the screen? How can I print things in inverse		video? </H4>		<P>You can clear the screen by sending several formfeed characters. Additionally,   some operating systems (like NetBSD) support a feature called ``whiteouts''.   [<a href="c-iaq-a.html#question-18.3">a</a>]</P>		<H4><A NAME="question-18.4"></A>18.4: How do I read the mouse? </H4>		<P>Flip it over, put on your reading glasses. </P>		<H4><A NAME="question-18.5"></A>18.5: How can my program discover the complete pathname to the		executable file from which it was invoked? </H4>		<P>By asking the user. </P>		<H4><A NAME="question-18.6"></A>18.6: How can a process change an environment variable in its		caller? </H4>		<P>Only by force. Example code for Unix: </P>		<PRE>memmove(getppid() + getenv(NULL), getpid() + getenv(NULL),	sizeof(environ);</PRE>		<H4><A NAME="question-18.7"></A>18.7: How can I check whether a file exists? I want to query the		user before overwriting existing files. </H4>		<P>Time an attempt to truncate it to zero length; if it takes more		than 20-30 ms, the file existed. The exact values will depend		on the system and the load; before testing, create several large		files and time attempts to truncate them, for calibration. </P>		<H4><A NAME="question-18.8"></A>18.8: How can I find out the size of a file, prior to reading		it in? </H4>		<P>There are two good ways: </P>		<OL>			<LI>Vernier calipers work well. 			<LI>mmap() the file, then use sizeof(). 		</OL>		<H4><A NAME="question-18.9"></A>18.9: I tried to use the second strategy above. I used mmap()		to map stdin, then tried to use sizeof. But, when my user is about		to write something very long, mmap() fails! How can I prevent		this? </H4>		<P>mmap() only 1k at a time, then, when you've read the first kilobyte		of your input, use </P>		<PRE>	memmove(mmapped_addr, mmapped_addr + 1024, 1024);</PRE>		<P>to move in the next kilobyte of data. </P>		<H4><A NAME="question-18.10"></A>18.10: How can I implement a delay, or time a user's response,		with sub-second resolution? </H4>		<P>Time writes of large files to disks; then you can wait for a certain		amount of time by writing a certain amount of data, and time a		response by how much you could write before the response arrived.		</P>		<P>You may need to delete spare or unneccessary files to do this;		for best results, use a loop like the following to eliminate temporary		files: </P>		<PRE>d = opendir(s);while (r = readdir(d)) {	/* remove files matching tmpnam's return, which is	 * the temporary file name. */	if (strcmp(d-&gt;d_name, tmpnam())) {		remove(d-&gt;d_name);	}}closedir(d);</PRE>		<H4><A NAME="question-18.11"></A>18.11: How can I read in an object file and jump to routines in		it? </H4>		<P>fopen and goto. </P>		<H4><A NAME="question-18.12"></A>18.12: How can I invoke an operating system command from within		a program? </H4>		<P>Ask the user to open a new shell. The best way to do this is </P>		<PRE>

⌨️ 快捷键说明

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