"C Program Design" Reading notes

Source: Internet
Author: User
Tags arithmetic array length bitwise bitwise operators data structures fread goto variable scope


"C Program Design" Reading notes

Keywords: C language
Original author name: Loose_went
The original source of the article: vczx.com---http://www.vczx.com/minute_c.php

Written in front:

"C Program Design" can be said to be a foundation of the programming book, but each read the feeling is different, it can be said that every time we read, there will be many new harvest. The so-called old book again read, memorable ah. This note is "C Programming" Tan Haoqiang, Tsinghua University Press published. In addition to the book to write down the key points of knowledge, but also added to my understanding of knowledge points, I think this is the importance of reading notes.

Chapter II Overview of data types, operators and expressions
Chapter Three the simplest C programming fourth chapter logic operation and judgment selection control
Fifth Chapter cycle Control Chapter Sixth array
Seventh Chapter function Eighth chapter pre-compiling processing
Nineth Chapter Pointers Tenth chapter structural body and the use body
11th Chapter Position Operation 12th Chapter document

Chapter I. Overview

1. The characteristics of C language

① language is concise, compact, easy to use and flexible. There are 32 keywords, 9 kinds of control statements.
The ② operator is rich in public 34 operators.
③ is rich in data structure, including: integral type, real type, character type, array, pointer, structure, common body and so on.
④ structured control statements (such as If...else, while, do...while, switch, for)
The ⑤ grammar limit is not too strict, the programming degree of freedom is big.
⑥ allows direct access to physical addresses, can perform bit operations, and can operate directly on hardware.
⑦ generates target code with high quality and high program execution efficiency.
⑧ portability is good.

2. The use of C language

C is not good at scientific computing and management, but C has obvious advantages for operating system and system utilities and for operating hardware. Now many large application software is also written in C.


Top of Page

Chapter II data types, operators and expressions

1. Data type of C

The data types of C include integer, character, solid, or floating-point (single and double), enumeration type, array type, struct type, common body type, pointer type, and NULL type.

2. Constants and variables

Constants whose values are immutable, the symbolic constant names are usually capitalized. Variable whose value can be changed, the variable name can only consist of letters, numbers, and underscores, and the first character must be a letter or an underscore. Otherwise, the name of the variable is not valid. A variable assigns the appropriate storage unit to it at compile time.

3. Integral type data

Representation of integer constants: Decimal Needless to say, octal begins with 0, such as 0123, and Hex begins with a 0x, such as 0x1e.
Integer variables are divided into: basic (int), short int, long int, and unsigned. Different types of data on various machines occupy different bytes of memory, the general int is 2 bytes, and the long type is 4 bytes.

4. Real-type data

Solid constant representation: The decimal form consists of a number and a decimal point (must have small points), such as: 0.12,. 123, 123, 0.0, etc. The exponent form is 123E3 representative of 123X10 's three-second party.
The real variables are divided into single precision (float) and double precision (double). Float is 4 bytes in general system, 7 digit valid digit, double type is 8 byte, 15~16 digit valid digit.

5. Character-type data

Character variables are enclosed in single quotes, such as ' A ', ' B ', and so on. There are also a number of special word constants, such as '/n ', '/t ' and so on. Represents the wrapping and transverse jumps respectively.
Character variables are defined as char, and a variable can only hold a single word constants amount.
A string constant is a sequence of characters enclosed in double quotes. Here it is important to note the difference between ' a ' and ' a ', which is a character constants, the latter is a string constant, and C stipulates that the end of each string is marked with a closing sign '/0 ', in effect "a" contains two characters: ' A ' and '/0 '.

6. The mixed operation between numerical data

Integer, character, real data can be mixed operations, the operation of different types of data to be converted to the same type of re operation, conversion rules:
Char,short-> int-> unsigned-> long-> double <-float

7. Operators and expressions

The C operators include:
Arithmetic operator (+-*/%)
Relational operators (> < = >= <=!=)
Logical operator (! && | | )
Bitwise operator (<< >> ~ | ^ &)
Assignment operator (=)
Conditional operator (? : )
Comma operator (,)
Pointer operator (* &)
Number of bytes (sizeof)
Coercion type conversion (type)
Component operators (. ->)
Subscript operator ([])
Other operators (such as function call operators ())
Self-increasing self-subtraction operator (+ +-) Note: The difference between ++i and i++, ++i use i before I plus 1,i++ use I, so I plus 1.
The solution of a comma expression: first solve the expression 1, then solve the expression 2, the value of the entire expression is the value of expression 2.


Top of Page

The simplest C programming in the third chapter

1. 9 Kinds of Control statements in C:

