This is the traditional translation of "C frequently asked Questions" found from Taiwan http://www.cis.nctu.edu.tw/chinese/doc/research/, found "C Frequently Asked Questions" Ye Bingzhe, is also a translator of "C Programming Language" 3 / e, this article is very good, paying it for learning purposes, I didn't get the author's authorization, the copyright of the original article still belongs to the original author .c FAQ ====================================================== Comp.lang.c Frequently Asked Questions List (with Answers , Fortunately). copyright (c) 1991-96 Marshall P. Cline, ph.d.posting 3 of 4.posting # 1 Explains Copying Permissions, (NO) WARRANTY, TABLE-OF-Contents, etc ====== ======================= ■ □ Section 14: Program style guidance ================== ============ Q81: Is there any good C program writing standard? Thank you for reading this document, not the invenability of yourself. But please don't ask this question in Comp.lang.c . Almost all software engineers, more or less regard this thing as "big toys". Moreover, some things that want to be C programs have written standards, which are made by those who are not familiar with this language and methodology, so it can only be the "past form" standard. This phenomenon of "playing position" makes everyone untrustworthy for program writing standards. Obviously, people who ask this question in Comp.lang.c , I want to make myself more refined, will not trip because of my ignorance, but some answers are only worse.
================================================ q82: Is the program writing criteria necessary? Is it enough? The program writing standard will not make people who don't understand OO; only training and experience are likely. If it is useful, it is to suppress those trivial critics - when the large agency wants to integrate the zero-scattered programming organization, these fragments often appear. But in fact, you are not just this standard. The architecture they provide allows newcomers to worry about some freedom, but systematic methods are better than these good-looking standards. Organizational institutions need a consistent design and implementation of "philosophy", such as strong size or weakness? Is it a reference interface with a pointer? STREAM I / O is still stdio? C program should not call C? Inverted? How to use ABC? Inheriting the skills of doing this or specificity? Which test strategy should this? Go check one by one? This should not provide consistent "GET" and "SET" interfaces for each information. The interface should be designed by the internal contact? Error condition This uses TRY / CATCH / THROW or the return value? ……and many more. We need a detailed "design" partial "semi-standard". I recommend a three-stage standard: training, consultant consultant, and link library. Training is a "intensive teaching", consultants let OO concepts are deeply apparent, rather than just taking, high-quality link libraries provide "long-distance teaching". All three training have very popular marketing conditions. ([Translation] is undoubtedly, this is the beauty, plus area.) Accepted the above-mentioned training: "Buy ready-made, don't worry yourself (Buy, don't build.). Link library, buy training courses, buy development tools, buy consultants. It is difficult to find successful tool manufacturers and application / system manufacturers who want to achieve success by self-study. [Translation] This paragraph is very reference value. However, some background information has to be provided to all reference. Don't forget: The author is American, is the background of this place, and pay attention to what he serves ..... Ugh! Are there so many professional consultants in China? : - From C to C is not only a grammatical language of " ", an organization wants to achieve the realm of OOP, but the "OO" spirit is not put in OOP, but only self-extaked; accounting balance sheet Will show their stupidity. C program written standard should be adjusted by C experts, please ask questions in comp.lang.c (but don't use "Coding Standard" words; as long as you ask: "What is the advantage of this skill?") . Find a master that can help you avoid traps, on a training course, buy a link library, see if the "good" library is in writing your program. Never lift your own standard unless you have some degree of masterpiece. There is no standard, which is a bad standard, because inappropriate "official statement" will make it difficult for civilians that are not smart enough. Now C training courses and link libraries have been very prosperous. Again one thing: When a thing is hot, the shook and swindlers are also born; be sure to think twice. Also ask someone who has repaired the class from somewhere, because the older is not necessarily a good faculty. Finally, choose the practitioners who know how to guide others, rather than a full-time teacher with only time knowledge of this language / methodology. [Translation] Good fortune! ================================================================= I should be in the middle of the function or start Come declared regional variables? It is used in the vicinity of the place where it is used for the first time. The object will be initialized (constructed) when the object is announced. If there is not enough information in the place where the object is initialized until the middle of the function, you can start a "null value" at the beginning, wait for the "setting" value; you can also in the middle of the function The initial correct thing gave it. In terms of execution efficiency, let it have the right value at the beginning, will build it first, engage it, then rebuild it. From a simple example like "String", there will be 350% speed gap. It may be different on your system; of course, the entire system may not be reduced to 300 %, but "must" will have unnecessary performance recession. Common refutes is: "We will provide" SET "operation behavior for each information of the object, and the additional cost of constructing will be scattered." This is worse than the effectiveness, because you add a maintenance dream. Provide "SET" operational behavior for each information is equal to the information that does not have a defense: you have revealed internal practice skills. You hide only the entity "name" of the member object, but you use List, String, and Float, you have exposed. Usually maintenance is more resource consumed than the CPU execution time. Regional variables should be declared near it for the first time. Sorry, this is different from the habit of C, but "new" is not necessarily "not good". ================================================= q85: Which original gear naming custom Great? "foo.c"? "foo.cc"? "foo.cpp"? If you have a practice, use it. If not, look at your compiler, which one is it used. Typical answers are: ". C", ".cc", ".cpp", or ".cxx" (natural, ". C" extension is assumed that the archive system will distinguish ".c". " c "case). In Paradigm Shift, we used ".c" in Makefiles, even under unclear-sensitive file system (in a distinctive system, we use a compiler option: "Suppose .C files are C Program ", such as: IBM CSET with" -tdp ", Zortech C with" -cpp ", Borland C " -p ", etc.). ================================================= q86: Which header gear naming practice the best? "foo.h"? "foo.hh"? "foo.hpp"? If you have a practice, use it. If not, and your editor does not have to distinguish between C and C files, as long as you use ".h", you will use the editor, like ".h", "hh" or ".hpp" . In Paradigm Shift, we use ".h" as a source file for C and C (then we will not build those pure C header files). ========================================================================================================================================================================================================== q87: C does not have a guide like Lint? in principle? YES, there are some common examples of danger. However, they are not "bad" because in some cases, they have to be used. * The "Fred" setting The operating operation should be transmitted back "* this" as "Fred &" (to allow a set command to be made). * There is a category of the virtual function, there is a virtual deconstruction. * If a category is {deconstructed, set the operand, copy construct sub-}, usually all of them are all needed. * "Fred" Category Copy Construction Subscriber, Both the settlement operate, add their parameters: "Fred: Fred (const fred &) and" fred & fred :: operator = (const) Fred &). * Sub-objects of category must use the initialization Lists, do not use the setting mode, because of the user's custom category, there will be a lot of efficiency gap (3x!). * Many setup operations should be tested first: "We" is "them"; such as: fred & fred :: operator = (const fred & fred) {if (this == & fred) Return * this; // .. .normal assignment duties ... return * this;} Sometimes it is not necessary to test, but generally, these situations are: no need to provide the user with a displayed settle operand (provided relative to the compiler Set the operating operation). * In those categories that simultaneously define " =", " " and "=", "A = B" and "A = A B" should usually do the same thing; other similar build-up operands Also (such as: A = 1 and A; P [I] and * (P I); etc.). This can be mandated using the type of binary operation operand "OP ="; for example: Fred Operator (const fred & a, const fred & b) {fred ANS = a; ANS = B; returnon;} , "Constructive" binary operations can even be a partner. However, the usual operand is sometimes more efficient (for example, if the "fred" category is a "string", and " =" must be reconfigured / copy the string memory, you will know Its last length may be better). ============================================== ■ □ 15th Section: SmallTalk program learns C key ========================================= ===== Q88: Why is C FAQ to discuss SMALLTALK? Is this used to attack SmallTalk? Two OOPLs in the world are C and SmallTalk. Since this popular OOPL has a second largest number of users, many new C programs are jumping by the SmallTalk background. This section answers the following questions: * The difference in these two languages? * Jump from SmallTalk to the programmer of C , know what, can you master C ? This section *! * Won't *! * Answer these questions: * Which language is "best"? * Why is SMALLTALK "very bad"? * Why is C "very bad"? This is not provocative to SmallTalk's horror, let them poke my tire when I sleep (I am very difficult to have time to rest this time. =========================================================================================================================================================================== ======================== q89: Where is C and SmallTalk? The most important difference is: * Static model or dynamic number? * Inheritance Can only be used to generate a sub-model? * Value VS Reference Semantics? The first two differences will be explained in this section, and the third point is the discussion topic of the next section. If you are SmallTalk program, now I want to learn C , and the three FAQs are best to study carefully. ========================================================== 别 」? How much similar / unlike smalltalk? Static Typing is said: The compiler will "static" (in compile time) to verify the type security of each operation, not the program code that will be checked when executed. For example, under the static type, it will detect the type of type signature alignment of the function from the variable, and incorrect pairing will be picked out by the compiler, not when executed. In the OO program, the most common "type" error is: to activate a member function for an object, but the object is not ready to process the operational action. For example, if the "FRED" category has a member function "F () but no" g () "," fred "is a" Fred "case," Fred.f () "is legal," FRED. " g () "is illegal. C (static) in the compile period capture type, SmallTalk (dynamically) is captured during the execution period. (Technically, C is very like pASCAL - "half" static type - because the index transformation and UNION can be used to destroy the type system; this reminds us: You use the pointer to the frequency of Union and Union can only be like you Same "goto".) ===================================================================================================================================================================================================================================================== Which is suitable for C ? Which is more suitable for C ? If you want to use C , please use it as a static language. C is extremely elastic, you can "long" like SmallTalk by pointer transformation, union or #define. But don't do this. This reminds us: less #define. Some occasions, pointer transformation and union are necessary, even very good practices, but must be cautious. The pointer transition is equal to the compiler to fully trust you. The wrong pointer transformation may destroy the accumulation, in other object memories, the operation behavior of the call does not exist, and cause a general failure. This is a very bad thing. If you avoid using these related things, your C program will be safer and faster, because you can detect things in the compile period, you don't have to leave it again. Even if you like a dynamic type, please avoid using it in C , or consider changing another language that delays the type checks to the execution period. C puts 100% of the type test period; she does not have any built-in mechanism for performing the periodic test. If you use C as a dynamic other OOPL, your fate will be held. ========================================================= how to distinguish a C object link library Do you belong to a dynamic type? Tip # 1: When all things are derived from a single root category, it is often called "Object". Tip # 2: When the container category Container Classes, like List, Stack, SET, etc., is not a template version. Tip # 3: When the container category (List, Stack, SET, etc.) put the insert / removed elements, it is treated as a pointer to "Object". (You can put Apple into the container, but when you take it out, the compiler only knows that it is derived from Object, so you have to transform it back to Apple *; you'd better pray that it is really apple, otherwise You will suffocate the brain.) You can use "Dynamic_cast" (joined in 1994) to transform the pointer "safe", but this dynamic test is still "dynamic". This program style is a basic element of C dynamic type, you can call the function: "Convert this object to Apple, or give me null, if it is not apple, you get a dynamic type: until During the period, I realized what happened. If you use template to make a container category, the C compiler will static detection of 99% of type information ("99%" is not true; some people claim 100%, and those need sustainability (Persistence " The person who can only get a static type of static type below 100%). The focus is: C is genericity through Template, rather than inheritance. ================================================ q93: How to use inheritance in C ? Is it different from SmallTalk? Some people think that inheritance is used to reuse the program code. In C , this is wrong. Description White Point, "Inheriting is not" designed to "reuse program code." [Translation] This part is quite important. Otherwise, C users will infect "inheritance fever".