faq/phonotadousmo.ml> is the right way to link to these Steve Summit, C Programming FAQs: Frequently Asked Questions, Addison-. A major revision and expansion of the phonotadousmo.mlc FAQ list was published in late by Author: Steve Summit Title: C Programming FAQs: Frequently Asked. the book "The C Programming Language", by Kernighan and Ritchie, and it is very Another recommended source is "C Programming FAQs", by Steve Summit.
|Language:||English, German, Portuguese|
|ePub File Size:||17.68 MB|
|PDF File Size:||12.46 MB|
|Distribution:||Free* [*Register to download]|
Notes to Accompany The C Programming Language, by Kernighan and Ritchie (``K&R''). Steve Summit phonotadousmo.mlc Frequently Asked Questions. This page lists my favorite links having to do with C programming. Follow this link to download the C99 standard (INCITS/ISO/IEC ) in PDF form; phonotadousmo.mlc FAQ — Steve Summit's FAQ list for the phonotadousmo.mlc newsgroup is a. 9MKCODDDVG5N» Doc» C Programming FAQs: Frequently asked Questions. Find eBook Authored by Steve Summit. Released at -. Filesize: MB.
The extended versions of this FAQ list see question References: PCS Sec. Tools and Resources Can you see anything wrong with it?
C Programming FAQs: Frequently Asked Questions
It is intended to complement the use of the published text and is protected by international copyright laws. The content is made available here and may be accessed freely for personal use but may not be published or retransmitted without written permission. Miscellaneous Is there a printf format for binary? How much do function calls slow things down?
Are they legal inside quoted strings? How can I get a copy of previous winning entries? Box and Mervin E. Ian F. Samuel P. Harbison and Guy L. Steele, Jr. Kernighan and P. Brian W. Kernighan and Dennis M. Knuth, The Art of Computer Programming. Park and Keith W. William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Dale Schumacher, Ed. There is another bibliography in the revised Indian Hill style guide see question Copies of the original X3. Note that ANSI derives revenues to support its operations from the sale of printed standards, so electronic copies are not available.
It has been suggested that the price differential between this work and the official standard reflects the value of the annotations: they are plagued by numerous errors and omissions, and a few pages of the Standard itself are missing. Many people on the net recommend ignoring the annotations entirely. The text of the Rationale not the full Standard can be obtained by anonymous ftp from ftp. Rodrigo Vanegas.
Ozan S. Howard West. Mark Warren. Tom Verhoeff. Larry Virden. Ed Vielmetti. Freek Wiedijk. Karl Heuer. Larry Weiss. Chris Volpe.
Kurt Watzka. Martin Weitzel. Thanks to Jukka Korpela and Jutta Degener for providing interim versions of this list in web form while I was dragging my feet.
Alan Watson. Questions 1. Questions 2. Why doesn't " int http: Questions 5. Questions 6. Questions 7. Questions Questions undefined behavior? Variable-Length Argument Lists http: If you might need large values above 32, or below , , use long. Otherwise, if space is very important i.
Otherwise, use int. If well-defined overflow characteristics are important and negative values are not, or if you want to steer clear of signextension problems when manipulating bits or bytes, use one of the corresponding unsigned types. Beware when mixing signed and unsigned values in expressions, though. Using unsigned char can help; see question None of the above rules apply if the address of a variable is taken and must have a particular type.
If for some reason you need to declare something with an exact size usually the only good reason for doing so is when attempting to conform to some externally-imposed storage layout, but see question B11 p. For one thing, the variable to hold getchar's return value must be an int. By passing getchar's return value through a char, either a normal character might be misinterpreted as EOF, or the EOF might be altered particularly if type char is unsigned and so never seen.
Some vendors of C products for bit machines support bit long ints. Programmers interested in writing portable code should therefore insulate their 64bit type needs behind appropriate typedefs. The definition is the declaration that actually allocates space, and provides an initialization value, if any. The best arrangement is to place each definition in some relevant.
This rule promotes a high degree of portability: A few very odd systems may require an explicit initializer to distinguish a definition from an external declaration. It is possible to use preprocessor tricks to arrange that a line like DEFINE int, i ; need only be entered once in one header file, and turned into a definition or a declaration depending on the setting of some macro, but it's not clear if this is worth the trouble. It's especially important to put global declarations in header files if you want the compiler to catch inconsistent declarations for you.
In particular, never place a prototype for an external function in a. ISO Sec. As a general rule, you should put these things in header. In particular, never put external function prototypes in.
On the other hand, when a definition or declaration should remain private to one source file, it's fine to leave it there. See also questions 1. Not really. First of all. See questions 1. If you do want to use function prototypes instead of lint for cross-file consistency checking. Question 1. It can be used as a stylistic hint to indicate that the function's definition is probably in another source file.
Can't a structure in C contain a pointer to itself? Structures in C can certainly contain pointers to themselves. See also question 2. To fix this code. A similar problem. The main difference is that the second declaration is of a slightly more abstract type--its users don't necessarily know that it is a structure. The first form declares a structure tag..
Question 2. Use the cdecl program. The pointer-to-function declarations in the examples above have not included parameter type information. The first part of this question can be answered in at least three ways: Modern versions of cdecl can help here.
Versions of cdecl are in volume 14 of comp. When the parameters have complicated types. Build the declaration up incrementally.
Either have the function return a generic function pointer. But I can't find a way to declare the functions. I'm building a state machine with one function for each state. You can't quite do it directly. Functions which are called without a declaration in scope perhaps because the first call precedes the function's definition are assumed to be declared as returning int and without any argument type information. Another possible source of this problem is that the function has the same name as another one declared in some header file.
Non-int functions must be declared before they are called. This problem can be fixed either by using new-style syntax consistently in the definition: In this case.
For old-style function definitions. It is usually safe to mix the two styles see question It might do so if calls using variable-length argument lists were less efficient than those using fixed-length. A compiler may use a different calling sequence for functions which accept variablelength argument lists. So that a proper prototype for printf will be in scope. Variables with static duration that is.
Memory obtained with calloc is all-bits Dynamically-allocated memory obtained with malloc and realloc is also likely to contain garbage. Variables with automatic duration i. Nothing useful can be predicted about the garbage.
Is it safe to take advantage of calloc's zero-filling? Does free work on memory allocated with calloc, or do you need a cfree? As a workaround, you can make the array global or static if you won't need a fresh copy during any subsequent calls , or replace it with a pointer if the array won't be written to.
If neither of these conditions hold, you'll have to initialize the array by hand with strcpy when f is called. A string literal can be used in two slightly different ways. As an array initializer as in the declaration of char a , it specifies the initial values of the characters in that array.
Anywhere else, it turns into an unnamed, static array of characters, which may be stored in read-only memory, which is why you can't safely modify it.
In an expression context, the array is converted at once to a pointer, as usual see section 6 , so the second declaration initializes p to point to the unnamed array's first element. For compiling old code, some compilers have a switch controlling whether strings are writable or not. Perhaps it is a pre-ANSI compiler, unable to accept function prototypes and the like. Question 6. The type pointer-to-type-T is not the same as array-of-type-T.
Use extern char a. Not at all. What you heard has to do with formal parameters to functions; see question 6. Arrays are not pointers. As usual, a picture is worth a thousand words. In other words, a is three places past the start of the object named a, while p is three places past the object pointed to by p.
In the example above, both a and p happen to be the character 'l', but the compiler gets there differently. Arrays automatically allocate space, but can't be relocated or resized. Pointers must be explicitly assigned to point to allocated space perhaps using malloc , but can be reassigned i. Due to the so-called equivalence of arrays and pointers see question 6. See questions 6. Be careful with sizeof, though.
An explicit declaration for the function is normally needed, since implicit external function declaration does not happen in this case because the function name in the initialization is not part of a function call. See also question 4.
50+ Best Free C Programming Tutorials, PDF & eBooks
Question 4. What's the story? The usage has always been unambiguous; there is nothing you ever could have done with a function pointer followed by an argument list except call the function pointed to. Typedef names are not automatically generated for structure tags. One good way is for clients to use structure pointers perhaps additionally hidden behind typedefs which point to structure types which are not publicly defined.
Another possibility is to declare the variable-size element very large. A thorough treatment of the arguments surrounding the legality of the technique is beyond the scope of this list. Rationale Sec. This technique is popular. It does seem to be portable to all known implementations Compilers which check array bounds carefully might issue warnings. Although a few early C compilers lacked these operations.
Footnote 1 However.
There is no single. A field-by-field comparison might require unacceptable amounts of repetitive code for large structures. If you need to compare two structures. Can I turn off the padding. Your compiler may provide an extension to give you this control perhaps a pragma.
When structures are passed as arguments to functions. Programmers often choose to use pointers to structures instead. Structures are often returned from functions in a location pointed to by an extra.
Some compilers merely pass a pointer to the structure. Some older compilers used a special. C has no way of generating anonymous structure values. You will have to use a temporary structure variable or a little structure-building function.
It is straightforward to define a simple structure and some arithmetic functions to manipulate them. How can I pass a constant by reference? You can't do this directly. You will have to declare a temporary variable. A more portable solution. It is relatively straightforward to write a structure out using fwrite: What's important is that fwrite receive a byte pointer. Note also that if the structure contains any pointers. When you're reading a binary data file.
I'm occasionally seeing 0x0a and 0x0d values getting garbled. The pragma directive provides a single. Even when the structure is not part of an array. See question 2. Structures may have this padding as well as internal padding. ANSI C defines the offsetof macro. If you don't have it.
Build a table of names and offsets. Since structurevalued functions are usually implemented by adding a hidden return pointer see question 2. The connection is hard to see because of the intervening comment. Perhaps there's a missing semicolon at the end of the last declaration in the last header file you're including. What could be causing this? Look for a misdeclared main see questions 2. See also questions 7. There is no standard way of initializing any other member nor.
If such intermixing were disallowed without explicit casts. A compiler may also generate nonfatal warnings when enumerations and integers are indiscriminately mixed. Although many people might have wished otherwise. Some advantages of enumerations are that the numeric values are automatically assigned. At the present time.
A disadvantage is that the programmer has little control over those nonfatal warnings. If all you're worried about is debugging. You can write a little function to map an enumeration constant to a string. Question 3. What's the difference? Unspecified means that an implementation should choose some behavior, but need not document it. Undefined means that absolutely anything might happen.
In no case does the Standard impose requirements; in the first two cases it occasionally suggests and may require a choice from among a small set of likely behaviors. Note that since the Standard imposes no requirements on the behavior of a compiler faced with an instance of undefined behavior, the compiler can do absolutely anything. In particular, there is no guarantee that the rest of the program will perform normally.
It's perilous to think that you can tolerate undefined behavior in a program; see question 3. If you're interested in writing portable code, you can ignore the distinctions, as you'll want to avoid code that depends on any of the three behaviors.
See also questions 3. Regardless of the order of evaluation, shouldn't it print 56? It is not guaranteed that an increment or decrement is performed immediately after giving up the previous value and before any other part of the expression is evaluated. In the example, the compiler chose to multiply the previous value by itself and to perform both increments afterwards.
The behavior of code which contains multiple, ambiguous side effects has always been undefined. This is a rough definition; see question 3. Furthermore, the prior value shall be accessed only to determine the value to be stored. The second sentence can be difficult to understand. It says that if an object is written to within a full expression, any and all accesses to it within the same expression must be for the purposes of computing the value to be written.
This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification. Some gave i the value 3, some gave 4, but one gave 7. I know the behavior is undefined, but how could it give 7?
Undefined behavior means anything can happen. See questions 3. Once an expression or program becomes undefined, all aspects of it become undefined. Since the two forms differ only in the value yielded, they are entirely equivalent when only their side effect is needed. Even if I don't, doesn't precedence dictate it?
Not in general. Operator precedence and explicit parentheses impose only a partial ordering on the evaluation of an expression. When you need to ensure the order of subexpression evaluation, you may need to use explicit temporary variables and separate statements.
Any book on C should make this clear. There is a special exception for those operators as well as the?: It's unwise to depend on it. A compiler may do anything it likes when faced with undefined behavior and. A similar problem can arise when two integers are divided. Under C's integral promotion rules. Use an explicit cast to force long arithmetic: Can I use code like this? In other words. If you really want to.
The pointer you declared is p. What's wrong with this code?
To make a pointer point somewhere. It would also be possible to use malloc to allocate the answer buffer. The simplest way to correct the question-asking program is to use a local array.
Since local variables are not initialized. This example also uses fgets instead of gets. The pointer variable answer. That is.
Unfortunately for this example. See question Strings are arrays. Question 8. Use strcpy instead: To increment the value pointed to by p. Say what you mean: It is an anomaly in pcc-derived compilers. Whenever possible. See also questions 4.
Are you sure the function initialized what you thought it did? Remember that arguments in C are passed by value. The called function altered only the passed copy of the pointer. You'll either want to pass the address of the pointer the function will end up accepting a pointer-to-a-pointer.
There is no generic pointer-to-pointer type in C. Not portably. You can simulate pass by reference yourself. Strictly speaking. C always uses pass by value. If the conversion bothers you. See also question 6. Since arrays decay immediately into pointers. Allowing pointer parameters to be declared as arrays is a simply a way of making it look as though the array was being passed--a programmer may wish to emphasize that a parameter is traditionally treated as if it were an array.
As a convenience. It's supposed to be a convenience. As mentioned above. Although programmers need not know the internal values. Question 5. The language definition states that for each pointer type.
A null pointer is known not to point to any object or function. According to the language definition. To generate a null pointer in a function call context. Note that many Unix manuals get this example wrong. Function prototypes cannot provide the types for variable arguments in variable-length argument lists however. When function prototypes are in scope. For example.
It is safest to properly cast all null pointer constants in function calls: Unadorned 0 okay: The same as on any other machine: See also questions 5. Whenever a programmer requests a null pointer. Besides potentially helping incorrect programs to work but only on machines with homogeneous pointers. The suggested definition would make uncast NULL arguments to functions expecting pointers to characters work correctly.
The problem is that there are machines which use different internal representations for pointers to different types of data. The internal representation of a null pointer does not matter. See also question 9.
What if the internal representation for null pointers is nonzero? When C requires the Boolean value of an expression in the if. The boolean negation operator. There is no trickery involved here. I thought that ANSI function prototypes were supposed to guard against argument type mismatches like this. When a function accepts a variable number of arguments. Blindly avoiding certain constructs or following rules without understanding them can lead to just as many problems as the rules were supposed to avert.
In the case of the goto statement. It is true sic that any nonzero value is considered true in C. On the other hand. In general. When a Boolean value is generated by a built-in operator. See also question 5. NULL are used for code readability. Question 9. Boolean values and definitions can evidently be confusing. NULL should only be used for pointers. A programmer who wishes to make explicit the distinction between 0 the integer and 0 the null pointer constant can then use NULL whenever a null pointer is required.
Using NULL is a stylistic convention only. As a matter of style. In particular. The table under question 5. Any usage of NULL as opposed to 0 should be considered a gentle reminder that a pointer is involved. See also questions 9. There is no one right answer. Many programmers believe that NULL should be used in all pointer contexts. Others feel that the confusion surrounding NULL and 0 is only compounded by hiding 0 behind a macro.
NULL should not be used when another kind of 0 is required. C programmers must understand that NULL and 0 are interchangeable in pointer contexts. Once again. NULL is used only as a stylistic convention. Although symbolic constants are often used in place of numbers because the numbers might change.
Using NULL may be preferable.
C Programming FAQs: Frequently Asked Questions
See questions 5. The old HP series uses a different addressing scheme for byte addresses than for word addresses. Later models used segment 0.
Some Honeywell-Bull mainframes use the bit pattern for internal null pointers. The Prime 50 series used segment It was common on old CDC ones-complement machines to use an all-one-bits word as a special flag for all kinds of data.
Most users in ring 11 have null pointers of 0xB The Symbolics Lisp Machine. It does not even save keystrokes. It is not needed in assignments and comparisons. This trick. Its use may suggest to the reader that the program's author is shaky on the subject of null pointers. An enumeration may be preferable if your debugger shows the names of enumeration constants when examining variables.
Using an int may be faster. Why isn't it a standard type? Should I use defines or enums for the true and false values?
C does not provide a standard Boolean type. Smaller types may make the generated code bigger or slower. See also section The actual values should be of concern only to compiler writers. The NULL macro. The internal or run-time representation of a null pointer.
The conceptual null pointer. Authors of C programs never see them. It is often hidden behind.
The null pointer constant. It is implemented with. One good way to wade out of the confusion is to imagine that C used a keyword perhaps nil. The fact that null pointers are represented both in source code. The compiler could either turn nil into the correct type of null pointer when it could determine the type from the source code. Why do these questions come up so often? C programmers traditionally like to know more than they need to about the underlying machine implementation.
Anup rated it it was amazing Feb 11, Ayush Bhat rated it it was amazing Apr 28, Kandarp Jani rated it it was amazing Jul 22, Raghu rated it it was amazing Apr 02, Fred Milano rated it really liked it Aug 05, Cal Desmond-Pearson rated it really liked it Jun 28, AK added it Apr 21, Hemant marked it as to-read Oct 11, Bahar is currently reading it May 27, Manuel Menezes de Sequeira added it Jul 05, Igor Furnas marked it as to-read Dec 11, Rishad marked it as to-read Apr 15, Darin added it Jul 01, Christopher added it Jul 25, Ben added it Jul 30, Vishal Darbar marked it as to-read Aug 18, Michael added it Feb 27, James added it Apr 06, Kartik Singhal marked it as to-read Jun 25, Pavithra V marked it as to-read Jul 20, Danica Joy marked it as to-read Aug 01, Enchained marked it as to-read Aug 31, KSL Akilan added it Sep 18, Hold Sky marked it as to-read Nov 11, Prabha Kcp marked it as to-read Jan 12, Rajkamal Vashistha marked it as to-read Jan 29, Ravee marked it as to-read Feb 10, Gaurav Rudra added it Feb 11, Krzysztof Tychulski marked it as to-read Feb 12, Thomas Johnson marked it as to-read Mar 01, Durre Shahwar added it Mar 22, Vighnesh Mj marked it as to-read Apr 07, Santosh marked it as to-read Apr 09, Microlan marked it as to-read Apr 16, Ram marked it as to-read Apr 20, Follow these two simple rules: ISBN is made available here by permission of the author and the publisher as a service to the community.
If you don't have ftp access, a mailserver at rtfm. David Spuler. Ozan S. Einar Indridason. Steele, now in its fourth edition. C99 supports complex as a standard type.