if () ~ else~
for () ~
while () ~
Do~while ()
Continue
Break
Switch
Goto
Return
Three basic structures of the program: sequential structure, selection structure, cyclic structure


2. Data output

C language does not provide input and output statements, input and output operations are performed by C library functions. But to include the header file stdio.h.
Putchar () output a character to the terminal
Format characters for printf ():
The ①d format character is used to output decimal integers
%d output of the actual length of the integer data
%MD the output length to M, if the data length is less than M, then the left complement space, if greater than M, then output the actual length
%ld Output Long Integer data
The ②o format character outputs integers in the form of eight
The ③x format character outputs integers in the form of 16
The ④u format character is used to output unsigned data, in decimal form
⑤c format characters are used to output a character
⑥s format character output a string
%s output actual length string
%ms output string occupies m column, if the string length is less than m, the left complement space, if greater than M, the actual output
%-ms output string occupies m column, if string length is less than m, right complement space,
%M.NS output takes m column, but takes only n characters from the left end of the string and aligns to the right
%-m.ns m, n meaning ditto, align to left, if n>m, then m automatically takes n value
⑦f format characters output real numbers in decimal form
%f integer part output, decimal part output 6 bits
%M.NF output data occupies m column, which has n decimal places. If the value length is less than M, the left fill space
%-m.nf ditto, right complement space
⑧e format characters output real numbers in exponential form
%e system specifies 6 decimal digits, 5-bit exponent (e+002)
⑨g format character output real number, according to numerical size, automatically select F format or e format


3. Data entry

GetChar () Enter a character from the terminal
SCANF (format control, address list) Standard C scanf does not use%u, for unsigned type data, in%d or%o or%x input. % of *, used to skip the corresponding data. Input data can not specify precision such as scanf ("%7.2f", &a); it is illegal.


Top of Page

Fourth chapter logic operation and judgment selection control

1. Relational operators:

C provides 6 relational operators (> < <= >= = =!=) Top four priorities above the latter two.

2. If statement

C provides three forms of if statements
An If (expression) statement
IF (expression) statement 1 Else Statement 2
If (expression 1) Statement 1
Else if (expression 2) Statement 2
...
Else Statement n

3. Conditional operator

(a>b)? A:B condition is true, expression takes value A, otherwise takes value B

4. Switch statement

Switch (expression)
{
case constant Expression 1: statement 1; Break
Case constant Expression 2: statement 2; Break
...
case constant Expression N: statement n; Break
Default: Statement n+1;
}

Top of Page

Fifth Chapter Circular Control

1. Several circular statements

Goto statement (now rarely used)
The While statement first judges the expression after the execution of the statement
The Do-while statement first executes the statement and then determines the expression
For statement

2. Break statements and Continue statements

The break statement is used to jump out of the loop and continue to end the loop.

Top of Page

Sixth Chapter Array

1. One-dimensional arrays

C stipulates that only static storage (static) and external storage (extern) arrays can be initialized. You can initialize an array without specifying the length of the array.

2. Two-dimensional array

3. Character array

Partial string handler function
Puts (character array) outputs a string to the terminal.
Gets (character array) enters a string from the terminal to the character array, and gets a function value, which is the first address of the character array
strcat (character array 1, character array 2) joins a string in a two-character array, and array 1 must be large enough.
Strcpy (character array 1, string 2) copies the string 2 to the character array 1.
STRCMP (String 1, String 2) compares strings, equals returns 0, string 1> string 2, returns a positive number, less than returns a negative number.
Strlen (character array) to find the length of the string.
STRLWR (string) converts uppercase letters in a string to lowercase
STRUPR (string) converts lowercase letters in a string to uppercase
These are some of the more common string processing functions.

Top of Page

Seventh Chapter function

1. A description of formal parameters and arguments

① The parameter does not occupy memory until the function is called
② arguments can be constants, variables, or expressions
③ must specify type of formal parameter
④ Real participating formal parameter types should be consistent
The ⑤ argument's data transfer to the formal parameter is "value pass", that is, a one-way pass

2. function return value

If you want the function to return a value, you get it in the function by using the returns statement, you specify the type of the function value when you define the function, and the integer is returned by default if you do not specify it.

3. Function call

