Unix (Linux) C Programming Problem

zhaozj2021-02-08  254

================================================ Copyright statement: You can freely copy and distribute this Documentation || If you want to modify this document or make better advice, please let the article of the article first. Not only in the bulletin board || posted. Because the author may leak. In any case, all || must keep this statement. || || || pjbright@telekbird.com.cn || || 04-11-2000 v0.1 | ======================== =============== 文章 directory Chapter 1: Foreword Chapter 2: Application Chapter 3: Start Task Chapter 4: Using the Lint Chapter 5: Using Make Chapter 6: Premium Unligerative Chapter VII: Commissioning Technology Chapter 8: Other Better Document Chapter 1: Foreword For the C language, some people think it has been outdated. For this issue, the benevolent seeks, the wise, the C ratio C has a more powerful advantage. But C is based on C. This is also the "Herbert Schildt"

> In the world's best-selling reasons. Not more, to learn that Linux must have a fairly C found. (This is also the root of this article :-)

Now combined with the experience in programming, in order to make the novice to bend, add flowers on the old brocade, so no matter what you use C or C programming, whether you are a beginner or mature professionals, this article will find that this article Will benefit you. Of course, I don't know how to understand, and I will not be old.

I love C. (as the world loves God, :-) ..

You can get the text of this post on Forum.LinuxAid.com.cn. And his HTML version is driving ...

If you see this article after a month, then this article may have been updated :-)

Chapter II: Convention

Professional source writing style.

Let's take a look at the source of the world class C. If Steve Maguire has a lot of good suggestions.

[] Advocates a naming agreement with an easy-to-understand "Hungarian".

All character variables are started at CH; such as: char ch_ ****;

All byte variables are crowned with B; such as: byte b _ ****;

All long-character variables are crowned with L; such as: long L _ ****;

All pointer variables are crowned with P; such as: char * p_ch _ ****;

It is recommended that the basic name of the type is added to the "label" starting with uppercase letters. Such as:

Analysis Char ** PPCHMYDATA;

It can see that it represents a pointer to character pointer MYDATA.

