"C programming" reading notes

zhaozj2021-02-16  92

"C programming" reading notes

Keywords: C language original author Name: loose_went article original: vczx.com --- http://www.vczx.com/minute_c.php

Written in front:

"C programming" can be said to be a reprogramming book, but every reading is different, it can be said that every time it is read, there will be many new gains. The so-called old book reads, the aftertaste is endless! This note is "C program design" Tan Haoqiang, Tsinghua University Press published. In addition to the key knowledge points in the book, I also added my understanding of knowledge points. I think this is the importance of reading notes.

Chapter 1 Overview Chapter 2 Data Types, Operators and Expressions Chapter 3 The Leading C Profile Design Chapter 4 Logical Operations and Judgment Selection Control Chapter 5 Cycling Control Chapter VI Chapter VIE Chapter Chapter 8 Preparation Compiling Treatment Chapter 9 Pointer Chapter 10 Structure and Common Body Chapter 11 Chapter 12 Document

Chapter 1 Overview

1. C language characteristics

1 Language is simple, compact, easy to use, flexible. A total of 32 keywords, 9 control statements. 2 The operator is rich, and there are 34 operators. 3 The data structure is rich, the data type is: integer, real, character, array, pointer, structure, shared body, etc. 4 has a structured control statement (such as if ... else, while, do ... while, switch, for) 5 grammar limit is not very strict, the degree of freedom in program design is large. 6 Allow direct access to the physical address, the bit (BIT) operation can be performed directly to the hardware operation. 7 Generate a high quality of the target code, and the program execution efficiency is high. 8 is good to be portable.

2. C language use

C Although it is not good at scientific computing and management, the operating system and system utility and the hardware are operated, C has a significant advantage. Many large applications are now written in c.

Top of page

Chapter II Data Type, Operators and Expressions

1. C data type

C data types include: integer, character type, real or floating point (single precision and double precision), enumeration type, array type, structural type, common size type, pointer type, and empty class.

2. Constants and variables

Confariable values ​​are not changed, and symbol constant names usually use capital. Variables have changed, and the variable name can only be composed of letters, numbers, and underscores, and the first character must be letters or underscores. Otherwise, the number of variables that is not legal. The variable is assigned to the corresponding storage unit when compiling.

3. Integer data

The expression method of integer: decimal is not said, octal starts with 0, such as 0123, and hexadecimal starting with 0x, such as 0x1e. The integer variable is divided into: Basic (int), short int), long integer (long int), and unregulated. The number of memory bytes in different machines is different, and the general int type is 2 bytes, and the long type is 4 bytes.

4. Real data

Realistic constant representation: decimal form consists of numbers and decimal points (must have decimal points), such as: 0.12, .123, 123., 0.0, etc. Index forms such as 123E3 represent 123 × 10 three times. The real variable is divided into two types of single precision (float) and double precision (Double). In the general system, the float is 4 bytes, 7 valid numbers, Double type 8 bytes, 15 to 16 valid numbers.

5. Character data

Character variables are enclosed in single quotes, such as 'A', 'B', etc. There are also some special characters constants, such as '/ n', '/ t', etc. Represents the wrap and horizontal jumping. The character variable is defined in char, and one variable can only store one character constant. String constants is a character sequence enclosed by a double quotation. Here, you must pay attention to 'a' and "a", the former is the character constant, the latter is a string constant, C stipulates that the end of each string adds an end sign '/ 0', actually "a" contains Two characters: 'a' and '/ 0'. 6. Mixed operation between numerical data

Integer, characters, real data, can be mixed, different types of data should be converted into the same type, conversion rules: char, short -> int -> unsigned -> long-> double <- float

7. Operators and expressions

C operators include: calculative operator ( - * /%) relational operator (> <==> = <=! =) logic operator (! && ||) bit operator (<< >> ~ | ^ &) Assignment Operator (=) Conditional Operator (?:) Command Operator (,) Pointer Operator (* &) Sproduction (SIZEOF) Mandatory Type Conversion (Type) Component Operator (. ->) Standard Operator ([]) Other operators (such as function call operators ()) self-increasing self-loss operators ( -) Note: i and i , i before using i After i plus 1, i , make i plus 1. Common expression process: first solve the expression 1, solve the expression 2, and the value of the entire expression is the value of the expression 2.

Top of page

Chapter 3 The simplest C program design

1. C 9 control statements:

IF () ~ else ~ for () ~ while () ~ do ~ while () ContinuebreakswitchgotoreTurn program: sequential structure, selection structure, loop structure

2. Data output

The C language does not provide an input and output statement, and the input and output operation is done by the library function of C. But you want to include the header file stdio.h. Putchar () outputs a format character of a character printf () to the terminal: 1 D formatter is used to output decimal integer% D Pressing the actual length output% MD of the integer data to make the output length m, if the data length is less than m, then left Pressure, if it is greater than M, output actual length% LD output long integer data 2 O format The output integer 3 x format is output integer 4 u format in the sixteen-based form 4 u formats to output UNSIGNED data, Output 5 c formats in Ten Binary Form Use to output a character 6 SF output a string% S output the actual length string% MS output Strings M group, if the string length is less than M, left-pressed space, if it is greater than M The actual output%-ms output is spectrous M columi, if the string length is less than m, the right tonic space, the% M.NS output accounts for M columi, but only the left end n characters in the string and right alignment% -M. NS m, n meaning, align by left, if n> m, then M automatically take N-value 7 F format synergies with decimal output real value F integer parts all output, decimal part output 6-bit% M.NF output data Tropal, where N-bit decimal is available. If the value length is less than m, the left-pressed space% -m.nf is the same, the right-patch space 8 E format is specified by the index form, the real number, 5 index (E 002) ⑨ g format output real , Automatically select F format or E format 3 depending on the size of the value. data input

GetChar () From the terminal input a character scanf (Format Control, Address List) Standard C Scanf does not use% U, for unsigned data, input by% D or% O or% x. % Later *, used to skip its corresponding data. The accuracy such as Scanf ("% 7.2F", & A) is not legal when entering data;

Top of page

Chapter IV Logic Operation and Judgment Selection Control

1. Relational operator:

C provides 6 relational operators (> <<=> = ===! =), the first four priorities are above the latter.

2. IF statement

C provides three forms of IF statement if (expressions) statement IF (expression) statement 1 ELSE statement 2IF (Expression 1) statement 1ELSE IF (Expression 2) statement 2 ... ELSE statement N

3. Conditional operator

(A> b)? A: B condition is true, the expression is valued, otherwise the 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

Chapter 5 cycle control

1. Several cyclic statements

GOTO statement (now rarely used) While statement first judges the expression after executing the statement do-while statement first execute the statement to determine the expression for statement

2. BREAK statement and Continue statement

The Break statement is used to jump out of the loop, and Continue is used to end this cycle.

Top of page

Chapter 6 array

1. One-dimensional array

c specifies that only Static Static and External Storage (Extern) arrays can initialize. When you initialize the array, you may not specify an array length. 2. Two-dimensional array

3. Characters

Some string processing function PUTS (character arrays) output a string to the terminal. GETS (character array) Enter a string from the terminal to the character array, and get a function value, connects the string in the array of characters in the first address STRCAT (character array 1, character array 2) for the array of characters. It must be large enough. STRCPY (Characters 1, String 2) Copy the string 2 into the character array 1. STRCMP (string 1, string 2) compare strings, equal return 0, string 1> string 2, return positive, less than return negative numbers. Strlen (Character array) is stringing the string length. Strlwr converts uppercase letters in the string into lowercase STRUPR (string) converts lowercase letters in the string to uppercase or more than some commonly used string processing functions.

Top of page

Chapter VII Function

1. Description

1 Before the function is called, the meticulin does not occupy the memory 2, the argument can be constant, variables, or expressions 3 must specify the type of ginseng 4, the participation, the parameter type should be the same. Transfer ", ie one-way transmission

2. Function return value

If you want the function to return a value, use the RETURN statement in the function, you must specify the type when the function is defined. If you do not specify, the default returns an integer.

3. Function call

1) Note that the number of real parameters and metall parameters when the function is called, and the type should correspond one by one. The order in which the real reference is evaluated is uncertain, and some systems are pressed from left to right, and some systems are pressed from the right to left. This should be aware of this. 2) Function call mode: function statement, function expression, function parameter 3) If the master function and the modulated function are in the same file, and the main adjustment function is in front, then the modulo is usually adjusted in the main adjustment function. Be explained. Unless: (1) The return value type of the modulated function is the integer or character type (2) before the modulation function appears before the main adjustment function. 4) Differences for the description and definition of the function, definitions refer to the establishment of function functions, including specifying a function name, function value type, ginseng, and its type, function body, and the like. The description is just a defined function return value type, including only a function name, a function type, and an empty arc, excluding a ginseng and function body. 5) C language allows the recursive call of the function (directly or indirectly calling the function itself during the process of calling a function).