1) Note that the number and type of arguments and parameters should correspond to each other when the function is called. The order in which the argument table is evaluated is indeterminate, some systems are left to right, and some systems are in the order of right to left. This point should be noted.
2 function call way: Function statement, function expression, function parameter
3 If the keynote function and the adjusted function are in the same file, and the keynote function is in the front, then the function should be described in the keynote function. Unless: (1) The return value type of the modulated function is an integral type or a character type (2) The called function appears before the keynote function.
4 the description and definition of the function is different, definition refers to the establishment of function functions, including the designation function name, function value type, formal parameter and its type, function body and so on. The description only describes the defined function return value type, which includes only the function name, function type, and an empty bracket, excluding formal parameters and function bodies.
5 C language allows recursive invocation of functions (the function itself is called directly or indirectly in the process of calling a function).

4. Arrays as function arguments

1 array elements are the same as function parameters and general variables
2 The parameters of the array masterpiece should be defined in the keynote and the modulated function respectively, the size of the parameter group can not be defined. Note: The array masterpiece argument is not a one-way pass.
3 multidimensional array as the parameter, in the function of the parameters of the definition can omit the first dimension of the size of the description, but cannot omit the description of the second dimension or higher dimensions.

5. Local variables and global variables

Variables can be divided into local variables and global variables from the perspective of variable scope.
1 internal variables (local variables)
A variable that is defined within a function and that is valid only within the scope of the function.
2 external variables (global variables)
Defined outside the function, it can be shared with other functions in this file, starting at the location where the variable is defined
To the end of this document. It is recommended that you use a global variable as little as possible, because it occupies
Resources, and the versatility of the function is reduced. If the function before defining the external variable wants to use the
External variables, you should use extern as an external variable description in the function.

6. Dynamic storage variables and static storage variables

The

is divided into static storage variables and dynamic storage variables, from the point of view of the time at which the value of the variable exists (the lifetime). Static storage refers to the allocation of fixed storage space to variables during program operation, and dynamic storage refers to the dynamic allocation of storage space to variables as needed during program operation. In the
C language, variables are stored in two broad categories: static storage classes and dynamic storage classes, including automatic (auto), Static, register (register), external (extern).
1) The way local variables are stored
The local variables in the function are not specifically described, all of the auto, that is, dynamic storage, auto can be omitted. A local variable can also be defined as static, at which point its value is invariant within the function. Static local variables, such as no initial value, the compile-time system automatically assigned to 0, dynamic local variables such as no initial value, it is an indeterminate value. C stipulates that an initial value can be assigned to an array only when global and local static variables are defined. To improve execution efficiency, C allows local variable values to be placed in registers, which are called register variables, and are described in register. But only local dynamic variables and formal parameters can be used as register variables, but not others.
2) How global variables are stored
Global variables are defined outside the function, and are allocated at compile time in a static store and can be referenced by functions in the program. How do you refer to a global variable for multiple files? If you define a global variable in a file, you must use extern to describe the global variable in this file, but if the global variable is defined with static, this global variable can only be referenced in this file, not by other files.
3) Storage category summary
from the scope point of view, local variables and global variables
Local variables: Automatic variables, that is, dynamic local variables (leaving the function, the value disappears)
static local variable (leave function, value remains)
Register variable (leave function, value disappears)
(parameters can be defined as automatic and register variables)
Global variables: Static global variables (reference only to this file)
Global variables (allow other file references)
from the existing time, there are static and dynamic storage
Dynamic storage: Automatic variables (valid in this function)
Register variable (valid within this function)
parameter
Static storage: Static local variable (valid within function)
Static global variable (valid in this file)
global variable (other file can reference)
location from variable value
Static store: Static local variables
Static global variables
global variable
Dynamic store: Automatic variables and Parameters
Registers: Register variables

7. internal functions and external functions

Internal functions: can only be called by other functions in this file, defined by the static before, internal functions also known as static functions.
External functions: can be called by other files, the definition of the front plus extern, if omitted, is implied as an external function, in the need to call this function in the file, generally use extern description.

Top of Page

Eighth chapter pre-compiling processing

The C compilation system is preprocessed before the program is normally compiled. C provides the following three kinds of preprocessing functions: 1 macro Definition 2) file contains 3 conditional compilation

1. Macro definition

Macro definition with no parameters
Represents a string with a specified identifier, in the form of a #define identifier string
A few notes:
1) Macro name generally used in uppercase
2 macro definition does not make grammar check, only when compiling the macro expansion of the source program will be the error
3 macro definition is not a C statement, not the end of the line plus a semicolon
4 Macro name valid range for definition to source file end
5 The scope of the macro definition can be terminated with the #undef command
6 when a macro is defined, you can refer to the defined macro name

