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

📄 subject_19421.htm

📁 一些关于vc的问答
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<br>内容:一篇 c faq:<BR>摘自&nbsp;&nbsp;google newsgroup:<BR>This article is Copyright 1990-1999 by Steve Summit.&nbsp;&nbsp;Content from the<BR>book _C Programming FAQs: Frequently Asked Questions_ is made available<BR>here by permission of the author and the publisher as a service to the<BR>community.&nbsp;&nbsp;It is intended to complement the use of the published text<BR>and is protected by international copyright laws.&nbsp;&nbsp;The content is made<BR>available here and may be accessed freely for personal use but may not<BR>be republished without permission.<BR><BR>This article contains minimal answers to the comp.lang.c frequently-<BR>asked questions list.&nbsp;&nbsp;More detailed explanations and references can be<BR>found in the long version (posted on the first of each month, or see<BR>question 20.40 for availability), and in the web version at http://www.eskimo.com/~scs/C-faq/top.html<BR>, and in the book _C Programming FAQs: Frequently Asked Questions_<BR>(Addison-Wesley, 1996, ISBN 0-201-84519-9). <BR><BR>Section 1. Declarations and Initializations<BR><BR>1.1:&nbsp;&nbsp;&nbsp;&nbsp;How do you decide which integer type to use?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;If you might need large values (tens of thousands), use long.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Otherwise, if space is very important, use short.&nbsp;&nbsp;Otherwise,<BR>&nbsp;&nbsp;&nbsp;&nbsp;use int.<BR><BR>1.4:&nbsp;&nbsp;&nbsp;&nbsp;What should the 64-bit type on a machine that can support it?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;C9X specifies long long.<BR><BR>1.7:&nbsp;&nbsp;&nbsp;&nbsp;What's the best way to declare and define global variables?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The best arrangement is to place each definition in some<BR>&nbsp;&nbsp;&nbsp;&nbsp;relevant .c file, with an external declaration in a header file.<BR><BR>1.11:&nbsp;&nbsp;&nbsp;&nbsp;What does extern mean in a function declaration?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Nothing, really; the keyword extern is optional here.<BR><BR>1.12:&nbsp;&nbsp;&nbsp;&nbsp;What's the auto keyword good for?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Nothing.<BR><BR>1.14:&nbsp;&nbsp;&nbsp;&nbsp;I can't seem to define a linked list node which contains a<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer to itself.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Structures in C can certainly contain pointers to themselves;<BR>&nbsp;&nbsp;&nbsp;&nbsp;the discussion and example in section 6.5 of K&R make this<BR>&nbsp;&nbsp;&nbsp;&nbsp;clear.&nbsp;&nbsp;Problems arise if an attempt is made to define (and use)<BR>&nbsp;&nbsp;&nbsp;&nbsp;a typedef in the midst of such a declaration; avoid this.<BR><BR>1.21:&nbsp;&nbsp;&nbsp;&nbsp;How do I declare an array of N pointers to functions returning<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers to functions returning pointers to characters?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;char *(*(*a[N])())();<BR>&nbsp;&nbsp;&nbsp;&nbsp;Using a chain of typedefs, or the cdecl program, makes these<BR>&nbsp;&nbsp;&nbsp;&nbsp;declarations easier.<BR><BR>1.22:&nbsp;&nbsp;&nbsp;&nbsp;How can I declare a function that returns a pointer to a<BR>&nbsp;&nbsp;&nbsp;&nbsp;function of its own type?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You can't quite do it directly.&nbsp;&nbsp;Use a cast, or wrap a struct<BR>&nbsp;&nbsp;&nbsp;&nbsp;around the pointer and return that.<BR><BR>1.25:&nbsp;&nbsp;&nbsp;&nbsp;My compiler is complaining about an invalid redeclaration of a<BR>&nbsp;&nbsp;&nbsp;&nbsp;function, but I only define it once.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Calling an undeclared function declares it implicitly as<BR>&nbsp;&nbsp;&nbsp;&nbsp;returning int.<BR><BR>1.25b:&nbsp;&nbsp;&nbsp;&nbsp;What's the right declaration for main()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See questions 11.12a to 11.15.<BR><BR>1.30:&nbsp;&nbsp;&nbsp;&nbsp;What am I allowed to assume about the initial values<BR>&nbsp;&nbsp;&nbsp;&nbsp;of variables which are not explicitly initialized?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Uninitialized variables with "static" duration start out as 0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;as if the programmer had initialized them.&nbsp;&nbsp;Variables with<BR>&nbsp;&nbsp;&nbsp;&nbsp;"automatic" duration, and dynamically-allocated memory, start<BR>&nbsp;&nbsp;&nbsp;&nbsp;out containing garbage (with the exception of calloc).<BR><BR>1.31:&nbsp;&nbsp;&nbsp;&nbsp;Why can't I initialize a local array with a string?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Perhaps you have a pre-ANSI compiler.<BR><BR>1.31b:&nbsp;&nbsp;&nbsp;&nbsp;What's wrong with "char *p = malloc(10);" ?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Function calls are not allowed in initializers for global or<BR>&nbsp;&nbsp;&nbsp;&nbsp;static variables.<BR><BR>1.32:&nbsp;&nbsp;&nbsp;&nbsp;What is the difference between char a[] = "string"; and<BR>&nbsp;&nbsp;&nbsp;&nbsp;char *p = "string"; ?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The first declares an initialized and modifiable array; the<BR>&nbsp;&nbsp;&nbsp;&nbsp;second declares a pointer initialized to a not-necessarily-<BR>&nbsp;&nbsp;&nbsp;&nbsp;modifiable constant string.<BR><BR>1.34:&nbsp;&nbsp;&nbsp;&nbsp;How do I initialize a pointer to a function?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Use something like "extern int func(); int (*fp)() = func;" .<BR><BR><BR>Section 2. Structures, Unions, and Enumerations<BR><BR>2.1:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between struct x1 { ... }; and<BR>&nbsp;&nbsp;&nbsp;&nbsp;typedef struct { ... } x2; ?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The first structure is named by a tag, the second by a typedef<BR>&nbsp;&nbsp;&nbsp;&nbsp;name.<BR><BR>2.2:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't "struct x { ... }; x thestruct;" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;C is not C++.<BR><BR>2.3:&nbsp;&nbsp;&nbsp;&nbsp;Can a structure contain a pointer to itself?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See question 1.14.<BR><BR>2.4:&nbsp;&nbsp;&nbsp;&nbsp;What's the best way of implementing opaque (abstract) data types<BR>&nbsp;&nbsp;&nbsp;&nbsp;in C?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;One good way is to use structure pointers which point to<BR>&nbsp;&nbsp;&nbsp;&nbsp;structure types which are not publicly defined.<BR><BR>2.6:&nbsp;&nbsp;&nbsp;&nbsp;I came across some code that declared a structure with the last<BR>&nbsp;&nbsp;&nbsp;&nbsp;member an array of one element, and then did some tricky<BR>&nbsp;&nbsp;&nbsp;&nbsp;allocation to make it act like the array had several elements.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Is this legal or portable?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;An official interpretation has deemed that it is not strictly<BR>&nbsp;&nbsp;&nbsp;&nbsp;conforming with the C Standard.<BR><BR>2.7:&nbsp;&nbsp;&nbsp;&nbsp;I heard that structures could be assigned to variables and<BR>&nbsp;&nbsp;&nbsp;&nbsp;passed to and from functions, but K&R1 says not.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;These operations are supported by all modern compilers.<BR><BR>2.8:&nbsp;&nbsp;&nbsp;&nbsp;Is there a way to compare structures automatically?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR>2.10:&nbsp;&nbsp;&nbsp;&nbsp;Can I pass constant values to functions which accept structure<BR>&nbsp;&nbsp;&nbsp;&nbsp;arguments?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not yet.&nbsp;&nbsp;As of this writing, C has no way of generating<BR>&nbsp;&nbsp;&nbsp;&nbsp;anonymous structure values.<BR><BR>2.11:&nbsp;&nbsp;&nbsp;&nbsp;How can I read/write structures from/to data files?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It is relatively straightforward to use fread and fwrite.<BR><BR>2.12:&nbsp;&nbsp;&nbsp;&nbsp;How can I turn off structure padding?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There is no standard method.<BR><BR>2.13:&nbsp;&nbsp;&nbsp;&nbsp;Why does sizeof report a larger size than I expect for a<BR>&nbsp;&nbsp;&nbsp;&nbsp;structure type?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The alignment of arrays of structures must be preserved.<BR><BR>2.14:&nbsp;&nbsp;&nbsp;&nbsp;How can I determine the byte offset of a field within a<BR>&nbsp;&nbsp;&nbsp;&nbsp;structure?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;ANSI C defines the offsetof() macro, which should be used if<BR>&nbsp;&nbsp;&nbsp;&nbsp;available.<BR><BR>2.15:&nbsp;&nbsp;&nbsp;&nbsp;How can I access structure fields by name at run time?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Build a table of names and offsets, using the offsetof() macro.<BR><BR>2.18:&nbsp;&nbsp;&nbsp;&nbsp;I have a program which works correctly, but dumps core after it<BR>&nbsp;&nbsp;&nbsp;&nbsp;finishes.&nbsp;&nbsp;Why?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Check to see if a structure type declaration just before main()<BR>&nbsp;&nbsp;&nbsp;&nbsp;is missing its trailing semicolon, causing main() to be declared<BR>&nbsp;&nbsp;&nbsp;&nbsp;as returning a structure.&nbsp;&nbsp;See also questions 10.9 and 16.4.<BR><BR>2.20:&nbsp;&nbsp;&nbsp;&nbsp;Can I initialize unions?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The current C Standard allows an initializer for the first-named<BR>&nbsp;&nbsp;&nbsp;&nbsp;member.<BR><BR>2.22:&nbsp;&nbsp;&nbsp;&nbsp;What is the difference between an enumeration and a set of<BR>&nbsp;&nbsp;&nbsp;&nbsp;preprocessor #defines?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;At the present time, there is little difference.&nbsp;&nbsp;The C Standard<BR>&nbsp;&nbsp;&nbsp;&nbsp;states that enumerations are compatible with integral types.<BR><BR>2.24:&nbsp;&nbsp;&nbsp;&nbsp;Is there an easy way to print enumeration values symbolically?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR><BR>Section 3. Expressions<BR><BR>3.1:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't the code "a[i] = i++;" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The variable i is both referenced and modified in the same<BR>&nbsp;&nbsp;&nbsp;&nbsp;expression.<BR><BR>3.2:&nbsp;&nbsp;&nbsp;&nbsp;Under my compiler, the code "int i = 7;<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n", i++ * i++);" prints 49.&nbsp;&nbsp;Regardless of the order<BR>&nbsp;&nbsp;&nbsp;&nbsp;of evaluation, shouldn't it print 56?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The operations implied by the postincrement and postdecrement<BR>&nbsp;&nbsp;&nbsp;&nbsp;operators ++ and -- are performed at some time after the<BR>&nbsp;&nbsp;&nbsp;&nbsp;operand's former values are yielded and before the end of the<BR>&nbsp;&nbsp;&nbsp;&nbsp;expression, but not necessarily immediately after, or before<BR>&nbsp;&nbsp;&nbsp;&nbsp;other parts of the expression are evaluated.<BR><BR>3.3:&nbsp;&nbsp;&nbsp;&nbsp;What should the code "int i = 3; i = i++;" do?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The expression is undefined.<BR><BR>3.3b:&nbsp;&nbsp;&nbsp;&nbsp;Here's a slick expression: "a ^= b ^= a ^= b".&nbsp;&nbsp;It swaps a and b<BR>&nbsp;&nbsp;&nbsp;&nbsp;without using a temporary.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not portably; its behavior is undefined.<BR><BR>3.4:&nbsp;&nbsp;&nbsp;&nbsp;Don't precedence and parentheses dictate order of evaluation?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Operator precedence and explicit parentheses impose only a<BR>&nbsp;&nbsp;&nbsp;&nbsp;partial ordering on the evaluation of an expression, which does<BR>&nbsp;&nbsp;&nbsp;&nbsp;not generally include the order of side effects.<BR><BR>3.5:&nbsp;&nbsp;&nbsp;&nbsp;But what about the && and || operators?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There is a special exception for those operators: left-to-right<BR>&nbsp;&nbsp;&nbsp;&nbsp;evaluation is guaranteed.<BR><BR>3.8:&nbsp;&nbsp;&nbsp;&nbsp;What's a "sequence point"?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;A point (at the end of a full expression, or at the ||, &&, ?:,<BR>&nbsp;&nbsp;&nbsp;&nbsp;or comma operators, or just before a function call) at which all<BR>&nbsp;&nbsp;&nbsp;&nbsp;side effects are guaranteed to be complete.<BR><BR>3.9:&nbsp;&nbsp;&nbsp;&nbsp;So given a[i] = i++; we don't know which cell of a[] gets<BR>&nbsp;&nbsp;&nbsp;&nbsp;written to, but i does get incremented by one, right?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;*No*.&nbsp;&nbsp;Once an expression or program becomes undefined, *all*<BR>&nbsp;&nbsp;&nbsp;&nbsp;aspects of it become undefined.<BR><BR>3.12:&nbsp;&nbsp;&nbsp;&nbsp;If I'm not using the value of the expression, should I use i++<BR>&nbsp;&nbsp;&nbsp;&nbsp;or ++i to increment a variable?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Since the two forms differ only in the value yielded, they are<BR>&nbsp;&nbsp;&nbsp;&nbsp;entirely equivalent when only their side effect is needed.<BR><BR>3.14:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't the code "int a = 1000, b = 1000;<BR>&nbsp;&nbsp;&nbsp;&nbsp;long int c = a * b;" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You must manually cast one of the operands to (long).<BR><BR>3.16:&nbsp;&nbsp;&nbsp;&nbsp;Can I use ?: on the left-hand side of an assignment expression?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR><BR>Section 4. Pointers<BR><BR>4.2:&nbsp;&nbsp;&nbsp;&nbsp;What's wrong with "char *p; *p = malloc(10);"?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The pointer you declared is p, not *p.<BR><BR>4.3:&nbsp;&nbsp;&nbsp;&nbsp;Does *p++ increment p, or what it points to?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;*p++ increments p.&nbsp;&nbsp;To increment the value pointed to by p, use<BR>&nbsp;&nbsp;&nbsp;&nbsp;(*p)++ .<BR><BR>4.5:&nbsp;&nbsp;&nbsp;&nbsp;I want to use a char * pointer to step over some ints.&nbsp;&nbsp;Why<BR>&nbsp;&nbsp;&nbsp;&nbsp;doesn't "((int *)p)++;" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;In C, a cast operator is a conversion operator, and by<BR>&nbsp;&nbsp;&nbsp;&nbsp;definition it yields an rvalue, which cannot be assigned to, or<BR>&nbsp;&nbsp;&nbsp;&nbsp;incremented with ++.<BR><BR>4.8:&nbsp;&nbsp;&nbsp;&nbsp;I have a function which accepts, and is supposed to initialize,<BR>&nbsp;&nbsp;&nbsp;&nbsp;a pointer, but the pointer in the caller remains unchanged.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The called function probably altered only the passed copy of the<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer.<BR><BR>4.9:&nbsp;&nbsp;&nbsp;&nbsp;Can I use a void ** pointer as a parameter so that a function<BR>&nbsp;&nbsp;&nbsp;&nbsp;can accept a generic pointer by reference?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not portably.<BR><BR>4.10:&nbsp;&nbsp;&nbsp;&nbsp;I have a function which accepts a pointer to an int.&nbsp;&nbsp;How can I<BR>&nbsp;&nbsp;&nbsp;&nbsp;pass a constant like 5 to it?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You will have to declare a temporary variable.<BR><BR>4.11:&nbsp;&nbsp;&nbsp;&nbsp;Does C even have "pass by reference"?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not really, though it can be simulated.<BR><BR>4.12:&nbsp;&nbsp;&nbsp;&nbsp;I've seen different methods used for calling functions via<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The extra parentheses and explicit * are now officially<BR>&nbsp;&nbsp;&nbsp;&nbsp;optional, although some older implementations require them.<BR><BR><BR>Section 5. Null Pointers<BR><BR>5.1:&nbsp;&nbsp;&nbsp;&nbsp;What is this infamous null pointer, anyway?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;For each pointer type, there is a special value -- the "null<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer" -- which is distinguishable from all other pointer<BR>&nbsp;&nbsp;&nbsp;&nbsp;values and which is not the address of any object or function.<BR><BR>5.2:&nbsp;&nbsp;&nbsp;&nbsp;How do I get a null pointer in my programs?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;A constant 0 in a pointer context is converted into a null<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer at compile time.&nbsp;&nbsp;A "pointer context" is an<BR>&nbsp;&nbsp;&nbsp;&nbsp;initialization, assignment, or comparison with one side a<BR>&nbsp;&nbsp;&nbsp;&nbsp;variable or expression of pointer type, and (in ANSI standard C)<BR>&nbsp;&nbsp;&nbsp;&nbsp;a function argument which has a prototype in scope declaring a<BR>&nbsp;&nbsp;&nbsp;&nbsp;certain parameter as being of pointer type.&nbsp;&nbsp;In other contexts<BR>&nbsp;&nbsp;&nbsp;&nbsp;(function arguments without prototypes, or in the variable part<BR>&nbsp;&nbsp;&nbsp;&nbsp;of variadic function calls) a constant 0 with an appropriate<BR>&nbsp;&nbsp;&nbsp;&nbsp;explicit cast is required.<BR><BR>5.3:&nbsp;&nbsp;&nbsp;&nbsp;Is the abbreviated pointer comparison "if(p)" to test for non-<BR>&nbsp;&nbsp;&nbsp;&nbsp;null pointers valid?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes.&nbsp;&nbsp;The construction "if(p)" works, regardless of the internal<BR>&nbsp;&nbsp;&nbsp;&nbsp;representation of null pointers, because the compiler<BR>&nbsp;&nbsp;&nbsp;&nbsp;essentially rewrites it as "if(p != 0)" and goes on to convert 0<BR>&nbsp;&nbsp;&nbsp;&nbsp;into the correct null pointer.<BR><BR>5.4:&nbsp;&nbsp;&nbsp;&nbsp;What is NULL and how is it #defined?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;NULL is simply a preprocessor macro, #defined as 0 (or<BR>&nbsp;&nbsp;&nbsp;&nbsp;((void *)0)), which is used (as a stylistic convention, in<BR>&nbsp;&nbsp;&nbsp;&nbsp;preference to unadorned 0's) to generate null pointers.<BR><BR>5.5:&nbsp;&nbsp;&nbsp;&nbsp;How should NULL be defined on a machine which uses a nonzero bit<BR>&nbsp;&nbsp;&nbsp;&nbsp;pattern as the internal representation of a null pointer?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The same as on any other machine: as 0.&nbsp;&nbsp;(The compiler makes the<BR>&nbsp;&nbsp;&nbsp;&nbsp;translation, upon seeing a 0, not the preprocessor; see also<BR>&nbsp;&nbsp;&nbsp;&nbsp;question 5.4.)<BR><BR>5.6:&nbsp;&nbsp;&nbsp;&nbsp;If NULL were defined as "((char *)0)," wouldn't that make<BR>&nbsp;&nbsp;&nbsp;&nbsp;function calls which pass an uncast NULL work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not in general.&nbsp;&nbsp;The complication is that there are machines<BR>&nbsp;&nbsp;&nbsp;&nbsp;which use different internal representations for pointers to<BR>&nbsp;&nbsp;&nbsp;&nbsp;different types of data.&nbsp;&nbsp;A cast is still required to tell the<BR>&nbsp;&nbsp;&nbsp;&nbsp;compiler which kind of null pointer is required, since it may be<BR>&nbsp;&nbsp;&nbsp;&nbsp;different from (char *)0.<BR><BR>5.9:&nbsp;&nbsp;&nbsp;&nbsp;If NULL and 0 are equivalent as null pointer constants, which<BR>&nbsp;&nbsp;&nbsp;&nbsp;should I use?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Either; the distinction is entirely stylistic.<BR><BR>5.10:&nbsp;&nbsp;&nbsp;&nbsp;But wouldn't it be better to use NULL, in case the value of NULL<BR>&nbsp;&nbsp;&nbsp;&nbsp;changes?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.&nbsp;&nbsp;NULL is a constant zero, so a constant zero is equally<BR>&nbsp;&nbsp;&nbsp;&nbsp;sufficient.<BR><BR>5.12:&nbsp;&nbsp;&nbsp;&nbsp;I use the preprocessor macro "#define Nullptr(type) (type *)0"<BR>&nbsp;&nbsp;&nbsp;&nbsp;to help me build null pointers of the correct type.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;This trick, though valid, does not buy much.<BR><BR>5.13:&nbsp;&nbsp;&nbsp;&nbsp;This is strange.&nbsp;&nbsp;NULL is guaranteed to be 0, but the null<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer is not?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;A "null pointer" is a language concept whose particular internal<BR>&nbsp;&nbsp;&nbsp;&nbsp;value does not matter.&nbsp;&nbsp;A null pointer is requested in source<BR>&nbsp;&nbsp;&nbsp;&nbsp;code with the character "0".&nbsp;&nbsp;"NULL" is a preprocessor macro,<BR>&nbsp;&nbsp;&nbsp;&nbsp;which is always #defined as 0 (or ((void *)0)).<BR><BR>5.14:&nbsp;&nbsp;&nbsp;&nbsp;Why is there so much confusion surrounding null pointers?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The fact that null pointers are represented both in source code,<BR>&nbsp;&nbsp;&nbsp;&nbsp;and internally to most machines, as zero invites unwarranted<BR>&nbsp;&nbsp;&nbsp;&nbsp;assumptions.&nbsp;&nbsp;The use of a preprocessor macro (NULL) may seem to<BR>&nbsp;&nbsp;&nbsp;&nbsp;suggest that the value could change some day, or on some weird<BR>&nbsp;&nbsp;&nbsp;&nbsp;machine.<BR><BR>5.15:&nbsp;&nbsp;&nbsp;&nbsp;I'm confused.&nbsp;&nbsp;I just can't understand all this null pointer<BR>&nbsp;&nbsp;&nbsp;&nbsp;stuff.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;A simple rule is, "Always use `0' or `NULL' for null pointers,<BR>&nbsp;&nbsp;&nbsp;&nbsp;and always cast them when they are used as arguments in function<BR>&nbsp;&nbsp;&nbsp;&nbsp;calls."<BR><BR>5.16:&nbsp;&nbsp;&nbsp;&nbsp;Given all the confusion surrounding null pointers, wouldn't it<BR>&nbsp;&nbsp;&nbsp;&nbsp;be easier simply to require them to be represented internally by<BR>&nbsp;&nbsp;&nbsp;&nbsp;zeroes?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Such a requirement would accomplish little.<BR><BR>5.17:&nbsp;&nbsp;&nbsp;&nbsp;Seriously, have any actual machines really used nonzero null<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Machines manufactured by Prime, Honeywell-Bull, and CDC, as well<BR>&nbsp;&nbsp;&nbsp;&nbsp;as Symbolics Lisp Machines, have done so.<BR><BR>5.20:&nbsp;&nbsp;&nbsp;&nbsp;What does a run-time "null pointer assignment" error mean?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It means that you've written, via a null pointer, to an invalid<BR>&nbsp;&nbsp;&nbsp;&nbsp;location.&nbsp;&nbsp;(See also question 16.8.)<BR><BR><BR>Section 6. Arrays and Pointers<BR><BR>6.1:&nbsp;&nbsp;&nbsp;&nbsp;I had the definition char a[6] in one source file, and in<BR>&nbsp;&nbsp;&nbsp;&nbsp;another I declared extern char *a.&nbsp;&nbsp;Why didn't it work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The declaration extern char *a simply does not match the actual<BR>&nbsp;&nbsp;&nbsp;&nbsp;definition.&nbsp;&nbsp;Use extern char a[].<BR><BR>6.2:&nbsp;&nbsp;&nbsp;&nbsp;But I heard that char a[] was identical to char *a.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not at all.&nbsp;&nbsp;Arrays are not pointers.&nbsp;&nbsp;A reference like x[3]<BR>&nbsp;&nbsp;&nbsp;&nbsp;generates different code depending on whether x is an array or a<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer.<BR><BR>6.3:&nbsp;&nbsp;&nbsp;&nbsp;So what is meant by the "equivalence of pointers and arrays" in<BR>&nbsp;&nbsp;&nbsp;&nbsp;C?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;An lvalue of type array-of-T which appears in an expression<BR>&nbsp;&nbsp;&nbsp;&nbsp;decays into a pointer to its first element; the type of the<BR>&nbsp;&nbsp;&nbsp;&nbsp;resultant pointer is pointer-to-T.&nbsp;&nbsp;So for an array a and<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer p, you can say "p = a;" and then p[3] and a[3] will<BR>&nbsp;&nbsp;&nbsp;&nbsp;access the same element.<BR><BR>6.4:&nbsp;&nbsp;&nbsp;&nbsp;Why are array and pointer declarations interchangeable as<BR>&nbsp;&nbsp;&nbsp;&nbsp;function formal parameters?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's supposed to be a convenience.<BR><BR>6.7:&nbsp;&nbsp;&nbsp;&nbsp;How can an array be an lvalue, if you can't assign to it?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;An array is not a "modifiable lvalue."<BR><BR>6.8:&nbsp;&nbsp;&nbsp;&nbsp;What is the real difference between arrays and pointers?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Arrays automatically allocate space which is fixed in size and<BR>&nbsp;&nbsp;&nbsp;&nbsp;location; pointers are dynamic.<BR><BR>6.9:&nbsp;&nbsp;&nbsp;&nbsp;Someone explained to me that arrays were really just constant<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;An array name is "constant" in that it cannot be assigned to,<BR>&nbsp;&nbsp;&nbsp;&nbsp;but an array is *not* a pointer.<BR><BR>6.11:&nbsp;&nbsp;&nbsp;&nbsp;I came across some "joke" code containing the "expression"<BR>&nbsp;&nbsp;&nbsp;&nbsp;5["abcdef"] .&nbsp;&nbsp;How can this be legal C?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes, array subscripting is commutative in C.&nbsp;&nbsp;The array<BR>&nbsp;&nbsp;&nbsp;&nbsp;subscripting operation a[e] is defined as being identical to<BR>&nbsp;&nbsp;&nbsp;&nbsp;*((a)+(e)).<BR><BR>6.12:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between array and &array?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The type.<BR><BR>6.13:&nbsp;&nbsp;&nbsp;&nbsp;How do I declare a pointer to an array?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Usually, you don't want to.&nbsp;&nbsp;Consider using a pointer to one of<BR>&nbsp;&nbsp;&nbsp;&nbsp;the array's elements instead.<BR><BR>6.14:&nbsp;&nbsp;&nbsp;&nbsp;How can I set an array's size at run time?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's straightforward to use malloc() and a pointer.<BR><BR>6.15:&nbsp;&nbsp;&nbsp;&nbsp;How can I declare local arrays of a size matching a passed-in<BR>&nbsp;&nbsp;&nbsp;&nbsp;array?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Until recently, you couldn't; array dimensions had to be compile-<BR>&nbsp;&nbsp;&nbsp;&nbsp;time constants.&nbsp;&nbsp;C9X will fix this.<BR><BR>6.16:&nbsp;&nbsp;&nbsp;&nbsp;How can I dynamically allocate a multidimensional array?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The traditional solution is to allocate an array of pointers,<BR>&nbsp;&nbsp;&nbsp;&nbsp;and then initialize each pointer to a dynamically-allocated<BR>&nbsp;&nbsp;&nbsp;&nbsp;"row."&nbsp;&nbsp;See the full list for code samples.<BR><BR>6.17:&nbsp;&nbsp;&nbsp;&nbsp;Can I simulate a non-0-based array with a pointer?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not if the pointer points outside of the block of memory it is<BR>&nbsp;&nbsp;&nbsp;&nbsp;intended to access.<BR><BR>6.18:&nbsp;&nbsp;&nbsp;&nbsp;My compiler complained when I passed a two-dimensional array to<BR>&nbsp;&nbsp;&nbsp;&nbsp;a function expecting a pointer to a pointer.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The rule by which arrays decay into pointers is not applied<BR>&nbsp;&nbsp;&nbsp;&nbsp;recursively.&nbsp;&nbsp;An array of arrays (i.e. a two-dimensional array<BR>&nbsp;&nbsp;&nbsp;&nbsp;in C) decays into a pointer to an array, not a pointer to a<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointer.<BR><BR>6.19:&nbsp;&nbsp;&nbsp;&nbsp;How do I write functions which accept two-dimensional arrays<BR>&nbsp;&nbsp;&nbsp;&nbsp;when the width is not known at compile time?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's not always particularly easy.<BR><BR>6.20:&nbsp;&nbsp;&nbsp;&nbsp;How can I use statically- and dynamically-allocated<BR>&nbsp;&nbsp;&nbsp;&nbsp;multidimensional arrays interchangeably when passing them to<BR>&nbsp;&nbsp;&nbsp;&nbsp;functions?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There is no single perfect method, but see the full list for<BR>&nbsp;&nbsp;&nbsp;&nbsp;some ideas.<BR><BR>6.21:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't sizeof properly report the size of an array which is<BR>&nbsp;&nbsp;&nbsp;&nbsp;a parameter to a function?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The sizeof operator reports the size of the pointer parameter<BR>&nbsp;&nbsp;&nbsp;&nbsp;which the function actually receives.<BR><BR><BR>Section 7. Memory Allocation<BR><BR>7.1:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't the code "char *answer; gets(answer);" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The pointer variable answer has not been set to point to any<BR>&nbsp;&nbsp;&nbsp;&nbsp;valid storage.&nbsp;&nbsp;The simplest way to correct this fragment is to<BR>&nbsp;&nbsp;&nbsp;&nbsp;use a local array, instead of a pointer.<BR><BR>7.2:&nbsp;&nbsp;&nbsp;&nbsp;I can't get strcat() to work.&nbsp;&nbsp;I tried "char *s3 =<BR>&nbsp;&nbsp;&nbsp;&nbsp;strcat(s1, s2);" but I got strange results.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Again, the main problem here is that space for the concatenated<BR>&nbsp;&nbsp;&nbsp;&nbsp;result is not properly allocated.<BR><BR>7.3:&nbsp;&nbsp;&nbsp;&nbsp;But the man page for strcat() says that it takes two char *'s as<BR>&nbsp;&nbsp;&nbsp;&nbsp;arguments.&nbsp;&nbsp;How am I supposed to know to allocate things?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;In general, when using pointers you *always* have to consider<BR>&nbsp;&nbsp;&nbsp;&nbsp;memory allocation, if only to make sure that the compiler is<BR>&nbsp;&nbsp;&nbsp;&nbsp;doing it for you.<BR><BR>7.3b:&nbsp;&nbsp;&nbsp;&nbsp;I just tried the code "char *p; strcpy(p, "abc");" and it<BR>&nbsp;&nbsp;&nbsp;&nbsp;worked.&nbsp;&nbsp;Why didn't it crash?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You got "lucky".<BR><BR>7.3c:&nbsp;&nbsp;&nbsp;&nbsp;How much memory does a pointer variable allocate?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Only enough memory to hold the pointer itself, not any memory<BR>&nbsp;&nbsp;&nbsp;&nbsp;for the pointer to point to.<BR><BR>7.5a:&nbsp;&nbsp;&nbsp;&nbsp;I have a function that is supposed to return a string, but when<BR>&nbsp;&nbsp;&nbsp;&nbsp;it returns to its caller, the returned string is garbage.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Make sure that the pointed-to memory is properly (i.e. not<BR>&nbsp;&nbsp;&nbsp;&nbsp;locally) allocated.<BR><BR>7.5b:&nbsp;&nbsp;&nbsp;&nbsp;So what's the right way to return a string?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Return a pointer to a statically-allocated buffer, a buffer<BR>&nbsp;&nbsp;&nbsp;&nbsp;passed in by the caller, or memory obtained with malloc().<BR><BR>7.6:&nbsp;&nbsp;&nbsp;&nbsp;Why am I getting "warning: assignment of pointer from integer<BR>&nbsp;&nbsp;&nbsp;&nbsp;lacks a cast" for calls to malloc()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Have you #included &lt;stdlib.h&gt;?<BR><BR>7.7:&nbsp;&nbsp;&nbsp;&nbsp;Why does some code carefully cast the values returned by malloc<BR>&nbsp;&nbsp;&nbsp;&nbsp;to the pointer type being allocated?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Before ANSI/ISO C, these casts were required to silence certain<BR>&nbsp;&nbsp;&nbsp;&nbsp;warnings.<BR><BR>7.8:&nbsp;&nbsp;&nbsp;&nbsp;Why does so much code leave out the multiplication by<BR>&nbsp;&nbsp;&nbsp;&nbsp;sizeof(char) when allocating strings?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Because sizeof(char) is, by definition, exactly 1.<BR><BR>7.14:&nbsp;&nbsp;&nbsp;&nbsp;I've heard that some operating systems don't actually allocate<BR>&nbsp;&nbsp;&nbsp;&nbsp;malloc'ed memory until the program tries to use it.&nbsp;&nbsp;Is this<BR>&nbsp;&nbsp;&nbsp;&nbsp;legal?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's hard to say.<BR><BR>7.16:&nbsp;&nbsp;&nbsp;&nbsp;I'm allocating a large array for some numeric work, but malloc()<BR>&nbsp;&nbsp;&nbsp;&nbsp;is acting strangely.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Make sure the number you're trying to pass to malloc() isn't<BR>&nbsp;&nbsp;&nbsp;&nbsp;bigger than a size_t can hold.<BR><BR>7.17:&nbsp;&nbsp;&nbsp;&nbsp;I've got 8 meg of memory in my PC.&nbsp;&nbsp;Why can I only seem to<BR>&nbsp;&nbsp;&nbsp;&nbsp;malloc 640K or so?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Under the segmented architecture of PC compatibles, it can be<BR>&nbsp;&nbsp;&nbsp;&nbsp;difficult to use more than 640K with any degree of transparency.<BR>&nbsp;&nbsp;&nbsp;&nbsp;See also question 19.23.<BR><BR>7.19:&nbsp;&nbsp;&nbsp;&nbsp;My program is crashing, apparently somewhere down inside malloc.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Make sure you aren't using more memory than you malloc'ed,<BR>&nbsp;&nbsp;&nbsp;&nbsp;especially for strings (which need strlen(str) + 1 bytes).<BR><BR>7.20:&nbsp;&nbsp;&nbsp;&nbsp;You can't use dynamically-allocated memory after you free it,<BR>&nbsp;&nbsp;&nbsp;&nbsp;can you?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.&nbsp;&nbsp;Some early documentation implied otherwise, but the claim<BR>&nbsp;&nbsp;&nbsp;&nbsp;is no longer valid.<BR><BR>7.21:&nbsp;&nbsp;&nbsp;&nbsp;Why isn't a pointer null after calling free()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;C's pass-by-value semantics mean that called functions can never<BR>&nbsp;&nbsp;&nbsp;&nbsp;permanently change the values of their arguments.<BR><BR>7.22:&nbsp;&nbsp;&nbsp;&nbsp;When I call malloc() to allocate memory for a local pointer, do<BR>&nbsp;&nbsp;&nbsp;&nbsp;I have to explicitly free() it?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes.<BR><BR>7.23:&nbsp;&nbsp;&nbsp;&nbsp;When I free a dynamically-allocated structure containing<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers, do I also have to free each subsidiary pointer?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes.<BR><BR>7.24:&nbsp;&nbsp;&nbsp;&nbsp;Must I free allocated memory before the program exits?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You shouldn't have to.<BR><BR>7.25:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't my program's memory usage go down when I free<BR>&nbsp;&nbsp;&nbsp;&nbsp;memory?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Most implementations of malloc/free do not return freed memory<BR>&nbsp;&nbsp;&nbsp;&nbsp;to the operating system.<BR><BR>7.26:&nbsp;&nbsp;&nbsp;&nbsp;How does free() know how many bytes to free?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The malloc/free implementation remembers the size of each block<BR>&nbsp;&nbsp;&nbsp;&nbsp;as it is allocated.<BR><BR>7.27:&nbsp;&nbsp;&nbsp;&nbsp;So can I query the malloc package to find out how big an<BR>&nbsp;&nbsp;&nbsp;&nbsp;allocated block is?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Not portably.<BR><BR>7.30:&nbsp;&nbsp;&nbsp;&nbsp;Is it legal to pass a null pointer as the first argument to<BR>&nbsp;&nbsp;&nbsp;&nbsp;realloc()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;ANSI C sanctions this usage, although several earlier<BR>&nbsp;&nbsp;&nbsp;&nbsp;implementations do not support it.<BR><BR>7.31:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between calloc() and malloc()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;calloc() takes two arguments, and initializes the allocated<BR>&nbsp;&nbsp;&nbsp;&nbsp;memory to all-bits-0.<BR><BR>7.32:&nbsp;&nbsp;&nbsp;&nbsp;What is alloca() and why is its use discouraged?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;alloca() allocates memory which is automatically freed when the<BR>&nbsp;&nbsp;&nbsp;&nbsp;function which called alloca() returns.&nbsp;&nbsp;alloca() cannot be<BR>&nbsp;&nbsp;&nbsp;&nbsp;written portably, is difficult to implement on machines without<BR>&nbsp;&nbsp;&nbsp;&nbsp;a stack, and fails under certain conditions if implemented<BR>&nbsp;&nbsp;&nbsp;&nbsp;simply.<BR><BR><BR>Section 8. Characters and Strings<BR><BR>8.1:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't "strcat(string, '!');" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;strcat() concatenates *strings*, not characters.<BR><BR>8.2:&nbsp;&nbsp;&nbsp;&nbsp;Why won't the test if(string == "value") correctly compare<BR>&nbsp;&nbsp;&nbsp;&nbsp;string against the value?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's comparing pointers.&nbsp;&nbsp;To compare two strings, use strcmp().<BR><BR>8.3:&nbsp;&nbsp;&nbsp;&nbsp;Why can't I assign strings to character arrays?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Strings are arrays, and you can't assign arrays directly.&nbsp;&nbsp;Use<BR>&nbsp;&nbsp;&nbsp;&nbsp;strcpy() instead.<BR><BR>8.6:&nbsp;&nbsp;&nbsp;&nbsp;How can I get the numeric (character set) value corresponding to<BR>&nbsp;&nbsp;&nbsp;&nbsp;a character?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;In C, if you have the character, you have its value.<BR><BR>8.9:&nbsp;&nbsp;&nbsp;&nbsp;Why is sizeof('a') not 1?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Character constants in C are of type int.<BR><BR><BR>Section 9. Boolean Expressions and Variables<BR><BR>9.1:&nbsp;&nbsp;&nbsp;&nbsp;What is the right type to use for Boolean values in C?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There's no one right answer; see the full list for some<BR>&nbsp;&nbsp;&nbsp;&nbsp;discussion.<BR><BR>9.2:&nbsp;&nbsp;&nbsp;&nbsp;What if a built-in logical or relational operator "returns"<BR>&nbsp;&nbsp;&nbsp;&nbsp;something other than 1?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;When a Boolean value is generated by a built-in operator, it is<BR>&nbsp;&nbsp;&nbsp;&nbsp;guaranteed to be 1 or 0.&nbsp;&nbsp;(This is *not* true for some library<BR>&nbsp;&nbsp;&nbsp;&nbsp;routines such as isalpha.)<BR><BR>9.3:&nbsp;&nbsp;&nbsp;&nbsp;Is if(p), where p is a pointer, valid?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes.&nbsp;&nbsp;See question 5.3.<BR><BR><BR>Section 10. C Preprocessor<BR><BR>10.2:&nbsp;&nbsp;&nbsp;&nbsp;I've got some cute preprocessor macros that let me write C code<BR>&nbsp;&nbsp;&nbsp;&nbsp;that looks more like Pascal.&nbsp;&nbsp;What do y'all think?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Bleah.<BR><BR>10.3:&nbsp;&nbsp;&nbsp;&nbsp;How can I write a generic macro to swap two values?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There is no good answer to this question.&nbsp;&nbsp;The best all-around<BR>&nbsp;&nbsp;&nbsp;&nbsp;solution is probably to forget about using a macro.<BR><BR>10.4:&nbsp;&nbsp;&nbsp;&nbsp;What's the best way to write a multi-statement macro?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;#define Func() do {stmt1; stmt2; ... } while(0)&nbsp;&nbsp;&nbsp;&nbsp;/* (no trailing ;) */<BR><BR>10.6:&nbsp;&nbsp;&nbsp;&nbsp;What are .h files and what should I put in them?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Header files (also called ".h files") should generally contain<BR>&nbsp;&nbsp;&nbsp;&nbsp;common declarations and macro, structure, and typedef<BR>&nbsp;&nbsp;&nbsp;&nbsp;definitions, but not variable or function definitions.<BR><BR>10.7:&nbsp;&nbsp;&nbsp;&nbsp;Is it acceptable for one header file to #include another?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's a question of style, and thus receives considerable debate.<BR><BR>10.8a:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between #include &lt;&gt; and #include "" ?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Roughly speaking, the &lt;&gt; syntax is for Standard headers and ""<BR>&nbsp;&nbsp;&nbsp;&nbsp;is for project headers.<BR><BR>10.8b:&nbsp;&nbsp;&nbsp;&nbsp;What are the complete rules for header file searching?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The exact behavior is implementation-defined; see the full list<BR>&nbsp;&nbsp;&nbsp;&nbsp;for some discussion.<BR><BR>10.9:&nbsp;&nbsp;&nbsp;&nbsp;I'm getting strange syntax errors on the very first declaration<BR>&nbsp;&nbsp;&nbsp;&nbsp;in a file, but it looks fine.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Perhaps there's a missing semicolon at the end of the last<BR>&nbsp;&nbsp;&nbsp;&nbsp;declaration in the last header file you're #including.<BR><BR>10.10b:&nbsp;&nbsp;&nbsp;&nbsp;I'm #including the header file for a function, but the linker<BR>&nbsp;&nbsp;&nbsp;&nbsp;keeps saying it's undefined.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See question 13.25.<BR><BR>10.11:&nbsp;&nbsp;&nbsp;&nbsp;Where can I get a copy of a missing header file?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Contact your vendor, or see question 18.16 or the full list.<BR><BR>10.12:&nbsp;&nbsp;&nbsp;&nbsp;How can I construct preprocessor #if expressions which compare<BR>&nbsp;&nbsp;&nbsp;&nbsp;strings?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You can't do it directly; try #defining several manifest<BR>&nbsp;&nbsp;&nbsp;&nbsp;constants and implementing conditionals on those.<BR><BR>10.13:&nbsp;&nbsp;&nbsp;&nbsp;Does the sizeof operator work in preprocessor #if directives?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR>10.14:&nbsp;&nbsp;&nbsp;&nbsp;Can I use an #ifdef in a #define line, to define something two<BR>&nbsp;&nbsp;&nbsp;&nbsp;different ways?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR>10.15:&nbsp;&nbsp;&nbsp;&nbsp;Is there anything like an #ifdef for typedefs?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Unfortunately, no.<BR><BR>10.16:&nbsp;&nbsp;&nbsp;&nbsp;How can I use a preprocessor #if expression to detect<BR>&nbsp;&nbsp;&nbsp;&nbsp;endianness?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You probably can't.<BR><BR>10.18:&nbsp;&nbsp;&nbsp;&nbsp;How can I preprocess some code to remove selected conditional<BR>&nbsp;&nbsp;&nbsp;&nbsp;compilations, without preprocessing everything?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Look for a program called unifdef, rmifdef, or scpp.<BR><BR>10.19:&nbsp;&nbsp;&nbsp;&nbsp;How can I list all of the predefined identifiers?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;If the compiler documentation is unhelpful, try extracting<BR>&nbsp;&nbsp;&nbsp;&nbsp;printable strings from the compiler or preprocessor executable.<BR><BR>10.20:&nbsp;&nbsp;&nbsp;&nbsp;I have some old code that tries to construct identifiers with a<BR>&nbsp;&nbsp;&nbsp;&nbsp;macro like "#define Paste(a, b) a/**/b", but it doesn't work any<BR>&nbsp;&nbsp;&nbsp;&nbsp;more.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Try the ANSI token-pasting operator ##.<BR><BR>10.22:&nbsp;&nbsp;&nbsp;&nbsp;What does the message "warning: macro replacement within a<BR>&nbsp;&nbsp;&nbsp;&nbsp;string literal" mean?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See question 11.18.<BR><BR>10.23-4: I'm having trouble using macro arguments inside string<BR>&nbsp;&nbsp;&nbsp;&nbsp;literals, using the `#' operator.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See questions 11.17 and 11.18.<BR><BR>10.25:&nbsp;&nbsp;&nbsp;&nbsp;I've got this tricky preprocessing I want to do and I can't<BR>&nbsp;&nbsp;&nbsp;&nbsp;figure out a way to do it.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Consider writing your own little special-purpose preprocessing<BR>&nbsp;&nbsp;&nbsp;&nbsp;tool, instead.<BR><BR>10.26:&nbsp;&nbsp;&nbsp;&nbsp;How can I write a macro which takes a variable number of<BR>&nbsp;&nbsp;&nbsp;&nbsp;arguments?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Here is one popular trick.&nbsp;&nbsp;Note that the parentheses around<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf's argument list are in the macro call, not the<BR>&nbsp;&nbsp;&nbsp;&nbsp;definition.<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#define DEBUG(args) (printf("DEBUG: "), printf args)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(n != 0) DEBUG(("n is %d\n", n));<BR><BR><BR>Section 11. ANSI/ISO Standard C<BR><BR>11.1:&nbsp;&nbsp;&nbsp;&nbsp;What is the "ANSI C Standard?"<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;In 1983, the American National Standards Institute (ANSI)<BR>&nbsp;&nbsp;&nbsp;&nbsp;commissioned a committee to standardize the C language.&nbsp;&nbsp;Their<BR>&nbsp;&nbsp;&nbsp;&nbsp;work was ratified as ANS X3.159-1989, and has since been adopted<BR>&nbsp;&nbsp;&nbsp;&nbsp;as ISO/IEC 9899:1990, and later amended.<BR><BR>11.2:&nbsp;&nbsp;&nbsp;&nbsp;How can I get a copy of the Standard?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Copies are available from ANSI in New York, or from Global<BR>&nbsp;&nbsp;&nbsp;&nbsp;Engineering Documents in Englewood, CO, or from any national<BR>&nbsp;&nbsp;&nbsp;&nbsp;standards body, or from ISO in Geneva, or republished within one<BR>&nbsp;&nbsp;&nbsp;&nbsp;or more books.&nbsp;&nbsp;See the unabridged list for details.<BR><BR>11.2b:&nbsp;&nbsp;&nbsp;&nbsp;Where can I get information about updates to the Standard?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See the full list for pointers.<BR><BR>11.3:&nbsp;&nbsp;&nbsp;&nbsp;My ANSI compiler is complaining about prototype mismatches for<BR>&nbsp;&nbsp;&nbsp;&nbsp;parameters declared float.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You have mixed the new-style prototype declaration<BR>&nbsp;&nbsp;&nbsp;&nbsp;"extern int func(float);" with the old-style definition<BR>&nbsp;&nbsp;&nbsp;&nbsp;"int func(x) float x;".&nbsp;&nbsp;"Narrow" types are treated differently<BR>&nbsp;&nbsp;&nbsp;&nbsp;according to which syntax is used.&nbsp;&nbsp;This problem can be fixed by<BR>&nbsp;&nbsp;&nbsp;&nbsp;avoiding narrow types, or by using either new-style (prototype)<BR>&nbsp;&nbsp;&nbsp;&nbsp;or old-style syntax consistently.<BR><BR>11.4:&nbsp;&nbsp;&nbsp;&nbsp;Can you mix old-style and new-style function syntax?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Doing so is currently legal, for most argument types<BR>&nbsp;&nbsp;&nbsp;&nbsp;(see question 11.3).<BR><BR>11.5:&nbsp;&nbsp;&nbsp;&nbsp;Why does the declaration "extern int f(struct x *p);" give me a<BR>&nbsp;&nbsp;&nbsp;&nbsp;warning message?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;A structure declared (or even mentioned) for the first time<BR>&nbsp;&nbsp;&nbsp;&nbsp;within a prototype cannot be compatible with other structures<BR>&nbsp;&nbsp;&nbsp;&nbsp;declared in the same source file.<BR><BR>11.8:&nbsp;&nbsp;&nbsp;&nbsp;Why can't I use const values in initializers and array<BR>&nbsp;&nbsp;&nbsp;&nbsp;dimensions?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The value of a const-qualified object is *not* a constant<BR>&nbsp;&nbsp;&nbsp;&nbsp;expression in the full sense of the term.<BR><BR>11.9:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between "const char *p" and<BR>&nbsp;&nbsp;&nbsp;&nbsp;"char * const p"?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The former declares a pointer to a constant character; the<BR>&nbsp;&nbsp;&nbsp;&nbsp;latter declares a constant pointer to a character.<BR><BR>11.10:&nbsp;&nbsp;&nbsp;&nbsp;Why can't I pass a char ** to a function which expects a<BR>&nbsp;&nbsp;&nbsp;&nbsp;const char **?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The rule which permits slight mismatches in qualified pointer<BR>&nbsp;&nbsp;&nbsp;&nbsp;assignments is not applied recursively.<BR><BR>11.12a:&nbsp;&nbsp;&nbsp;&nbsp;What's the correct declaration of main()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;int main(int argc, char *argv[]) .<BR><BR>11.12b:&nbsp;&nbsp;&nbsp;&nbsp;Can I declare main() as void, to shut off these annoying "main<BR>&nbsp;&nbsp;&nbsp;&nbsp;returns no value" messages?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR>11.13:&nbsp;&nbsp;&nbsp;&nbsp;But what about main's third argument, envp?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's a non-standard (though common) extension.<BR><BR>11.14:&nbsp;&nbsp;&nbsp;&nbsp;I believe that declaring void main() can't fail, since I'm<BR>&nbsp;&nbsp;&nbsp;&nbsp;calling exit() instead of returning.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It doesn't matter whether main() returns or not, the problem is<BR>&nbsp;&nbsp;&nbsp;&nbsp;that its caller may not even be able to *call* it correctly.<BR><BR>11.15:&nbsp;&nbsp;&nbsp;&nbsp;The book I've been using always uses void main().<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's wrong.<BR><BR>11.16:&nbsp;&nbsp;&nbsp;&nbsp;Is exit(status) truly equivalent to returning the same status<BR>&nbsp;&nbsp;&nbsp;&nbsp;from main()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes and no.&nbsp;&nbsp;(See the full list for details.)<BR><BR>11.17:&nbsp;&nbsp;&nbsp;&nbsp;How do I get the ANSI "stringizing" preprocessing operator `#'<BR>&nbsp;&nbsp;&nbsp;&nbsp;to stringize the macro's value instead of its name?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You can use a two-step #definition to force a macro to be<BR>&nbsp;&nbsp;&nbsp;&nbsp;expanded as well as stringized.<BR><BR>11.18:&nbsp;&nbsp;&nbsp;&nbsp;What does the message "warning: macro replacement within a<BR>&nbsp;&nbsp;&nbsp;&nbsp;string literal" mean?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Some pre-ANSI compilers/preprocessors expanded macro parameters<BR>&nbsp;&nbsp;&nbsp;&nbsp;even inside string literals and character constants.<BR><BR>11.19:&nbsp;&nbsp;&nbsp;&nbsp;I'm getting strange syntax errors inside lines I've #ifdeffed<BR>&nbsp;&nbsp;&nbsp;&nbsp;out.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Under ANSI C, #ifdeffed-out text must still consist of "valid<BR>&nbsp;&nbsp;&nbsp;&nbsp;preprocessing tokens."&nbsp;&nbsp;This means that there must be no<BR>&nbsp;&nbsp;&nbsp;&nbsp;newlines inside quotes, and no unterminated comments or quotes<BR>&nbsp;&nbsp;&nbsp;&nbsp;(i.e. no single apostrophes).<BR><BR>11.20:&nbsp;&nbsp;&nbsp;&nbsp;What are #pragmas ?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The #pragma directive provides a single, well-defined "escape<BR>&nbsp;&nbsp;&nbsp;&nbsp;hatch" which can be used for extensions.<BR><BR>11.21:&nbsp;&nbsp;&nbsp;&nbsp;What does "#pragma once" mean?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It is an extension implemented by some preprocessors to help<BR>&nbsp;&nbsp;&nbsp;&nbsp;make header files idempotent.<BR><BR>11.22:&nbsp;&nbsp;&nbsp;&nbsp;Is char a[3] = "abc"; legal?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Yes, in ANSI C.<BR><BR>11.24:&nbsp;&nbsp;&nbsp;&nbsp;Why can't I perform arithmetic on a void * pointer?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The compiler doesn't know the size of the pointed-to objects.<BR><BR>11.25:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between memcpy() and memmove()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;memmove() offers guaranteed behavior if the source and<BR>&nbsp;&nbsp;&nbsp;&nbsp;destination arguments overlap.<BR><BR>11.26:&nbsp;&nbsp;&nbsp;&nbsp;What should malloc(0) do?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The behavior is implementation-defined.<BR><BR>11.27:&nbsp;&nbsp;&nbsp;&nbsp;Why does the ANSI Standard not guarantee more than six case-<BR>&nbsp;&nbsp;&nbsp;&nbsp;insensitive characters of external identifier significance?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The problem is older linkers which cannot be forced (by mere<BR>&nbsp;&nbsp;&nbsp;&nbsp;words in a Standard) to upgrade.<BR><BR>11.29:&nbsp;&nbsp;&nbsp;&nbsp;My compiler is rejecting the simplest possible test programs,<BR>&nbsp;&nbsp;&nbsp;&nbsp;with all kinds of syntax errors.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Perhaps it is a pre-ANSI compiler.<BR><BR>11.30:&nbsp;&nbsp;&nbsp;&nbsp;Why are some ANSI/ISO Standard library functions showing up as<BR>&nbsp;&nbsp;&nbsp;&nbsp;undefined, even though I've got an ANSI compiler?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Perhaps you don't have ANSI-compatible headers and libraries.<BR><BR>11.31:&nbsp;&nbsp;&nbsp;&nbsp;Does anyone have a tool for converting old-style C programs to<BR>&nbsp;&nbsp;&nbsp;&nbsp;ANSI C, or for automatically generating prototypes?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See the full list for details.<BR><BR>11.32:&nbsp;&nbsp;&nbsp;&nbsp;Why won't frobozz-cc, which claims to be ANSI compliant, accept<BR>&nbsp;&nbsp;&nbsp;&nbsp;this code?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Are you sure that the code being rejected doesn't rely on some<BR>&nbsp;&nbsp;&nbsp;&nbsp;non-Standard extension?<BR><BR>11.33:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between implementation-defined,<BR>&nbsp;&nbsp;&nbsp;&nbsp;unspecified, and undefined behavior?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;If you're writing portable code, ignore the distinctions.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Otherwise, see the full list.<BR><BR>11.34:&nbsp;&nbsp;&nbsp;&nbsp;I'm appalled that the ANSI Standard leaves so many issues<BR>&nbsp;&nbsp;&nbsp;&nbsp;undefined.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;In most of these cases, the Standard is simply codifying<BR>&nbsp;&nbsp;&nbsp;&nbsp;existing practice.<BR><BR>11.35:&nbsp;&nbsp;&nbsp;&nbsp;I just tried some allegedly-undefined code on an ANSI-conforming<BR>&nbsp;&nbsp;&nbsp;&nbsp;compiler, and got the results I expected.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;A compiler may do anything it likes when faced with undefined<BR>&nbsp;&nbsp;&nbsp;&nbsp;behavior, including doing what you expect.<BR><BR><BR>Section 12. Stdio<BR><BR>12.1:&nbsp;&nbsp;&nbsp;&nbsp;What's wrong with the code "char c; while((c = getchar()) !=<BR>&nbsp;&nbsp;&nbsp;&nbsp;EOF) ..."?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The variable to hold getchar's return value must be an int.<BR><BR>12.2:&nbsp;&nbsp;&nbsp;&nbsp;Why won't the code "while(!feof(infp)) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;fgets(buf, MAXLINE, infp); fputs(buf, outfp); }" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;EOF is only indicated *after* an input routine fails.<BR><BR>12.4:&nbsp;&nbsp;&nbsp;&nbsp;My program's prompts and intermediate output don't always show<BR>&nbsp;&nbsp;&nbsp;&nbsp;up on the screen.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It's best to use an explicit fflush(stdout) whenever output<BR>&nbsp;&nbsp;&nbsp;&nbsp;should definitely be visible.<BR><BR>12.5:&nbsp;&nbsp;&nbsp;&nbsp;How can I read one character at a time, without waiting for the<BR>&nbsp;&nbsp;&nbsp;&nbsp;RETURN key?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See question 19.1.<BR><BR>12.6:&nbsp;&nbsp;&nbsp;&nbsp;How can I print a '%' character with printf?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;"%%".<BR><BR>12.9:&nbsp;&nbsp;&nbsp;&nbsp;How can printf() use %f for type double, if scanf() requires<BR>&nbsp;&nbsp;&nbsp;&nbsp;%lf?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;C's "default argument promotions" mean that values of type float<BR>&nbsp;&nbsp;&nbsp;&nbsp;are promoted to double.<BR><BR>12.9b:&nbsp;&nbsp;&nbsp;&nbsp;What printf format should I use for a typedef when I don't know<BR>&nbsp;&nbsp;&nbsp;&nbsp;the underlying type?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Use a cast to convert the value to a known type, then use the<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf format matching that type.<BR><BR>12.10:&nbsp;&nbsp;&nbsp;&nbsp;How can I implement a variable field width with printf?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Use printf("%*d", width, x).<BR><BR>12.11:&nbsp;&nbsp;&nbsp;&nbsp;How can I print numbers with commas separating the thousands?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There is no standard routine (but see &lt;locale.h&gt;).<BR><BR>12.12:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't the call scanf("%d", i) work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The arguments you pass to scanf() must always be pointers.<BR><BR>12.13:&nbsp;&nbsp;&nbsp;&nbsp;Why doesn't the code "double d; scanf("%f", &d);" work?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Unlike printf(), scanf() uses %lf for double, and %f for float.<BR><BR>12.15:&nbsp;&nbsp;&nbsp;&nbsp;How can I specify a variable width in a scanf() format string?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You can't.<BR><BR>12.17:&nbsp;&nbsp;&nbsp;&nbsp;When I read numbers from the keyboard with scanf "%d\n", it<BR>&nbsp;&nbsp;&nbsp;&nbsp;seems to hang until I type one extra line of input.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Try using "%d" instead of "%d\n".<BR><BR>12.18:&nbsp;&nbsp;&nbsp;&nbsp;I'm reading a number with scanf %d and then a string with<BR>&nbsp;&nbsp;&nbsp;&nbsp;gets(), but the compiler seems to be skipping the call to<BR>&nbsp;&nbsp;&nbsp;&nbsp;gets()!<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;scanf() and gets() do not work well together.<BR><BR>12.19:&nbsp;&nbsp;&nbsp;&nbsp;I'm re-prompting the user if scanf() fails, but sometimes it<BR>&nbsp;&nbsp;&nbsp;&nbsp;seems to go into an infinite loop.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;scanf() tends to "jam" on bad input since it does not discard<BR>&nbsp;&nbsp;&nbsp;&nbsp;it.<BR><BR>12.20:&nbsp;&nbsp;&nbsp;&nbsp;Why does everyone say not to use scanf()?&nbsp;&nbsp;What should I use<BR>&nbsp;&nbsp;&nbsp;&nbsp;instead?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;scanf() has a number of problems.&nbsp;&nbsp;Usually, it's easier to read<BR>&nbsp;&nbsp;&nbsp;&nbsp;entire lines and then interpret them.<BR><BR>12.21:&nbsp;&nbsp;&nbsp;&nbsp;How can I tell how much destination buffer space I'll need for<BR>&nbsp;&nbsp;&nbsp;&nbsp;an arbitrary sprintf call?&nbsp;&nbsp;How can I avoid overflowing the<BR>&nbsp;&nbsp;&nbsp;&nbsp;destination buffer with sprintf()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Use the new snprintf() function, if you can.<BR><BR>12.23:&nbsp;&nbsp;&nbsp;&nbsp;Why does everyone say not to use gets()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It cannot be prevented from overflowing the input buffer.<BR><BR>12.24:&nbsp;&nbsp;&nbsp;&nbsp;Why does errno contain ENOTTY after a call to printf()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Don't worry about it.&nbsp;&nbsp;It is only meaningful for a program to<BR>&nbsp;&nbsp;&nbsp;&nbsp;inspect the contents of errno after an error has been reported.<BR><BR>12.25:&nbsp;&nbsp;&nbsp;&nbsp;What's the difference between fgetpos/fsetpos and ftell/fseek?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;fgetpos() and fsetpos() use a special typedef which may allow<BR>&nbsp;&nbsp;&nbsp;&nbsp;them to work with larger files than ftell() and fseek().<BR><BR>12.26:&nbsp;&nbsp;&nbsp;&nbsp;Will fflush(stdin) flush unread characters from the standard<BR>&nbsp;&nbsp;&nbsp;&nbsp;input stream?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No.<BR><BR>12.30:&nbsp;&nbsp;&nbsp;&nbsp;I'm trying to update a file in place, by using fopen mode "r+",<BR>&nbsp;&nbsp;&nbsp;&nbsp;but it's not working.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Be sure to call fseek between reading and writing.<BR><BR>12.33:&nbsp;&nbsp;&nbsp;&nbsp;How can I redirect stdin or stdout from within a program?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Use freopen().<BR><BR>12.34:&nbsp;&nbsp;&nbsp;&nbsp;Once I've used freopen(), how can I get the original stream<BR>&nbsp;&nbsp;&nbsp;&nbsp;back?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There isn't a good way.&nbsp;&nbsp;Try avoiding freopen.<BR><BR>12.36b:&nbsp;&nbsp;&nbsp;&nbsp;How can I arrange to have output go two places at once?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You could write your own printf variant which printed everything<BR>&nbsp;&nbsp;&nbsp;&nbsp;twice.&nbsp;&nbsp;See question 15.5.<BR><BR>12.38:&nbsp;&nbsp;&nbsp;&nbsp;How can I read a binary data file properly?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Be sure to specify "rb" mode when calling fopen().<BR><BR><BR>Section 13. Library Functions<BR><BR>13.1:&nbsp;&nbsp;&nbsp;&nbsp;How can I convert numbers to strings?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Just use sprintf().<BR><BR>13.2:&nbsp;&nbsp;&nbsp;&nbsp;Why does strncpy() not always write a '\0'?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;For mildly-interesting historical reasons.<BR><BR>13.5:&nbsp;&nbsp;&nbsp;&nbsp;Why do some versions of toupper() act strangely if given an<BR>&nbsp;&nbsp;&nbsp;&nbsp;upper-case letter?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Older versions of toupper() and tolower() did not always work as<BR>&nbsp;&nbsp;&nbsp;&nbsp;expected in this regard.<BR><BR>13.6:&nbsp;&nbsp;&nbsp;&nbsp;How can I split up a string into whitespace-separated fields?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Try strtok().<BR><BR>13.7:&nbsp;&nbsp;&nbsp;&nbsp;I need some code to do regular expression and wildcard matching.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;regexp libraries abound; see the full list for details.<BR><BR>13.8:&nbsp;&nbsp;&nbsp;&nbsp;I'm trying to sort an array of strings with qsort(), using<BR>&nbsp;&nbsp;&nbsp;&nbsp;strcmp() as the comparison function, but it's not working.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You'll have to write a "helper" comparison function which takes<BR>&nbsp;&nbsp;&nbsp;&nbsp;two generic pointer arguments, converts them to char **, and<BR>&nbsp;&nbsp;&nbsp;&nbsp;dereferences them, yielding char *'s which can be usefully<BR>&nbsp;&nbsp;&nbsp;&nbsp;compared.<BR><BR>13.9:&nbsp;&nbsp;&nbsp;&nbsp;Now I'm trying to sort an array of structures, but the compiler<BR>&nbsp;&nbsp;&nbsp;&nbsp;is complaining that the function is of the wrong type for<BR>&nbsp;&nbsp;&nbsp;&nbsp;qsort().<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The comparison function must be declared as accepting "generic<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers" (const void *) which it then converts to structure<BR>&nbsp;&nbsp;&nbsp;&nbsp;pointers.<BR><BR>13.10:&nbsp;&nbsp;&nbsp;&nbsp;How can I sort a linked list?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Algorithms like insertion sort and merge sort work well, or you<BR>&nbsp;&nbsp;&nbsp;&nbsp;can keep the list in order as you build it.<BR><BR>13.11:&nbsp;&nbsp;&nbsp;&nbsp;How can I sort more data than will fit in memory?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You want an "external sort"; see the full list for details.<BR><BR>13.12:&nbsp;&nbsp;&nbsp;&nbsp;How can I get the time of day in a C program?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Just use the time(), ctime(), localtime() and/or strftime()<BR>&nbsp;&nbsp;&nbsp;&nbsp;functions.<BR><BR>13.13:&nbsp;&nbsp;&nbsp;&nbsp;How can I convert a struct tm or a string into a time_t?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The ANSI mktime() function converts a struct tm to a time_t.&nbsp;&nbsp;No<BR>&nbsp;&nbsp;&nbsp;&nbsp;standard routine exists to parse strings.<BR><BR>13.14:&nbsp;&nbsp;&nbsp;&nbsp;How can I perform calendar manipulations?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The ANSI/ISO Standard C mktime() and difftime() functions<BR>&nbsp;&nbsp;&nbsp;&nbsp;provide some support for both problems.<BR><BR>13.14b:&nbsp;&nbsp;&nbsp;&nbsp;Does C have any Year 2000 problems?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;No, although poorly-written C programs do.&nbsp;&nbsp;Make sure you know<BR>&nbsp;&nbsp;&nbsp;&nbsp;that tm_year holds the value of the year minus 1900.<BR><BR>13.15:&nbsp;&nbsp;&nbsp;&nbsp;I need a random number generator.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The Standard C library has one: rand().<BR><BR>13.16:&nbsp;&nbsp;&nbsp;&nbsp;How can I get random integers in a certain range?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;One method is something like<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(int)((double)rand() / ((double)RAND_MAX + 1) * N)<BR><BR>13.17:&nbsp;&nbsp;&nbsp;&nbsp;Each time I run my program, I get the same sequence of numbers<BR>&nbsp;&nbsp;&nbsp;&nbsp;back from rand().<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You can call srand() to seed the pseudo-random number generator<BR>&nbsp;&nbsp;&nbsp;&nbsp;with a truly random initial value.<BR><BR>13.18:&nbsp;&nbsp;&nbsp;&nbsp;I need a random true/false value, so I'm just taking rand() % 2,<BR>&nbsp;&nbsp;&nbsp;&nbsp;but it's alternating 0, 1, 0, 1, 0...<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Try using the higher-order bits: see question 13.16.<BR><BR>13.20:&nbsp;&nbsp;&nbsp;&nbsp;How can I generate random numbers with a normal or Gaussian<BR>&nbsp;&nbsp;&nbsp;&nbsp;distribution?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See the longer versions of this list for ideas.<BR><BR>13.24:&nbsp;&nbsp;&nbsp;&nbsp;I'm trying to port this old program.&nbsp;&nbsp;Why do I get "undefined<BR>&nbsp;&nbsp;&nbsp;&nbsp;external" errors for some library functions?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Some semistandard functions have been renamed or replaced over<BR>&nbsp;&nbsp;&nbsp;&nbsp;the years; see the full list for details.<BR><BR>13.25:&nbsp;&nbsp;&nbsp;&nbsp;I get errors due to library functions being undefined even<BR>&nbsp;&nbsp;&nbsp;&nbsp;though I #include the right header files.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You may have to explicitly ask for the correct libraries to be<BR>&nbsp;&nbsp;&nbsp;&nbsp;searched.<BR><BR>13.26:&nbsp;&nbsp;&nbsp;&nbsp;I'm still getting errors due to library functions being<BR>&nbsp;&nbsp;&nbsp;&nbsp;undefined, even though I'm requesting the right libraries.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Library search order is significant; usually, you must search<BR>&nbsp;&nbsp;&nbsp;&nbsp;the libraries last.<BR><BR>13.28:&nbsp;&nbsp;&nbsp;&nbsp;What does it mean when the linker says that _end is undefined?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You generally get that message only when other symbols are<BR>&nbsp;&nbsp;&nbsp;&nbsp;undefined, too.<BR><BR><BR>Section 14. Floating Point<BR><BR>14.1:&nbsp;&nbsp;&nbsp;&nbsp;When I set a float variable to 3.1, why is printf printing it as<BR>&nbsp;&nbsp;&nbsp;&nbsp;3.0999999?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Most computers use base 2 for floating-point numbers, and many<BR>&nbsp;&nbsp;&nbsp;&nbsp;fractions (including 0.1 decimal) are not exactly representable<BR>&nbsp;&nbsp;&nbsp;&nbsp;in base 2.<BR><BR>14.2:&nbsp;&nbsp;&nbsp;&nbsp;Why is sqrt(144.) giving me crazy numbers?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Make sure that you have #included &lt;math.h&gt;, and correctly<BR>&nbsp;&nbsp;&nbsp;&nbsp;declared other functions returning double.<BR><BR>14.3:&nbsp;&nbsp;&nbsp;&nbsp;I keep getting "undefined: sin" compilation errors.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Make sure you're actually linking with the math library.<BR><BR>14.4:&nbsp;&nbsp;&nbsp;&nbsp;My floating-point calculations are acting strangely and giving<BR>&nbsp;&nbsp;&nbsp;&nbsp;me different answers on different machines.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;First, see question 14.2 above.&nbsp;&nbsp;If the problem isn't that<BR>&nbsp;&nbsp;&nbsp;&nbsp;simple, see the full list for a brief explanation, or any good<BR>&nbsp;&nbsp;&nbsp;&nbsp;programming book for a better one.<BR><BR>14.5:&nbsp;&nbsp;&nbsp;&nbsp;What's a good way to check for "close enough" floating-point<BR>&nbsp;&nbsp;&nbsp;&nbsp;equality?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;The best way is to use an accuracy threshold which is relative<BR>&nbsp;&nbsp;&nbsp;&nbsp;to the magnitude of the numbers being compared.<BR><BR>14.6:&nbsp;&nbsp;&nbsp;&nbsp;How do I round numbers?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;For positive numbers, try (int)(x + 0.5) .<BR><BR>14.7:&nbsp;&nbsp;&nbsp;&nbsp;Where is C's exponentiation operator?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Try using the pow() function.<BR><BR>14.8:&nbsp;&nbsp;&nbsp;&nbsp;The predefined constant M_PI seems to be missing from &lt;math.h&gt;.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;That constant is not standard.<BR><BR>14.9:&nbsp;&nbsp;&nbsp;&nbsp;How do I test for IEEE NaN and other special values?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;There is not yet a portable way, but see the full list for<BR>&nbsp;&nbsp;&nbsp;&nbsp;ideas.<BR><BR>14.11:&nbsp;&nbsp;&nbsp;&nbsp;What's a good way to implement complex numbers in C?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;It is straightforward to define a simple structure and some<BR>&nbsp;&nbsp;&nbsp;&nbsp;arithmetic functions to manipulate them.<BR><BR>14.12:&nbsp;&nbsp;&nbsp;&nbsp;I'm looking for some mathematical library code.<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;See Ajay Shah's index of free numerical software at<BR>&nbsp;&nbsp;&nbsp;&nbsp;ftp://ftp.math.psu.edu/pub/FAQ/numcomp-free-c .<BR><BR>14.13:&nbsp;&nbsp;&nbsp;&nbsp;I'm having trouble with a Turbo C program which crashes and says<BR>&nbsp;&nbsp;&nbsp;&nbsp;something like "floating point formats not linked."<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;You may have to insert a dummy call to a floating-point library<BR>&nbsp;&nbsp;&nbsp;&nbsp;function to force loading of floating-point support.<BR><BR><BR>Section 15. Variable-Length Argument Lists<BR><BR>15.1:&nbsp;&nbsp;&nbsp;&nbsp;I heard that you have to #include &lt;stdio.h&gt; before calling<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf().&nbsp;&nbsp;Why?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;So that a proper prototype for printf() will be in scope.<BR><BR>15.2:&nbsp;&nbsp;&nbsp;&nbsp;How can %f be used for both float and double arguments in<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf()?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;In variable-length argument lists, types char and short int are<BR>&nbsp;&nbsp;&nbsp;&nbsp;promoted to int, and float is promoted to double.<BR><BR>15.3:&nbsp;&nbsp;&nbsp;&nbsp;Why don't function prototypes guard against mismatches in<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf's arguments?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Function prototypes do not provide any information about the<BR>&nbsp;&nbsp;&nbsp;&nbsp;number and types of variable arguments.<BR><BR>15.4:&nbsp;&nbsp;&nbsp;&nbsp;How can I write a function that takes a variable number of<BR>&nbsp;&nbsp;&nbsp;&nbsp;arguments?<BR><BR>A:&nbsp;&nbsp;&nbsp;&nbsp;Use the &lt;stdarg.h&gt; header.<BR><BR>15.5:&nbsp;&nbsp;&nbsp;&nbsp;How can I write a function that takes a format string and a<BR>&nbsp;&nbsp;&nbsp;&nbsp;variable number of arguments, like printf(), and passes them to<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf() to do most of

⌨️ 快捷键说明

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