4. Array as function parameters

1) Array elements as function parameters and general variables The same 2) An array name as a parameter should define an array in the Motor and the modulated function, and the size of the array group may not be defined. Note: Array name is parameter, not one-way delivery. 3) Multi-dimensional grouping parameters, the first dimension of the first dimension can be omitted when defined in the modular parameter group, but the second dimensional or higher dimension is not omitted.

5. Local variables and global variables

From the variable scope angle, the variable can be divided into local variables and global variables. 1) Internal variable (local variable) is defined within a function, only valid variables within the function range. 2) External variable (global variable) In the function of the function, you can use the other functions of this file, and the valid range starts from the position of the defined variable to the end of this file. It is recommended to use the global variable as little as possible because it takes up resources during all the execution of the program, and the versatility of the function is reduced. If a function before defining an external variable To use the external variable, you should use external variables in this function.

6. Dynamic storage variables are divided into a time (survival) angle of the variable value from the variable value, which can be divided into static storage variables and dynamic storage variables. Static Storage Indicates that a fixed storage is assigned to variables during operation during operation of the program run. Dynamic storage index is assigned storage space as needed. In C language, the storage method of the variable is divided into two categories: static storage classes and dynamic storage classes, including: Auto, static (static), register, external (Extern). 1) Local variables in the storage mode of the local variables are not specifically description, that is, the auto, that is, dynamic storage, and Auto can be omitted. Local variables can also be defined as static, then it is constant in the function value in the function. If the static partial variable is not assigned, the system is automatically assigned to 0, and the dynamic local variable is not assigned, and its value is an uncertain value. C provides that the initial value can only be set to the logarithmic variable only when defining global variables and local static variables. In order to improve the execution efficiency, C allows the local variable value to be placed in the register, and this variable is called the register variable, and it is necessary to use the register. However, only local dynamic variables and form parameters can be used as Register variables, others can't. 2) The storage method of global variables The global variable is defined outside the function, and the compile time is assigned in a static memory area, and can be referenced in each function in the program. How does the situation of multiple files reference to global variables? If a global variable is defined in a file, you should use extern to global variables in this file, but if the global variable is defined, this global variable can only be referenced in this document. Can't be referenced by other files. 3) Storage category Summary from the scope of action, local variables and global variables local variables: automatic variable, dynamic local variable (exiting function, value disappearance) static local variable (exiting function, value still reserved) register variable (leaving Function, value disappears) (Conversion as Automatic Variables and Register Variables) Global Variables: Static Global Variables (This file reference) global variables (allowed other file references) from existing time division, static storage and dynamics Storage Dynamic Storage: Automatic Variable (Valid within this function) Register variable (effective in this function) Configure static storage: Static local variable (effective within function) Static global variable (this file is valid) global variable (other files can be referenced) Position static storage area from variable value: Static local variable static global variable global variable dynamic storage area: Automatic variables and metallologies: register variables

7. Internal function and external functions

Internal function: You can only be called by other functions in this document, define the Static, the internal function is also called the static function. External Function: You can be called by other files, before adding extern, if omitted, an external function is implicit, in the file that needs to call this function, generally uses extern description.

Top of page

Chapter 8 Preciprochemical Processing

The C compilation system advances before performing the usual compilation of the program. The preprocessing function provided by C mainly has the following three: 1) Macro Definition 2) File contains 3) condition compilation

1. Macro definition

Macro definition without parameters uses a specified identifier to represent a string, form: #define identifier character string Detailed description: 1) Macro name generally use uppercase 2) Macro definition does not make a syntax check, only in compilation When the source program is expanded 3) 3) Macro definition is not a C statement, not in the end of the line, the slogan 4) Macro name valid range is defined to the local file end 5) You can use the #undef command to terminate the macro defined scope 6) When the macro is defined, you can reference the macro definition of the defined macro name parameters: #define macro name (parameter table) string This is similar to the function, but they are different: 1) When the function is called, Expression values, re-entry, and macro is just simple replacement, and does not request value 2) The function call is allocated in the program runtime, and the macro show does not assign memory, and there is no return value concept 3) The real parameters and shapes in the function must be defined, and the requirements are consistent, the macro name has no type, and the parameters are not type. 4) The function has only one return value, and the macro can get several results 5) macro exclusions do not occupy the run time, accounting for only compile time, and the function calls occupy the run time