Macro definition with Parameters
Define form: #define Macro name (parameter table) string
This is somewhat like a function, but they are different:
1 function call, the first realistic parameter expression value, and then into the formal parameters, and the macro is simply replaced, not evaluated
2 The function call allocates memory when the program is run, and the macro expands without allocating memory or the concept of a return value
3 defines the type for both the arguments and the parameters in the function, and requires consistency, the macro name is not typed, and the parameter has no type.
4 The function has only one return value, and the macro can get a few results
5 macro substitution does not account for run time, only for compile time, and function call takes up running time

2. file contains processing

#include "File 1" is to insert all of the contents of file 1 into the #include location and compile as a source file.
In the #include command, the file name can be used "" or "<", if now file1.c contains file2.h files, "" means that the system first in the FILE1.C directory to find File2.h, if not found, and then the system specified the standard way to retrieve the directory, < > Indicates that the system retrieves the directory directly in the specified standard way. So use "" to insure a little.

3. Conditional compilation

Conditional compilation means not compiling the entire program, but compiling the part that satisfies the condition. Conditional compilation has the following forms:
1) #ifdef identifier
Program Section 1
#else
Program Section 2
#endif
Its function: When the identifier is already defined in front (generally with #define), the program Segment 1 is compiled, otherwise the program segment 2 is compiled.
2) #ifndef identifier
Program Section 1
#else
Program Section 2
#endif
Its function is opposite to #ifdef, when the identifier has not been defined, to the program segment 1 compile, otherwise to the program segment 2 compile.
3) #if expression
Program Section 1
#else
Program Section 2
#endif
Its function: When the expression value is true (not 0), the program Segment 1 is compiled, otherwise the program segment 2 is compiled.


Top of Page

Nineth Chapter Pointers

The pointer is plainly the address. A pointer variable is a variable that holds a pointer (address).

1. A pointer to a variable and a pointer variable that points to a variable

Reading is very clumsy, and plainly is the address of the variable and the address variable used to hold the variable address. Because a variable in the compilation of the system to assign an address, if you use a variable to store this address, then this variable is called a pointer variable, which is used to hold the variable address of such a variable. The so-called "point" means to store XX's address, such as the pointer variable to the variable, "point" means to hold the address of the variable, such as pointer to the array of variables, "point" refers to the location of the address of the array. As long as you understand this, the pointer is not difficult. In addition, there are pointer variables that point to the string, pointer variables to the function, pointer variables to the pointer, and so on.

1) The definition of pointer variable
Form: type identifier * identifier such as: int *pointer;
Note two: * indicates that pointer is a pointer variable that cannot be written as *pointer when using this variable, *pointer is the variable pointer points to. A pointer variable can only point to a variable of the same type. As above
Pointer can only point to int-type variables.

2 references to pointer variables
Two related operators:
& Address operator &a represents the address of variable a
* Pointer operator *a represents the value of variable a

2. pointers to arrays and pointer variables to arrays

The starting address of the array's pointer index group, and the pointer Index group element's address of the array element.
1 the definition and assignment of a pointer variable that points to an array element
The definition is the same as the pointer variable definition of the variable, and C stipulates that the array name represents the first address of the array, that is, the initial array element address.
2 referencing an array element by pointer
We usually refer to an array element in the form of a[i], if the pointer can be referenced as such, * (A+i), or define a pointer variable p, assign the first address of the array A to p,p=a, and then use the * (p+i) reference.
Note: pointer variable p points to the first address of array A, then p++ points to the address of the next element of array A, that is, a[1].
3 function parameters of array masterpieces
A parameter group and a real parameter group are not values passed, but rather share the same address, so if the value of the parameter changes during the function call, the value of the argument changes as well.
4 pointing to a multidimensional array of pointers and pointer variables
The two-dimensional array is the majority. Suppose you define a two-dimensional array a[3][4], then
A represents the first address of the entire two-dimensional array, as well as the first address of line No. 0 and also the first address of the elements in column No. 0 of line No. 0. A +0 and a[0] represent the No. 0 header address, a+1 and A[1] representing the first address of the line.
Assuming that a is the first address of an array, if a is one-dimensional, a+i represents the address of the first element, and if a is two-dimensional, then a+i represents the first address of line I.
So how does the element address of the second column in the first row be represented? A[1]+2 or &a[1][2] or * (a+1) +2.
We just remember that in a two-dimensional array a represents the first address of the entire array, A[i] represents the first address of line I, A[i] and * (a+i) equivalent. As long as the use of proficiency is nothing complicated.
5 pointer variable pointing to a one-dimensional array of M integers
For example: Int (*p) [4],p is a one-dimensional array that contains 4 elements, if p points to a[0], then p+1 points to a[1], that is, the increment of P is in the length of the one-dimensional array, which is 4, for example:
Assuming that A[3][4]={1,3,5,7,9,11,13,15,17,19,21,23},p first points to a[0], which is the first address of array A, then p+1 is the first address of a[1], which is the address of element 9, as int (*p) [4] at the time of the definition p, Defines a one-dimensional array length of 4, so p+1 is equal to the length of a one-dimensional array of 4.

