The essence of Unix (Linux) C Programming Problems-general Linux technology-Linux programming and kernel information. The following is a detailed description. Some people think that the C language is outdated. for this question, the benevolent sees Shi, and the wise sees wisdom. indeed, C ++ has many more powerful advantages than C. however, C ++ is built on C. this is also written by Herbert Schildt < > Why is the best selling in the world? What's more, to learn more about LINUX, you must have a considerable C skills. (This is also the basis for collecting and sorting this article :-)
Now, in combination with my personal experience in programming, it makes great effort for beginners to avoid detours, so whether you are using C or C ++ programming, whether you are a beginner in programming or a mature professional, you will find that this article will benefit you. of course, I try my best to make it clear and easy to understand.
========================================================
| Copyright Disclaimer: You can freely copy and distribute this document. |
| If you want to modify this document or make better suggestions, please |
| Notify the author of the article first. Instead of only on the announcement board |
| Post. Because the author may miss it. In any case, |
| This statement must be retained. |
|
|
| Pjbright@telekbird.com.cn |
Document directory Chapter 1: Preface
Chapter 2: Conventions
Chapter 3: Start a task
Chapter 4: Using lint
Chapter 5: Use make
Chapter 6: Quality error-free programming
Chapter 7: debugging technology
Chapter 8: Other better documents
Chapter 1: Preface
You can get the text of this post on forum.linuxaid.com.cn, And the HTML version is in the rush ......
If you see this article one month later, this article may have been updated :-)
Chapter 2: Conventions
Professional source programming style.
Let's take a look at the source Writing Style of world-class C masters. For example, Steve Maguire has many good suggestions.
[] Advocates the use of easy-to-understand "Hungary" naming conventions.
All character variables start with ch, for example, char ch _****;
All byte variables are given B; for example, byte B _****;
All long-character variables are labeled with l; for example, long l _****;
All pointer variables are given P; for example, char * p_ch _****;
We recommend that you add a "tag" starting with an uppercase letter after the basic name derived from the type. For example:
Analyze char ** ppchMydata;
It can be seen at a glance that it represents a pointer to the character pointer Mydata.
The biggest disadvantage of "Hungary" Naming is that it is hard to read:-(. But what does this mean compared to the C source program that is not the President's speech? Think about the following data naming:
Char a, B, c;
Long d, e, f;
.
.
.
(I won't go on again anyway ...)
[] Advocating standard writing.
It doesn't matter if you don't worry about the writing format. run the cb command to format your source program. although the format of the source program does not affect the correctness of your compilation results, remember that it can be easily read by other programmers. otherwise, no one will take care of you.
For more information about cb commands, use man cb to refer to its manual page.
Of course, apart from cb, there are more and better. But cb is what you can find on any UNIX (LINUX). What's more, it is not bad.
Chapter 3: Start a task
Take a deep breath before starting the task!
[] Are you ready for other documents?
Do you have nothing but a C source? Not yet moved, grain and grass first. you must first understand the functions to be completed by the program. before writing a program, the program's functions should be standardized. one way to write the specifications and known program functions is to first compile the corresponding operation manual. if you do this by yourself, advise you to write a demand book first. remember, this is a great thing for you to get twice the result with half the effort.
One example: I plan to patch this bank's credit sub-function module. I spent 10 weeks writing Planning books, requirement books, operation procedures, instructions, and other documents. the program will be written in two weeks and submitted to each credit department for testing and use after a preliminary test (one week. then, modify the corresponding document based on the feedback, and modify the source program according to the document. the official version will be released six months later.
[] Should I follow the ANSI standard?
If you only use the ANSI standard initial document, congratulations, your program has extensive support and compatibility worldwide. bright and infinite. however, you must make a trade-off between general purpose and private purpose. Sorry, I cannot help you.
My principle is: the core uses ANSI, and the interface is taken as needed. In this way, you only need to edit the user interface when converting the platform. It is practical.
Appendix: ANSI Standard C header file
Is it cool?
[] Why?
After getting a new task and before starting the new task, you should immediately think back to what you once had. The old re-engineering is far more efficient and environmentally friendly than the new ones.
[] Should I have my own database?
My answer is that I should have my own special library and be compatible with ANSI. unlike 3.8, you only need to attach your own dedicated library after the source program. second, the source code will be very refined after you have your own library. don't envy others.
[] Learn Conditional compilation. Pay attention to your platform features ?)
Unless you are sure that the program you want to write is customized on a specific OS-specific hardware platform. otherwise, pay attention to the length of the data type, and the precision is different. sometimes the differences between different compilers must be considered.
....
... (You are welcome to enrich the white space here)
....
Well, what details are there in the task?
[] Am I ge langtai?
Don't be so stingy. Add detailed comments to the source program so that you and others can read the program even after many years.
Separate functions with comments.
[] Be careful when deleting unnecessary code.
A good suggestion is: Use # ifdef DEL, rather than simply comment out or even roughly direct dd. if you are using /*... */, but once the code to be deleted has many lines or annotations in the comments, this may not be so easy.
[] How to name a source program file?
The performance is not the same as that of any original application. A simple method is to give a try. What kind of reaction does the system have?
[] Modify only one place at a time.
[] Write only one function at a time.
[] Compile common programs.
The program can be optimized only after the program is compiled and the required performance requirements are met.
[] Do not use a. out as the result. You can use the same executable file name as the source program.
[] Must I use VI for editing?
There are many specialized programming editors in LINUX. They can make you have higher efficiency and lower-level input errors, but I still want to advise you at least to master VI. After all, VI is everywhere.
[] Collaborative operations. Please believe that you are not operating alone. Therefore, it is necessary for you to master some other tools. For example:
....
... (You are welcome to enrich the white space here)
....
Chapter 4: Using lint
Lint is not as bad as you think. On the contrary, once the source program forms a form without LINT errors, it will be easy to maintain and enjoy the benefits brought by this.
[] LINT should be used before cc (gcc.
Lint is a syntax check program. You should have enough patience for this talkative mother-in-law. Although you know what you are doing, it is always a good habit to use LINT before CC.
[] What are the characteristics of lint?
The important reason for using lint before compilation is that LINT can not only discover syntax errors in ansi c, but also identify potential problems or code problems that are difficult to transplant to another machine. in addition to pointing out simple syntax errors, LINUT can also point out other errors for the following reasons:
A. statements that cannot be reached.
B. No cycle is entered.
C. unused variables.
D. function parameters are never used.
E. The parameter is automatically used before the value is assigned.
F. The function returns values in some places, but does not return values elsewhere.
G. function calls make the number of parameters different in different places.
H. The structure pointer is incorrectly used.
I. Fuzzy use of operator priority.
Haha, it's useful!
[] How to Control LINT output?
Sometimes LINT will have a large screen of warning information, but does not seem to point out the error. In order to identify potential errors, you need to spend time browsing these large amounts of warning information.
However, if your program separates several independent modules, do not use the option when starting LINT at the beginning. some code-independent warnings can be ignored when these modules are modified or expanded. the following options are available for this purpose:
-H does not provide a heuristic test for determining whether there are errors and whether the types are correct.
-V regardless of the parameter not defined in the function
-U no matter whether the variables and functions used are not defined or defined, but not used.
[] Simply insert commands in the program to affect LINT running. It looks like a comment.
/* NOTREACHED */The unattainable code is not described.
/* VARARGSn */The number of variables of the function is not checked normally. Only the Data Types of the n parameters are checked.
/* NOSTRUCT */does not perform strict type check on the next expression.
/* ARGUSED */In the next function, no warning message is given for unused parameters.
/* LINTLIBRARY */is placed at the beginning of the file. It does not provide warning information for unused functions.
For more use of LINT, use man lint.
Chapter 5: Use make
[] What is make?
Unix (Linux) is a natural development platform, and I am very happy with it. make is a powerful tool. it automatically tracks mutually dependent source code blocks and forms a program, making it easy to build an executable program. make is the specification between the dependency part and the code.
[] All programs must use make?
Yes. Although you only have a few simple modules, you need a structure to support it from simplicity to complexity, unless your program has been finalized.
[] What makes Makefile?
Makefile consists of the following parts:
Note.
^
Using the # symbol insert. make will ignore any content after # And Its RETURN key.
Variable.
^
Make allows you to define famous variables similar to SHELL variables. For example, if you have defined SOURCES = prog. c, the value of this variable $ (SCOURES) contains the source file name.
Dependency.
^
The left side is the target module followed by a colon, followed by the module that is dependent on the module.
Command.
^
Start with the TAB key (that is, using the same number of spaces cannot replace it ).
[] Makefile example
The following is a simple example to describe how to use make. assume that your program has two source files main. c. c. A header file named myhead under the subdirectory include. h. A library is generated by three source files: myrout1.c, myrout2.c, and myrout3.c.
The makefile file is:
# A Basic MAKEFILE file.
# This includes the personal header file and the personal library.
HEADERS = include/myhead. h
SOURCES = main. c acc. c
PRODUCT = $ (HOME)/bin/tool
LIB = myrout.
LIBSOURES = myrout1.c myrout2.c myrout3.c
CC = cc
CFLAGS =-g
All: $ (PRODUCT)
$ (PRODUCT): $ (SOURCES)
$ (CC) $ (CFLAGS)-o $ (PRODUCT) $ (SOURCES)
Lint: $ (PRODUCT)
Lint $ (SOURCES) $ (LIBSOURCES)
Haha, It's like SHELL programming. if you use gcc in LINUX like me, you only need to change the above CC = cc to CC = gcc. how about it? Do you want a more complex one?
[] A more complex Makefile
Have you noticed that in the above example, as long as make is started, all source code will be re-compiled.
If you can understand the makefile below, congratulations.
# A more complex makefile
HEADERS = include/myhead. h
SOURES = main. c acc. c
OBJECTS = main. c acc. c
PRODUCT = $ (HOME)/bin/tool
LIB = myrout.
LIBSOURCES = myrout1.c myrout2.c myrout3.c
LIBOBJECTS = $ (LIB) (myrout1.o) $ (LIB) (myrout2.o) $ (LIB) (myrout3.o)
INCLUDE = include
CC = cc
CFLAGS =-g-Xc
LINT = lint
LINTFLAGS =-Xc
All: $ (PRODUCT)
$ (PRODUCT): $ (OBJECTS) $ (LIB)
$ (CC) (CFLAGS)-o $ (PRODUCT) $ (OBJECTS) $ (LIB)
. C. o: $ (HEADERS)
$ (CC) $ (CFLAGS)-c I $ (INCLUDE) $ <
$ (LIB): $ (HEADERS) $ (LIBSOURCES)
$ (CC) $ (CFLAGS)-c $ (? :. O =. c)
Ar rv $ (LIB) $?
Rm $?
. C. c :;
Lint: $ (PRODUCT)
$ (LINT) $ (LINIFLAGS) $ (SOURCES) $ LIBSOURCES)
Chapter 6: Quality error-free programming
Dear, check whether you have noticed the following details? That is to say, are you a qualified programmer who can write high-quality error-free code? Always remember that writing error-free code is the responsibility of the programmer, not the tester (Excerpted from my "detail page", so this section will never be complete. thank you for filling her out)
[] At least one error for all programmers:
If (a = 3) {...} if a is equal to 3, then ......
You should at least develop the habit of writing constants before you determine whether a variable is equal to a constant. in this way, even if you accidentally write it like this: if (3 = ){......} it can be easily discovered before cc.
[] Re-emphasize: Priority of logical operators.
I don't want to talk about it. In short, if you have to write the following code:
If (a & 0x1 & B & 0x2 ){......}
You 'd better have a detailed guide at hand, or you are an expert in this field.
[] Try not to use the int data type.
This is just a piece of advice. You can use char, short, and long data types. After a few years, when you become a master, you will find that I am working hard at this time.
[] Non-integer functions must be fully defined.
For example, long float jisuan (char chArr [], int chNum)
{Long float lMydata;
...
...
Return (lMydata );}
[] Be careful when entering non-integer functions.
Such as long float lfNum;
...
...
Scanf ("% lf", & lfNum );
[] The valid number of float type is 7 bits. When there are more than 7 bits, the 8th bits and later bits are inaccurate and can be defined as long float.
[] Use fread fwrite function whenever possible for file input. Use fprintf fscanf function only if it is used for another purpose.
[] When comparing arrays and strings, confirm to end.
Chapter 7: debugging technology
Debugging technology is not easy to say in this article. The reason why I want to separate it is to apply the old saying M $: "It will be better in the next version ":-((. in fact, such articles are everywhere on the National BBS.
Here I just want to talk about one of the responsibilities of programmers: using assertions in programs.
~~~~
[] Both the delivery version of the program and the debugging version of the program can be maintained.
[] Use assertions to confirm function parameters.
[] Delete non-defined features from the program, or use assertions in the program to check for illegal use of non-defined features.
[] Do not waste others' time and describe unclear assertions in detail.
[] Eliminate the implicit assumptions, or use assertions to check their correctness.
[] Use assertions to check for situations that are impossible.
Example: I use assertions in all my source programs. in my compilation of the national electronic exchange fuzzy search function module test, the front-end staff panted to tell me that there was an unfamiliar English on the screen. I would like to explain which number is at the beginning, and then read the asserted document based on this number. It turns out that the previous day has not ended normally. After checking, there are only half a database. this situation rarely happens, but it is not impossible. assertions can be used to promptly and correctly identify whether a program is incorrect or an external factor. therefore, using assertions is an extremely important method to eliminate errors before they occur. this is also an aspect of determining whether a programmer has a good quality.
Chapter 8: Other better documents
< > Kenth H. Rosen... etc.
< > UNIX Software Operation.
< > Herbert Schildt.
< > Steve Maguire.
< > Daniel Barlow.
< > Tan haoqiang.
NOTE: My < > We believe that we can meet readers in 8 weeks. it discusses in detail the usage and precautions of low-level library curses, windows, panel library, menu library, form library, and TAM conversion library, and provides available instances. because the full text is huge and complex, the writing speed is slower. (slow work ?) Sorry. in addition, due to my experience, all things I have discussed may be outdated. If you are also developing applications for industries such as finance, post and telecommunications, and securities that only use character terminals, you are right. in contrast, if you are a newbie, please drop these tough things. haha ....
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.