C Programming Language Specification (c)

7. constants, variables and structures

Rule 7-1 intuitive meaning to make use of those figures to represent constant string or multiple times in the program.

Rule 7-2 requires publicly constants in header files, no publicly defined constants in header files.

Rule 7-3 is not necessary to remove the public variables.
Description: common variable is one of the reasons of increasing the coupling between modules, it should reduce unnecessary common variable to reduce the coupling between modules.

Rule 7-4 Do not use uninitialized variables as the right value.
Description: In particular assignment without reference pointer in C, often cause system crashes.

7-5 regular feature structure to be single , is directed to an abstract matters.
Description: The design of the structure should strive to make the structure represents a realistic transaction abstraction, not simultaneously represent a variety. Each element structure should represent different aspects of the same transaction, and should not be described in different transactions no relationship or weak elements into the same structure.

7-6 design rules do not cover everything, a very flexible data structure.
Example: The following structure is not clear and reasonable.

typedef struct STUDENT_STRU
{
unsigned char name[8]; /* student's name */
unsigned char age; /* student's age */
unsigned char sex; /* student's sex, as follows */
/* 0 - FEMALE; 1 - MALE */
unsigned charteacher_name[8]; /* the student teacher's name */
unisgned charteacher_sex; /* his teacher sex */
} STUDENT;

If read as follows, it may be more reasonable.

typedef struct TEACHER_STRU
{
unsigned char name[8]; /* teacher name */
unisgned char sex; /* teacher sex, as follows */
/* 0 - FEMALE; 1 - MALE */
} TEACHER;

typedef struct STUDENT_STRU
{
unsigned char name[8]; /* student's name */
unsigned char age; /* student's age */
unsigned char sex; /* student's sex, as follows */
/* 0 - FEMALE; 1 - MALE */
unsigned int teacher_ind; /* his teacher index */
} STUDENT;

7-7 the number of elements in the structure of the rule should be moderate. If the number of structural elements according to certain principles can be considered much the different sub-structures of elements to reduce the number of elements in the original structure.

Rule 7-8 of the build system default data type conversion, but also have a full understanding.

Between 7-9 rules signed and unsigned, integer and floating-point type between, from wide to narrow type type, function parameters, the function returns the expression, there is no implicit conversion complex expressions.

7-10 integer value of a complex expression rules only to narrower cast type and basic type of the expression have the same sign.
If the cast to use on any complex expression, type conversion may be applied should be strictly limited. For example as follows:

(uint32_t) (u16a + u16b) 		/* not compliant */
(uint32_t) u16a + u16b 		/* compliant */
(uint32_t) u16a + (uint32_t) u16b /* compliant */

When the rule statement 7-11 when used in a distributed environment or data structures and different inter-CPU communication environment, the machine must consider the byte order bit field and byte alignment problems such use.

8. Macro Designer

It works only rule is extended with 8-1 C braces initialization constant expression enclosed in parentheses, a type qualifier, the storage class identifier or do-while-zero structure.

These macros are used among all permissible form. Storage class identifier includes type qualifiers, and keywords such as extern, static and const. Any other form of #define could lead to unexpected behavior, or is very difficult to understand the code.

In particular, the macro can not be used to define a part of the statement or statements, in addition to do-while structure. Macros can not be redefined grammar of the language. All brackets replace the list of macros, regardless of what form of (), {}, [] should be in pairs. do-while-zero structure (see example below) in the macro statement is the only acceptable form body having a complete statement. do-while-zero sequence and structure for enclosing statement to ensure that it is correct. Note: You must omit the semicolon at the end of the macro statement body.
E.g:

/* The following are compliant */
#define PI 3.14159F /* Constant */
#define XSTAL 10000000 /* Constant */
#define CLOCK (XSTAL / 16) /* Constant expression */
#define PLUS2(X) ( (X) + 2 ) /* Macro expanding to expression */
#define STOR extern /* storage class specifier */
#define INIT(value) { (value), 0, 0 } /* braced initialiser */
#define READ_TIME_32 () \
do { \
DISABLE_INTERRUPTS (); \
time_now = (uint32_t) TIMER_HI << 16; \
time_now = time_now | (uint32_t) TIMER_LO; \
ENABLE_INTERRUPTS (); \
} while (0)	 /* example of do-while-zero */

Rule 8-2 calls the function macros can not do without parameters.
This is a constraint error, but the preprocessor know and ignore this problem. Composition macro function for each parameter must have at least a pre-labeled, or its behavior is undefined.

8-3 rule defining a function macro, each instance of the parameter should be enclosed in parentheses.
Function-like macro definitions, the parameter should be enclosed in parentheses. Abs example, a function can be defined as:
#define abs (X) (((X)> = 0) (X): -? (X))
can not be defined as:
#define abs (X) (((X)> = ? 0) x: -x)

Published 56 original articles · won praise 75 · Views 300,000 +

Guess you like

Origin blog.csdn.net/qq_44710568/article/details/105265769