3. A pointer to a string and a pointer variable that points to a string

1) The representation of the string
There are two representations of strings in C: One is an array, the other is a character pointer
Char string[]= "I love c!";
Char *str= "I love c!";
In fact, the pointer form is also an array in memory, but the first address of the array is stored in the character pointer variable str, do not think STR is a string variable.
2) string pointer as function parameter
The string pointer is actually the first address of the array.
3 The difference between a character pointer variable and a character array
The ① character array consists of several elements, each containing one character, while the character pointer variable holds only the first address of the string, not the entire string
② array initialization is static and is not used for pointer variables.
③ assigns values to character arrays and can only assign values to individual elements, not as follows:
Char str[14];
Str= "I love c!";
Can be used on pointer variables,
Char *str;
Str= "I love c!";
Note: The first address of the string is not the character assigned to STR at this time.
The ④ array allocates memory cells at definition and compile time, and the pointer variable is best initialized after it is defined, or the value of the pointer variable points to an indeterminate memory segment, which destroys the program. Such as:
Char *a;
scanf ("%s", a); This method is very dangerous and should be like this:
Char *a, str[10];
a = str;
scanf ("%s", a), so that the character pointer points to a determined memory segment.
The value of the ⑤ pointer variable can be changed, and the first address of the string represented by the character array name cannot be changed.

4. Pointers to functions and pointer variables to functions

A function is assigned an entry address at compile time, and this entry address is called a pointer to a function. Function names represent the entry addresses of functions, which are the same as the array. We can use a pointer variable to hold the entry address, and then call the function through the pointer variable. For example, suppose there is a function that asks for a larger two: int max (int x, int y);
This can be done when we call this function:
int C;
C=max (A, b); This is usually called method, in fact we can define a function pointer, through the pointer to call, such as:
Int (*p) (); Note the definition of the function pointer variable
P=max; This sentence is to assign the entry address of the function to the function pointer variable p
C= (*p) (A, b);
Some friends may not understand (*p) (), in fact, it means to define a pointer to the function of the variable p,p not fixed to which function, but specifically to hold the function entry address variable. When you assign a function's entry address to it in your program, it points to which function. Note, however, that P cannot perform p++,p-operations like pointer variables.
Since p is a pointer variable, it can be passed as a parameter of a function. In fact, one of the most common uses of a function's pointer variable is to pass it to other functions as a function parameter. This is also a more in-depth part of the C language application.

5. function that returns a pointer value

We know that a function can bring back an integer value, character value, real value, and so on, the function can also bring back a pointer type of data, that is, address. This function is defined in the following form:
Type identifier * Function name (parameter table) such as: int *a (x,y) returns a pointer to an integral type
When using this function, be aware that you must first define an appropriate pointer to receive the return value of the function. This appropriate pointer whose type should be the type that the function return pointer points to.
Such a function is more difficult to understand, in fact, as long as the normal function to deal with it is easy. When we feel that the pointer is difficult to understand, we think of it as an integral type for the time being, it is much better to understand.

6. Array of pointers

An array of pointers is no doubt the array element is a pointer, defined in the form: type ID * array name [array length]
such as: int *p[4], do not write int (*p) [4], which is a pointer variable that points to a one-dimensional array. Pointer arrays are used to hold the first address of several strings, note that when defining an array of pointers, the following are initialized:
static char *name[]={"Li Jing", "Wang mi", "Xu Shang"};
Do not assume that the array is stored in a string, it holds the string first address, this must be noted.

7. Pointer to pointer

To be clear, a pointer is stored with a variable, and the variable is a pointer to the pointer. Definitions such as: char * *P;

8. Pointer array as an argument to the main () function

function in the form of
Main (int argc, char *argv[]) {}
The parameters of the main function are obtained from the command line, ARGC refers to the number of command-line arguments, note that the command name is also a parameter, command line arguments are strings, and their first address constitutes an array of pointers argv. The formal parameters of the main function argc and argv are just a habit and can be defined as other names.

9. Pointer summary

1 The data type of the pointer

Define meaning
Int I; Define an integer variable i
Int *p; P is a pointer variable that points to an integral type of data
Int A[n]; Defines an integer array of a, which has n elements
Int *p[n]; Defines the pointer array p, which has n a pointer element that points to an integral type
Int (*p) [n]; P is a pointer variable that points to a one-dimensional array containing n elements
Int f (); F is a function that returns an integer value
Int *p (); P is a function that returns a pointer to an integer data
Int (*p) (); P is a pointer to a function that returns an integer value
Int **p; Defines a pointer variable that points to a pointer