2. File contains processing

#include "file 1" is to insert all content copies of the file 1 into the #include position as a source file. In the #include command, the file name can be "" or <>, if the file1.h file is now included in file1.c, "" indicates that the system is looking for file2.h in the directory where File1.c is located. If not found If you retrieve the directory according to the standard manner specified by the system, <> indicates that the system retrieves the directory in the specified standard. So "" insurance is a bit.

3. Conditional compilation

Conditional compilation means that it is not compiled throughout the program, but compiled the part of the condition. Conditional compilation There are several forms: 1) #IFDEF identifier Procedure 1 #Else block 2 # ENDIF Its role: When the identifier has been defined in front (generally #define), the block 1 is compiled Otherwise compile the block 2. 2) #1fndef identifier Procedure 1 # Else block 2 # ENDIF Its role and #ifdef reverse it, when the identifier is not defined, compile the block 1, otherwise the block 2 is compiled. 3) #IF Expression Block 1 # Else Block 2 # ENDIF Its role: When the expression value is true (non-0), the block segment 1 is compiled, otherwise the block 2 is compiled.

Top of page

Chapter 9 Pointer

The pointer is white and the address is the address. The pointer variable is used to store the variables of the pointer (address).

1. Pointer and pointer variables for variables

Read it is very embarrassing, it is the address of the variable and the address variable used to store variable addresses. Because a variable is in compilation, the system is to allocate an address for it, if this address is used to store this address, then this variable is called a pointer variable to the variable, which is used to store such a variable of the variable address. The so-called "pointing" means that the address of the ××, "pointing" means the number of variables, "pointing" means to store the address of the variable, and then pointing to the array pointer variable, "pointing" is the address of the placement. As long as you understand this, the pointer is not difficult. In addition, there is a pointer variable to the string, pointing to a pointer variable, pointing to a pointer variable of a pointer, and the like. 1) Definition of pointer variable: Type identifier * identifier such as: int * Pointer; pay attention to two points: * indicates that Pointer is a pointer variable. When using this variable, it cannot be written as * Pointer, * Pointer is the variable pointing by Pointer Point . A pointer variable can only point to the same type of variable. As the POIinter above can only point to the INT type variable.

2) Two related operators for pointer variables: & Take address operators & a represents the address of the variable A * Pointer operator * a represents the value of the variable A

2. Array pointer and pointer variable to array

Array's pointer index set of start addresses, array elements of pointer index group elements. 1) The definition of the pointer variable to the array element is the same as the assignment definition of the assignment, and the pointer variable of the pointing variable, and C specifies the number of group names represent the first address of the array, that is, the first array element address. 2) Through the pointer reference array element, we usually refer to the form of an array element is A [I], if the pointer can be referenced, * (A i), or define a pointer variable P, assign the first address of the array A to P , P = a; then use * (p i) reference. Note: The pointer variable P point to the array A first address, then P points to the next element address of the array A, that is, the address of the A [1]. 3) The array name made function parameter parameter group and the real parameter are not a value transfer, but the same segment address is shared, so if the value of the parameter changes during the function call, the value of the arguments will follow. 4) Pointing to the plurality of dimensional arrays and pointer variables are numerous in two-dimensional arrays. Assume that a two-dimensional array a [3] [4] is defined, then A represents the first address of the entire 2D array, represents the first address of the 0th line, and the first address of the element of the 0th line 0 column. A 0 and a [0] represent the first line of the first line of the first line of the first line of the first line of the first line of the first line. Suppose A is the first address of an array, then if A is one-dimensional, A i represents the address of the i-th element, if A is two-dimensional, then A i represents the first address of the first line. So how is the element address of the second column of the first line? A [1] 2 or & a [1] [2] or * (A 1) 2. We only need to remember: in the 2D array A represents the first address of the entire array, A [i] represents the first address of the first line, A [i] and * (a i) are equivalent. Just use the skills that is not more complicated. 5) Pointer variables to the one-dimensional array of M individual numbers such as int (* p) [4], P is a one-dimensional array pointing to containing four elements, if P corrects A [0], then P 1 points to A [1], that is, the value of P is in units of one-dimensional array, here is 4, and an example: assuming a [3] [4] = {1, 3, 5, 7, 9 11, 13, 15, 17, 19, 21, 23}, P first point to a [0] is the first address of the array A, then P 1 is the first address of the A [1], that is, the address of the element 9, because In defining P, INT (* p) [4], defines a one-dimensional number of group lengths of 4, so P 1 is equal to the length 4.3 of a one-dimensional array. The pointer of the string and pointer variable to string

