Everything is neither created out of thin air nor optimized in place in one step!
C language is no exception. The following principles apply to some C language applications:
1. The smaller the space usage, the better.
The parameter passing method of C language functions is to copy a parameterProgramSubject usage;
Therefore, it is better to pass the struct pointer to the transmitted struct, including the design of the Union, which is also out of the minimum space occupied;
Memory alignment and bit domain.
For example, let's look at the bit domain:
Strcut char {<br/> unsigned CH: 7; <br/> unsigned Font: 6; <br/> unsigned size: 19; <br/>}< br/> struct char character; </P> <p> // fully utilizes the memory space!
Let's look at the Union:
Union {<br/> int I; <br/> float F; <br/> char * s; <br/>} ui; </P> <p>/* <br/> in combination, the memory occupied is the one with the largest length in the element. <br/> therefore, the above sizeof (UI) = sizeof (float); <br/> However, in this case, space is wasted. <br/> the optimization method is that all elements are defined as pointers, the sizeof (UI) value is the smallest and convenient to call! <Br/> */
2. Simple and Easy to remember
Let's take a look at the changes in struct name definitions:
/* Original definition */<br/> struct st {<br/> int A; <br/> char SP [10]; <br/>}// st is a structural variable </P> <p> struct st ST1, st2; // define two struct </P> <p>/* Add typedef */<br/> typedef struct {<br/> int; <br/> char SP [10]; <br/>} st; </P> <p> st str1, st2; // defines two struct types, obviously, we can see the difference from the above! </P> <p>/* nested definition of struct. Some data structures are commonly used */<br/> typedef struct _ st {<br/> int; <br/> char SP [10]; <br/> struct _ st * st2; // defines a pointer! <Br/>} st;
All in all, it is to constantly simplify the complexity! This example also includes array a [I], function FP!
According to the analysis, the C language development process is simplified, efficient, and in line with people's reading habits!
3. Check the character I/O processing family.
Int fgetc (File * stream );
Int GETC (File * stream );
Int getchar (void );
Why are the return values of the functions that process characters int rather than Char?
The reason is that it is necessary to report whether the end of the file (EOF );
If the returned value is char type, one of the 256 characters must be specified as EOF. If this character appears inside the file, when reading the text,
An error occurs. Because I do not know whether to parse it into an EOF or a common character, I define it as an int type and find a value that will never appear in 256 characters,
Mark of the EOF at the end of the file! The value is usually-1.