2 ANSI adds a void * Pointer type that defines a pointer variable, but does not point to any data type, and then casts the type when used. Such as:
Char *p1;
void *p2;
P1 = (char *) P2;
You can also define a function as a void * type, such as:
void *fun (CH1, CH2)
Represents a function fun returns an address that points to an empty type, and casts it if it is required to use this address. For example (assuming P1 is char):
p1= (char *) fun (C1,C2);

Pointers should be said to be the most important C language concept, but also the essence of C language, it has a lot of advantages, but the use of bad will also bring serious errors, which requires us to use more, practice, and slowly accumulate experience.


Top of Page

Tenth chapter structure and the use of body

1. Defined

General form of structure definition:
struct structure Body name {
Member List
};
The definition of a struct variable can be defined as: struct structural body name structure variable name;

2. Reference to a struct variable

The following rules should be noted when referencing structural body variables:
1 can not be the structure of the variable as a whole input and output, can only be in the variables of the individual member input output. The new standard C allows a struct variable to be assigned directly to another struct variable with the same structure.

3. Initialization of structural variables

Such as:
struct student
{long int num;
Char name[20];
char sex;
Char addr[20];
}a={89031, "Li Lin", ' M ', "123 Beijing Road"};

4. Structure Body Array

struct student stu[4];
Defines an array stu with elements of struct student type and an array of 4 elements. Note that the elements of the array are stored continuously in memory.
When you define an array of structures, the number of array elements can be unspecified. At compile time, the system determines the number of array elements based on the number of structural body constants given the initial value.

5. Pointer to struct variable

Because the structure variables are stored continuously in memory, so we can store the structure variables in the memory of the starting address in a variable, then this variable is to point to the structure of the variable pointer.
Note Assign the first address of a struct variable to the form of a pointer variable:
struct student stu_1;
struct student *p;
p=&stu_1; To add an address character and point to a function and a pointer to a string do not
There are three ways to refer to members of a struct-body variable:
Take the above structure as an example: set P to be a pointer to this struct variable, i.e. p=&a;
1) a.num
2) (*p). Num
3) P->num

6. Pointer to an array of structural bodies

struct student *p;
struct student stu[4];
P=stu;
P is a pointer variable that points to an array of struct bodies. Note here that the p++,p point to stu[0],p++ points to stu[1]. P points to the first address of an element in an array, not to the point where P points to a member of the element, as P=&stu[i].name is wrong.

7. Using pointers to structural bodies as function parameters

Although ANSI C allows the entire structure to be used as a function parameter, it is expensive to pass all member values one by one. Therefore, the use of pointers for parameters can improve operational efficiency.
Struct student Stu;
Called with the entire structure as a parameter:
Fun (Stu);
Moreover, the fun of the modulated function should be defined as structural variables, struct student stu;
Use pointers as arguments to call the form:
Fun (&stu);
The fun function is defined as pointer variable, struct student *p;

8. Using pointers to process linked lists

A linked list is an important data structure because it can be dynamically stored and distributed. The list has a head pointer that holds the first address of the entire list. The list is defined in the following form:
struct node{
int num;
...
struct node *next;
};
Next is used to store the address of the next node.
How to dynamically open and release storage units. C provides the following related functions:
1) malloc (size) in the memory dynamic storage area to open up a length of the continuous space of size. Returns the first address of the space successfully, the failure returns 0;
2) calloc (n,size) in the memory dynamic storage area to open up n length of size of contiguous space. Returns the first address of the space successfully, the failure returns 0;
3 free (PTR) releases the memory area pointed to by PTR. PTR is the value returned when a call to malloc and Calloc was last called.
In the above function, n and size are integral types, and PTR is a character pointer.


9. Common body

Definition form:
Union Shared body Name
{
Member List
} variable list;
The common body and the structure are similar, just a little different, the starting address of a member in the structure body is different, the length of the structure variable in memory is the sum of the length of each member, and the starting address of a member in the common body is the same, and the total memory length of the shared body variable is the length of the longest member.
Characteristics of the common body type data:
1 the same memory segment can be stored in several different types of members
2 The member that works in the common body variable is the last member
3 cannot assign a value to a shared-body variable name and cannot be initialized at the time of definition.
4 The common body variable can not be used as a function parameter
5 A common body type can appear in the definition of a struct, or alternatively, a shared array can be defined.
In addition, the name of the struct can be used as a parameter and the shared body name may not be available.
These two data structures are used in different situations.