1) The representation of the string has two representations of the string: one is an array, one is character pointer char string [] = "i love c!"; Char * str = "i love c!"; In fact, the form of pointer has opened up an array in memory, and only the first address of the array is stored in the character pointer variable STR, don't think Str is a string variable. 2) The string pointer function parameter actually string pointer is the first address of the array. 3) Character pointer variables The difference between the character array 1 character array consists of several elements, each element stores a character, and the character pointer variable only the first address of the string, not the entire string 2 to initialize the array to initialize Static, The pointer variable is not available. 3 Assign value to the character array, only assign a value for each element, cannot be like this: char STR [14]; str = "i love c!"; The pointer variable can, char * str; str = "i love c!" ; Note: It is not a character assigned to the STR at this time, but the first address of the character. 4 arrays allocate memory units at the time of definition and compile, and it is best to initialize it after the pointer variable definition, otherwise the value of the pointer variable points to an uncertain memory segment will destroy the program. Such as: char * a; scanf ("% s", a); this method is very dangerous, it should be: char * a, str [10]; a = str; scanf ("% s", a); This character pointer points to a determined memory segment. 5 The value of the pointer variable can be changed, and the character serial address represented by the character array name cannot be changed. 4. Function pointer and pointer variable of function

A function is assigned an entry address when compiling, which is called a function of a function. The function name represents the entry address of the function, which is the same as the array. We can use a pointer variable to store this entry address and then call the function via the pointer variable. Such as: Int Max (int X, int y); when we call this function: int C; c = max (A, b); this is the usual call method In fact, we can define a function pointer to call, such as: int (* p) (); // pay attention to the definition of function pointer variables P = max; // This sentence is assigned to the entrance address of the function Function pointer variable PC = (* p) (A, b); some friends may understand (* p) () unlike understanding, in fact it means defining a pointer variable P, P is not fixed to which function is fixed But it is specifically used to store variables of the function entry address. In the program, which function is assigned to it, which function is pointed to. However, it should be noted that p cannot be made by P , P- and other meaningless operations like a pointer variable to the variable. Since P is a pointer variable, then it can be passed as a function of the function. One of the most common uses of the function of the function of functions is to pass as a function parameter to other functions. This is also a more in-depth part of the application in the C language.

5. Returns the function of the pointer value

We know that a function can bring back a integer value, character value, real value, etc., and functions can also bring back a pointer type data, ie address. The definition of this function is as follows: Type identifier * function name (parameter table) such as: int * a (x, y) Returns a pointing pointing to the integer pointer to use this function, pay attention: When calling, you must define first A suitable pointer to receive the return value of the function. This appropriate pointer is whose type should return the type pointed to the pointer. Such a function is relatively difficult to understand, and it is easy to handle it as a general function. When we feel that the pointer is difficult to understand, it will temporarily make an integer, it is much better. 6. Pointer array

The array of pointers is undoubtedly the array element is a pointer, defined as: type ID * array name [array length] such as int * p [4], don't write into int (* p) [4], this is a one-dimensional array Pointer variable. Pointer array is used to store the first address of several strings, pay attention, initialize when defining the argument array, as follows: static char * name [] = {"li jing", "wang mi", "xu Shang"}; In thinking that the array is a string, which is stored in the first address of the character, and this must pay attention.

7. Pointer to point to pointers

Speaking of it, use a pointer to store a variable, then this variable is a pointer to the pointer. Definition as: char * * p;

8. Pointer number group as a parameter of the main () function

The parameter of the function is main (int Argc, char * argv [] {} main function, is obtained from the command line, the argc refers to the number of command line parameters, pay attention to the command name is also a parameter, and the command line parameters are string. Their first address constitutes a pointer array argv. The shape of the main function is just a habit, or it can be defined as other names.

9. Pointer

1) Data types related to pointers