The biggest lack of "Hungarian" name is difficult: - ((. But relative to the C source of the president's speech, this is what this is what it is to see the following data name:

CHAR A, B, C;

Long D, E, F;

.

.

.

(I will not look down again. ")

[] Advocate the standards.

If you think that the format is written, you don't want to write a format. Before you hand, use the CB command to format your source. Although the format of the source does not affect the correct results of your compilation results Sex, but remember, allowing other programmers to read it easily. Otherwise, no one will care about you. About the more usage of the CB command, you can use the Man CB to refer to its man page.

Of course, in addition to CB, there are more better. But CB is found on any UNIX (Linux). Not to mention it is not bad.

Chapter III: Start the task

Before you start the task, you should first take a deep breath!

[] Are you ready for other documents?

Do you have anything else outside the C source? The soldiers have not moved, the grain is in advance. You must first know the function you have to complete. Before starting the program, the function of the program should have a specification. Write specification And a way to confirm the program function is to write the corresponding manual. If you are a single one, persuade you to write a demand book. Remember to remember, this means that you mean a half-mean mean.

An example: I plan to make a patch for the credit sub-function module of the Bank. I used the 10-week time to write planning books, demand books, operational processes, instructions and so on. After the 2 weeks of time, After preliminary test (1 week), submit it to each credit department test. Then change the corresponding document according to the feedback information, and modify the source of the source according to the document. After 6 months, the official version was released.

Do you have to follow the ANSI standard?

If you only use ANSI's standard header file, congratulations, your program has a wide range of support and compatibility around the world. Bright is unlimited. But you must make a handle between general and dedicated, sorry, I can't help you .

My principle is: The core is taken with ANSI, the interface is taken on demand. This is only necessary to separate the user interface during the conversion platform. Practical first.

Attachment: ANSI standard C header file

Is it very cold?

[]Again later?

Before getting new tasks and, you should immediately recall what you have to have. The old adjustment is more efficient and environmentally friendly than another stove.

[] Is there any own library?

My answer should have their own special libraries and are compatible with ANSI. Different from 3.8, you only need to attach your own dedicated library after the source program. Secondly after you have your own library, the source code will Very refined. Don't envy others.

[] To learn conditions compile. Pay attention to your platform features. (Master's sign?)

Unless you are sure that the program you want is tailored to a specific OS-specific hardware platform. Otherwise, you should pay attention to the length of the data type, the accuracy is different, don't want to be of course, sometimes even different compilers Consider consideration.

....

.... (Welcome to fulfill this blank)

....

Ok, what details are there in the mission?

[] Is I Zengtai?

Don't be so embarrassed. Add a detailed annotation in the source program to make yourself and others can read what procedures it is in many years.

Separate individual functions with a note line.

[] Be careful when deleting unwanted code.

A good suggestion is: use #ifdef del, not simply comment, or even rough DD. If you use / * ... * /, but once the code to be deleted has a lot of lines, or in the comment This may not be so good when comment.

[] How to name the source file file?

The performance features are not the same as any original application name. A simple method is to try, what is the system to react?

[] Only modify one place once.

[] A single function is written only once.

Write a general program.

This program is optimized after the program is written, and after completing the required performance requirements.

[] Do not use A.out as the result. You can use the same executable file name as the source.

Do you have to edit it with VI?

There are many special programming editors under Linux. They can make you have higher efficiency and lower low-level input errors, but I still have to persuade you to master Vi at least. After all, Vi blooms everywhere.

[] Collaborative homework. Please believe that you are not fighting in the monarch. Therefore, you need to master some other tools. Such as

....

.... (Welcome to fulfill this blank)

....

Chapter 4: Using LINT

Lint doesn't think that you think. On the contrary, once the source creation forms a form in which there is no Lint error, it will be easily kept and enjoy this benefits.

[] Lint should be used before CC (GCC).

LinT is a syntax checkbox. For this big mother-in-law, you should have enough patience. Although you know what you are doing, it is always a good habit before the CC.

[] What are the characteristics of lint?

Important reason for using Lint before compiling is that LINT can not only find syntax errors in ANSI C, but also points out potential issues or difficult to transplantation of code issues in another machine. In addition to the simple syntax error, Linut also An additional error can be indicated based on the following reasons: a. Unable to reach the statement.

B. No entering the loop.

C. There is no variable used.

D. Function parameters are not used.

E. Automatically use parameters before you assign values.

F. Functions have returned values ​​in some places, but do not return elsewhere.

G. Function calls to different places different numbers.

H. Error uses the structural pointer.

I. Fuzzy use operator priority.

Hehehe, it is useful!

[] How to control the output of the LINT?

Sometimes Lint will have a large-screen warning message. But it doesn't seem to point out errors. In order to find a potential error, you need to browse these large warning information.

But if your program freshes several independent modules, do not use options when you start LINT. When you change or expand these modules, you can ignore some warnings that are not related to the code. To this end, you can use the following selection items :

-h is wrong if it is wrong, and the type correctly does not give a heuristic test.

-V does not have a defined parameters in the function

-u No matter the variables and functions used, they are not defined or defined but are not used.

[] Simply, insert the instruction in the program to affect the Lint run. It looks like a comment.

/ * NotReached * / unreachable code does not give information instructions.

/ * VARARGSN * / Function variable number is not usually checked, only the data type of starting N parameters is checked.

/ * Nostruct * / For the next expression does not strictly check.

/ * Argused * / Next function, no warning messages that are not used by parameters are given.

/ * LinTlibrary * / placed on the beginning of the file, it will not give a warning message that is not used.

For more use of LINT, please use Man Lint to know.

Chapter 5: Using Make

What is Make?

Unix (Linux) is a natural development platform. I am very happy. Make is a powerful tool. It automatically tracks interdependent source blocks and forms a program so that it is easy to create an executable program. Make is This specification described between this dependency part and the code.

[] Do you have Make all programs?

Yes. Although you only have a few simple modules, you need to have a structure to support it from simple to complicate. Unless your program has already contained.

[] What is made from Makefile?

Makefile consists of several parts:

Comment.

^^^^

Use the # symbol to insert. Make will ignore any content after #, and the subsequent return keys.

variable.

^^^^

Make allows a name variable similar to the shell variable. For example, you define Sources = Prog.c, then the value of the variable is $ (Scoures) contains the source file name.

Dependency.

^^^^^^^^^

The left is the target module, then take a colon. The module with the relationship with the module is again connected.

command.

^^^^

Start with the Tab key (ie, the same number of spaces can not be replaced).

Makefile example

Here is a simple example to illustrate the use of Make. Suppose your program has two source files main.c and myc.c, a header file mEhead.h under the subdirectory include, a library MYROUT1 .c, myrout2.c, myrout3.c generation.

Its makefile file is:

# A basic makefile file.

# These include personal header files and personal libraries.

Headers = include / myhead.h

Sources = main.c myc.c

Product = $ (Home) / bin / Tool

LIB = myrout.a

LibsouRes = myrout1.c myrout2.c myrout3.c

CC = CC

Cflags = -g

All: $ (Product)

$ (Product): $ (Sourcees)

$ (CC) $ (CFLAGS) -O $ (Product) $ (Sources)

LINT: $ (Product)

LINT $ (SOURCES) $ (libsources)

Haha, quite like shell programming. If you use the GCC under Linux like me, then just change the above CC = CC to cc = GCC. How, do you want to come to a more complex point? [] One more Complicated Makefile

Whether you notice that in the previous example, you will recompile all source code as long as you start Make.

If you can understand the following Makefile, congratulations, you have cleared it.

# A more complex Makefile

Headers = include / myhead.h

SouRes = main.c myc.c

Objects = main.c myc.c

Product = $ (Home) / bin / Tool

LIB = myrout.a

Libsources = myrout1.c myrout2.c myrout3.c

LibObjects = $ (ilrout1.o) $ (iB) (myrout2.o) $ (iB) (MyRout3.o)

Include = incrude

CC = CC

Cflags = -g -xc

LINT = LINT

Lintflags = -XC

All: $ (Product)

$ (Product): $ (Objects) $ (LIB)

$ (CFLAGS) -O $ (Product) $ (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)

$ (LINTI) $ (SOURCES) $ libsources)

Chapter VI: Quality Worn Programming

Dear, check it, do you noticed the following details? That is, is you a qualified, can you write a programs for high quality error? To remember, write a wrong code is the responsibility of the programmer. Instead of testers. (Excerpt in my "Detail Page", this section will never stay complete, welcome to enrich her)

[] All programmers have appeared at least one error:

IF (a = 3) {...} If a is equal to 3, then ...

You should develop such habits: When it is judged whether a variable is equal to whether it is equal to whether it is equal. This is even if you accidentally write this: if (3 = a) {...} It can easily find it before CC.

[] Lost tunes: priority for logical operators.

I don't want to have a lot of mouth. In short, if you must write the following code:

IF (A & 0x1 && B & 0x2) {......

Your hand is best to have a detailed guide. Or you are an expert in this area.

[] Try not to use the int data type.

This is just an advice. You can use Char, Short, long data type. After a few years, when you grow into a master, you will find that my good intentions are at this time.

[] For non-integer functions, it must be completely defined.

Su LONG FLOAT JISUAN (Char Charr [], INT Chnum)

{Long float lmydata;

...

...

Return (LMYDATA);

[] Be careful to enter the input of non-integer functions.

Su LONG FLOAT LFNUM;

...

...

Scanf ("% lf", & lfnum);

[] Float type valid numbers are 7 bits. When more than 7 bits, the 8th and later bits will not be accurate, and they can be defined as long float.

[] The input of the file is used to use the FREAD FWRITE function. The fprintf fscanf function is used only when another use.

[] Confirm with the end of '/ 0' for the comparison operation of array and string.

Chapter 7: Debugging Technology

The debugging technology is not very good in this article, the reason why it is independent is the old saying: "It will be better in the next version": - ((. In fact, this article is on the national BBS Full day

转载请注明原文地址:https://www.9cbs.com/read-2994.html

New Post(0)