10. Enum type

The form of the definition is as follows: Give an example
Enum Weekday{sun,mon,tue,wed,thu,fri,sat};
Enum weekday workday,week_end; Defining an enumeration variable
Workday and week_end are defined as enumerated types, and their values can only be one of sun to Sat.
You can also define an enumeration variable directly, which is the same as the struct
Enum Weekday{sun,mon,tue,wed,thu,fri,sat}wordday,week_end;
Note: Enumeration elements exist as constants, they are valued, C makes their values in order for 0,1,2 when compiling ...
For example: In the above definition, Sun's value is 0,mon 1
Also: Although an enumeration element has a value, you cannot assign an integer directly to an enumeration variable. Mandatory type conversions should be done, such as:
Workday= (enum weekday) 2; it is the equivalent of assigning Tue to workday.

11. Defining types with typedef

The role of a typedef is to allow you to define a data type name that you like to replace the existing data type name. Such as:
typedef int INT; Then I can use int to define integer variables. function is the same as int.
A typedef is used for structural body definitions, such as:
Typedef struct{
Int Day;
Int month;
Int year;
}date;
DATE birthday;
DATE *p; wait.
Using typedef is beneficial to the general and transplant of programs.


Top of Page

11th Chapter Position Operation

1) Overview

The so-called bit operation refers to the operation of bits. In the system software, often must deal with the bits problem.
The bitwise operators provided by C are:
& Bitwise AND
| by bit or
^ per-bitwise XOR OR
~ Take the Counter
<< Move Left
>> Move Right
& for a unit to clear 0, to take a number of points in the positioning and retention of the position is a great use.
| It is often used to place 1 of certain positions of a number.
^ Judge two bit values, different to 1, same as 0. Used to make a particular bit flip, and so on.
~ commonly used in conjunction with other bitwise operators, used to set the screen word.
<< put a number of bits all left, high left after the overflow, discard does not work. The left one is equal to the number multiplied by 2, and the left-shifted n bit is equal to 2n. The left shift is much faster than the multiplication operation.
>> when moving right, be aware of the symbolic problem. For unsigned numbers, move to the left high position when moving to the right to 0, and for the signed number, if the original symbol bit is 0 (positive), move to the left 0, or if the sign bit is 1 (negative), then the left to 0 or 1 depends on the system. The move into 0 is called "Logical right Shift", and the move into 1 is called "Arithmetic right Shift".

2) Bit segment

Divide a byte into segments to hold a few pieces of information. The so-called bit segment is a member of a struct type that defines the length in bits. Such as:
struct packed-data{
unsigned a:2;
unsigned b:6;
unsigned c:4;
unsigned d:4;
int I;
}data;
Among them, a,b,c,d accounted for 2, 6, 4, 4-bit respectively. I is an integral type, accounting for 4 bytes.
The reference to a bit member is as follows:
DATA.A = 2, and so on, but to be aware of the assignment, do not exceed the scope defined by the bit segment. If the bit member A is defined as 2 bits, the maximum value is 3, that is (11) 2, so data.a=5 will take 5 of the two low to assign the value, you will not get the desired value.
There are several important notes about the definition and reference of a bit segment:
① if a paragraph is to be stored from another word, you can define:
unsigned a:1;
unsigned b:2;
unsigned:0;
unsigned c:3; (Another unit)
Use a bit length of 0 to allow the next segment to be stored from the next storage unit.
② a bit segment must be stored in one storage unit, not two units across.
③ can define unknown bit segments. Such as:
unsigned a:1;
Unsigned:2; (This two-bit space is not necessary)
unsigned b:3;
The length of the ④ bit cannot be greater than the length of the storage cell, nor can the bit array be defined.

Top of Page

Chapter 12th Documents

1) Overview

C language is a sequence of characters, divided into ASCII files (text files) and binary files. That is, a C file is a byte stream or a binary flow.
ASCII file each byte put an ASCII code, representing a character, output and character one by one corresponding, easy to process characters, but occupy more space. The binary file is exported to disk as it is stored in memory, saving space, because the output is not corresponding to the character, can not directly output the character form, generally used to save the intermediate results. At present, C-file processing only a buffer file system method, that is, whether from the program to the disk file or from the disk file to the program, the data must first pass the buffer, to be filled after the buffer, to send centrally.

2) Folder Type pointer

In a buffered file system, the key concept is the file pointer. Because each file being used creates a buffer in memory to store the information about the file. This information is stored in a struct variable, which is defined by the system, named file, and defined in stdio.h.
FILE *FP;
A file pointer variable FP is defined, and subsequent operations on the file are performed through FP.