Define meaning INT i; define a integer variable I INT * P; P is a pointer variable INT A [n] to integer data; define an integer array A, it has N elements INT * P [n]; definition pointer The array p, which has n points to integer pointers INT (* p) [n]; p is a pointer variable INT f () pointing to the one-dimensional array of N elements; f is a function INT returned to an integer value INT * p (); p is a function of the return value as a pointer, which points to integer data INT (* p) (); P is a pointer to the function, the function returns a integer value int ** P; define a point pointing Pointer variable

2) ANSI adds a Void * pointer type, which is defined a pointer variable, but does not point to any data type, and then forced the conversion type when used. Such as: char * p1; void * p2; p1 = (char *) P2; can also define a function as a VOID * type, such as: Void * Fun (CH1, CH2) means function fun returns an address, it points to the empty type If this address is required, it is also necessary to force it. Such as (assuming P1 is CHAR): p1 = (char *) FUN (C1, C2);

The pointer should be said to be a more important concept in the C language. It is also the essence of C language. It has a lot of advantages, but it will bring serious errors, which requires us to use, practice, slowly accumulate experience. .

Top of page

Chapter 10 Structure and Surgery

1. definition

General Form of Structural Definition: Struct Structure Name {Member List}; Defines a Structural Variable Miss Structure: Struct Structure Structural Variable Name; 2. Reference of structural variables

Note the following rules when citing structural variables: 1) The structural variable cannot be output as an integral input, and only the various members of the variable can be output. The new standard C allows a structural variable to be directly assigned to another structure variable having 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. Structural array

Struct Student Stu [4] defines an array STU that is the struct student type, and the arrays have 4 elements. Note that each element of the array is continuously stored in memory. When defining a structural array, the number of array elements may not be specified. When compiling, the system will determine the number of array elements based on the number of structural constants of the initial value.

5. Pointer to the structural variable

Since the structural variable is continuously stored in memory, we can store the structure variable in memory into a variable, then this variable is a pointer to the structural variable. Note that the first address of the structural variable is assigned to the pointer variable: struct student stu_1; struct student * p; p = & stu_1; // To add the address, pointing the function and pointing the string not in the reference structure When a member in the variable, there are three ways: Take the above structure as an example: set P to point to the pointer to this structural variable, ie P = & a; 1) a.Num2) (* p) .num3) P-> Num

6. Pointer to the structural array

Struct student * p; struct student stu [4]; p = stu; then P is a pointer variable to the structure array. Here you should pay attention to P , P pointing to STU [0], P , pointing to STU [1]. P pointing to the first address of an element in an array, and cannot let P point to a member in the element, such as P = & stu [i] .name is wrong.

7. Function parameters with pointer to the structure

Although ANSI C allows the entire structure as a function parameter, all members values ​​are passed one by one, and the overhead is large. So use the pointer to parameters to improve operational efficiency. Struct Student Stu; uses the entire structure as a parameter call form: Fun (STU); and the modulo is also defined in the structural variable, Struct Student Stu; use pointer to parameter call form: fun (& stu); The function fun defines a pointer variable, struct student * p;

8. Pointer handling linked list

The chain is an important data structure because it can be stored dynamically. The linked list has a head pointer to store the first address of the entire linked list. The definition form of the linked list is as follows: struct node {int Num; ... struct node * next;}; next to store the address of the next node. How to make dynamic development and release storage units? c provides the following related functions: 1) Malloc (size) opens up a continuous space of size Size in the memory dynamic storage area. Successful return space first address, failed to return 0; 2) Calloc (n, size) opens up N length SIZE in the memory dynamic storage area. Successfully returned the space first address, failed to return 0; 3) Free (PTR) releases the memory from PTR. PTR is the value returned when the Malloc and Calloc is recently called. In the above function, n and size are integer, PTR is a character pointer. 9. Common body

Definition form: UNION shared body name {member list} variable list; common body and structure similar, just a point, unlike the starting address of a member in the structure, the length of structural variables in memory is the length of each member And; the starting address of the member in the common body is the same, the length of the memory length of the common body variable is the length of the longest member. Characteristics of the Type Data: 1) The same memory segment can store several different types of members 2) Members in the shared body variable are members of the last stored 3) cannot assign values ​​for the common body variable name, cannot be defined Time initialization. 4) You cannot use the common-variable variable as a function parameter 5) The common numeral type can appear in the structural definition, and vice versa, or may also define the shared group. In addition, the structural body name can be used as a parameter, and the shared body is not available. These two in the data structures have been used in different occasions.

10. Enumerated type

The definition form is as follows: ENUM WeekDay {Sun, MON, TUE, WED, THU, Fri, Sat}; Enum weekday workday, week_end; // Define Enumeration Variables Workday and Week_END are defined into an enumeration type, their value Can only be one of Sun to SAT. You can also directly define enumeration variables, this is the same ENUM Weekday {Sun, Mon, Tue, WED, THU, Fri, Sat} Wedday, Week_END; Note: Enumeration Elements As constant, they are worthless , C is compiled, and the value is 0, 1, 2, 2 ..., such as: The value of Sun is 0, and the value of MON is 1. Although the enumeration element value, it cannot be one Integers directly assign an enumeration variable. Mandatory type conversion should be performed, such as Workday = (Enum weekday) 2; it is equivalent to assigning Tue to Workday.

11. Define type with typedef

The role of TypeDef is to let you define your own favorite data type name instead of the existing data type name. Such as: typedef int int; then I can define integer variables with int. The role is like INT. Typedef is used for structural definitions such as TypeDef struct {Int day; int month; int year;} Date; Date BirthDay; Date * P;, etc. Typedef is used to facilitate the generality and transplantation of the program.

Top of page

Chapter 11

1 Overview

The so-called position is an operation of the binary position. In system software, there is often a problem of binary bits. The bit operand provided by c: & bit bit and | bit or ^ pressing or ~ reflecting << left shift >> right shift & to clear a unit clear, take some pointing in one number and Retaining a large purpose of pointing. | It is often used to place certain positions of a number 1. ^ Judging two bit values, different is 1, the same is 0. Often used to make specific bits flip, etc. ~ Commonly used in conjunction with other bit operators, often used to set the mask. << Put all the two into the binary position, the high level is overflow, and it does not work. The left shift is equivalent to the number of 2, and the left shift n bits are equivalent to multiplying 2n. Left shift is much faster than multiplication. >> Tell the sign problem when right shift. For the no sign, right movement into 0, for the number of symbols, if the original symbol bit is 0 (positive), turn into 0 left; if the symbol bit is 1 (negative number), the left shift into 0 or 1 Depending on the system. Improve 0 is called "logical right shift", shifted into 1 called "count right movement". 2) segment

Several information is divided into several segments. The so-called segment is a member in the structure type defined in the position of the unit. Such as: struct packed-data {unsigned a: 2; unsigned B: 6; Unsigned C: 4; Unsigned D: 4; Int i;} DATA; where A, B, C, and D occupies 2, 6, 4, 4 Bit, 4 digits. I is intellectual, accounting for 4 bytes. The reference to the segment member is as follows: DATA.A = 2;, etc., but should not pay attention to the range definitions of the bit segment. If the bit segment member A is defined as 2 bits, the maximum is 3, ie (11) 2, so data.a = 5; if it takes 5, the two low positions are assigned, and the desired value is not required. Some important instructions about the definition and reference of the segment: 1 If a certain segment is to be stored from another word, you can define: unsigned A: 1; Unsigned B: 2; unsigned: 0; Unsigned C: 3; Another unit) uses a bit section of length 0, and the effect is to store the next bit segment from the next memory unit. 2 A bit segment must be stored in one storage unit and cannot cross two units. 3 can define an unknown segment. Such as: unsigned A: 1; UNSIGNED: 2; (These two-bit space is not available) Unsigned B: 3; 4 Segment length cannot be greater than the length of the memory cell, nor can it define the number of bits.

Top of page

Chapter 12

1 Overview

The C language looks the file as a sequence of characters and is divided into ASCII files (text files) and binary files. That is, a C file is a word current or binary stream. The ASCII file gives an ASCII code for each byte, represents a character, the output and characters, which is easy to process characters one by one, but more space. The binary file is output to the disk in memory in memory, saving space, because the output is not corresponding to the character, can not directly output character form, generally used to save intermediate results. Currently C on file processing only has a method of buffer file system, that is, whether it is from a program to disk file or from a disk file to a program, the data must pass through a buffer, and then the buffer is full, it is set to be sent.

2) Folder type pointer

In the buffer file system, the key concept is the file pointer. Because each file is used to open a buffer in memory to store information about information. This information is saved in a structural variable, which is defined by the system, named File, defined in stdio.h. FILE * fp; defines a file pointer variable FP, which will be done by FP. 3) Opening and close the file