3 Open and close the file

Before you can read and write to a file, open the file first.
The function to open the file is: fopen (), which is invoked as follows:
FILE *FP;
Fp=fopen (filename, use file method);
fopen () fails to return a null pointer, success returns a file pointer to "filename", assigned to FP, so that the FP is associated with the open file. Or, FP points to "filename".
How to use the file: r,w,a,rb,wb,ab,r+,w+,a+,rb+,wb+,ab+, the specific meaning to remember.

4) The closure of the file

To prevent data loss, be sure to close the open file before the program ends, decoupling the file pointer from the file. Closes the file with the fclose (file pointer) function, sends the data in the buffer to the disk file, and then releases the file pointer. Successfully returns 0, failure returns non-0.

5 file reading and writing

After the file is opened, you can read and write to it, commonly used file read and write functions are:
①FPUTC and Fgetc
Fputc writes a character to a file in the form of FPUTC (CH, FP), and writes the character ch to the file that the FP points to. The character is returned successfully, and the failure returns eof,eof defined as a symbol constant-1 in stdio.h.
Fgetc reads a character from the specified file, which must be opened in read or read-write mode. Call Form CH=FGETC (FP); read a WORD from the FP-pointed file assignments to CH, and when the file ends, FGETC returns an EOF, we can use function feof (FP) to determine whether the end of the file, return 1 to the end of the file, or return 0. This function applies to both text files and binary files.
②fread and Fwrite functions
Can read and write a set of data. The calling form is as follows:
Fread (buffer, size, count, FP);
fwrite (buffer, size, count, FP);
Buffer for a pointer to Fread, refers to the data from the file to read the location of the address, for Fwrite, is to write the data address of the file.
The number of bytes to read from the size
Count the number of bytes of data to read and write (in the book) is actually read and write.
FP File pointers
The two functions return a value of 1 and fail to 1, and are generally used for reading and writing binary files.
Note: Some c-compiler systems do not have these two functions.
③fprintf () and fscanf () functions
Formatted output and input functions are similar to printf () and scanf (), except that the read and write objects of fprintf () and fscanf () are not terminals but disk files. Call Mode:
fprintf (file pointer, format string, output list);
FSCANF (file pointer, format string, output list);
④fgets () and fputs () functions
The function is to read and write a string, such as:
Fgets (STR,N,FP);
Read the n-1 character from the file pointed to by FP and store it in STR, returning the first address of STR successfully.
Fputs ("the", FP);
Writes the string to a file that the FP points to. Successfully returned 0, failure is not 0.

6 The location of the file

There is a position pointer in the file that points to the current read-write location, and you can use the function if you want to force the position pointer to change:
①rewind the position pointer back to the beginning of the file
②fseek ()
The fseek () function can change the position of the position pointer arbitrarily to achieve random read and write files. Call form:
Fseek (file pointer type, displacement amount, starting point);
The starting point has the following three values:
Seek_set or 0 file start
Seek_cur or 1 file current location
Seek_end or 2 end of file
Displacement refers to the number of bytes moved at the starting point (positive to the end of the file, negative to the file head), and the general displacement using long data to avoid error in files larger than 64K. The fseek () function is typically used in binary files, because the text file is converted to characters, and the calculation can be confusing.
Fseek (FP, 100L, 0); Moves the position pointer from the file header to the end of the file by 100 bytes.
Fseek (FP, 50L, 1); Moves the pointer from its current position to the end of the file by 50 bytes.
Fseek (FP, -10l, 2); Moves the pointer from the end of the file to the file header at 10 bytes.
③ftell ()
Gets the current position of the stream file position pointer, successfully returns the amount of displacement relative to the file header, and the failure returns to -1l.

Top of Page

In addition, because ANSI C does not use a non-buffered file system, and other C systems also use the non-buffered file system, so for this chapter is only a little look, not to see such procedures do not know, hehe. This section mainly tells a few documents read and write about the function, did not take notes. If you care, look at it yourself.
At this point, the basic language of the book review again, because the work is too busy, it took me half a month's time. But overall, the harvest is still very large, there are many new things that have not been discovered before, there are many previously understood more obvious things, this time deepened understanding. In fact, after reading a chapter, it is best to answer the book after the problem, because this is the chapter on the knowledge point of the examination. At the same time, make a small program, also can improve their programming ability.

Today, take this simple note for friends to refer to, if there is any inaccurate place, sincerely welcome correction.

Contact: msn:loose_went@hotmail.com e-mail:webmaster@vczx.com
 
 
 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.