Open the file before reading and writing for the file. The function of opening the file is: fopen (), the call mode is: file * fp; fp = fopen (filename, use file mode); FOPEN () Failed to return an empty pointer NULL, then return a file pointer to "filename" Give the FP, so that FP is associated with the open file. Or, FP points to "filename". File usage: R, W, A, RB, WB, AB, R , W , A , RB , WB , AB , specific meanings to remember.

4) Put the file

To prevent data loss, before the end, be sure to turn the open file, so that the file pointer is detached with the file. Use the Fclose function to turn off the file, after performing the function, first send the data in the buffer to disk file, then release the file pointer. Successfully returns 0, and the failure returns to 0.

5) Document reading and writing

Once the file is opened, you can read it, the commonly used file read / write functions are: 1FPUTC and FGETCFPUTC Write a character to the file, the form is FPUTC (CH, FP); writing the character ch to the file pointed to by the FP. Successfully returned to this character, failed to return EOF, EOF is defined as symbol constant -1 in stdio.h. FGETC reads a character from the specified file, which must be opened by reading or reading. The call form is CH = FGETC (FP); reads a character from the FP to which a character is assigned to CH. When the file ends, the FGETC returns an EOF, we can use the function feof (fp) to determine if the file is tail, return 1 means that the end of the file has been arrived, otherwise it returns 0. This function is suitable for text files and binaries. 2Fread and FWRITE functions can read and write a set of data. The modes are as follows: Fread (buffer, size, count, fp); fwrite (buffer, size, count, fp); buffer is a pointer, talking to FREAD, refers to the storage address of the data from the file, to fwrite Speaking, it is the address of the data to write to the file. Size wants to read and write the number of words to read and write how many size bytes of the data item (on the book) is actually the number of times the number of FP file pointers these two functions have been successful 1, failure is not 1 Generally, it is generally used for reading and writing in binary files. Note: Some C compilation systems do not have these two functions. 3FPrintf () and fscanf () functions format output and input functions, similar to Printf () and scanf (), only a point, FPrintf () and FSCANF () read and write objects are not terminal but disk files. Call mode: fprintf (file pointer, format string, output list); fscanf (file pointer, format string, output list); 4fgets () and fputs () function functions are read and write a string such as FGETS (STR , N, FP); It is intended to read N-1 characters from the file pointing from the FP, stored into the STR, successfully returns the first address of the STR. FPUTS ("China", FP); write string china to files pointing to FP. Successful returns 0, failed to be 0.6) file positioning

There is a position pointer in the file, pointing to the location of the current read and write, if you want to change the position of the position pointer, you can use the function: 1Rewind to re-return the position pointer to the beginning of the file 2FSeek () fseek () function can arbitrarily change the position pointer. Location to achieve random read and write files. Call form: fseek (file pointer type, bit shift, starting point); starting point has the following three values: seek_set or 0 file SEEK_CUR or 1 File Current location SEEK_END or 2 File Last Lift Reference Refers to the starting point as the base point, move The number of bytes (the positive number moves to the file, the negative number to the file header), and the general displacement amount is used to avoid more than 64K file error. The FSeek () function is generally used for binary, because the text file is to be transformed, and it will be confusing. FSEEK (FP, 100L, 0); move the position pointer from the file header to the file to move 100 bytes. FSEEK (FP, 50L, 1); move the pointer from the current location to the file 50 bytes. FSEEK (FP, -10L, 2); move the pointer from the file tail to the file header 10 bytes. 3FTELL () gets the current position of the flow file position pointer, successfully returns the displacement amount relative to the file header, failed to return -1L.

Top of page

In addition, since ANSI C does not use a non-buffered file system, other C systems use non-buffered file systems, so for this chapter is just slightly looked at, not to see such programs in the future, huh, huh. This section mainly tells the relevant functions read and written in several files, and did not take notes. Let's take a look at it if you care. At this point, the basic book of this language class has been touched again. Because the work is too busy, I spent half a month. However, in general, harvest is still very large, there are many new things that have not been discovered before, and there are many things that have been more shallow before, and this time I have deepened understanding. In fact, after reading a chapter, it is best to answer the book after the book, because this is a test of this chapter. At the same time, you can use a small program and improve your programming capabilities. Today, take this simple note to your reference, if there is any inaccurate place, sincere welcomes you!

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

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

New Post(0)