Start-C Program Design

Source: Internet
Author: User

According to tan haoqiang, the second version of C language programming has probably been turned over.
Chapter 1 C language Overview
1. C language was developed on the basis of language B. After the sixth version of Unix was released in 1975, C language attracted widespread attention.
2. C language features: concise, compact, easy to use, multiple operators, rich data structures, structured control statements, less strict syntax restrictions, bit operations, implements most of the functions of the Assembly Language and can operate on the hardware directly. The quality of the generated target code is high, the execution efficiency is high, and the portability is good (compared with the compilation );
3. Brief Introduction: The C program is composed of functions and must have a main function as the entry. The function consists of the first [function type function name, parameter type, parameter Name] and function body [Declaration part, execution part]; statement separated by semicolon (;);/* ----- */
4. Running procedure: Enter the source program> compile> link to the library function> run the executable Target Program (. c -->. OBJ -->. EXE)
 
Chapter 2 Program soul-Algorithm
1. The program contains the description of the data [data structure] and the description of the operation [algorithm]... program = Algorithm + Data Structure + programming method + language tools and environment
2. In a broad sense, the methods and steps used to solve the problem are called algorithms ,.. this is mainly about algorithms that can be executed by computers (for example, I cannot get a haircut ). numerical Value [numerical solution] and non-numerical algorithm [Transaction domain, such as retrieval management scheduling]
3. Example: for example, determining the year of the rising, finding the average, determining the prime number, and obtaining regular addition, subtraction, multiplication, division, and so on
4. algorithm features: poor, deterministic, zero or multiple inputs, one or more outputs, and validity,
5 ,. representation of algorithms: natural languages (such as Chinese and English) are expressed in flowcharts (expressed in some frames) (traditional BS flowcharts, n-S structured flow chart large box contains a small box [including order, selection and cycle structure box]
Pseudo-code representation (a mix of Chinese and English, clearly express the meaning). It is expressed in computer language (with C, language, Java language, etc)
6. Structured Program Design: top-down, gradual refinement, modular design, structured Coding

 
Chapter 3 data types, operators, and expressions
1. Data Type: basic type: [integer, floating-point (single-precision and double-precision), enumeration type], construction type: [array, struct, shared body], pointer type, null type (void)
2. constants and variables. The value that cannot be changed during the running of a program is called a constant. Generally, it can be determined from the literal meaning. A identifier can be used to represent a constant, which is defined by # define, for example, if # define price 30 is used in place of price, no value can be assigned to price. When the value is changed, 'change all'
Variable: The amount of variable whose value can be changed is called a variable. A name should occupy a certain storage unit in the memory. the variable name is actually a symbolic address. during compilation, the system allocates the memory address. The program finds the corresponding memory address through the variable name. From the storage unit value --- the variable must be defined first and then used
3. Integer Data: decimal, octal (starting with 0, for example, 0123 equals 83,-011 equals-9) hexadecimal (starting with 0x, for example, 0x123 equals 291, 0x12 equals-18
Storage Method: two bytes, 16 bits, and the power of 2. If there is a symbol, it is taken as the symbol,
4. Real (floating point type)-in decimal or exponential form, such as 1.23e3; float occupies four bytes. Double occupies eight digits, long double occupies 16 digits, score character, index part, and decimal part
5. string data, such as 'A '.. '\' is an escape character. For example, \ n indicates line feed and \ indicates \.. \ 'refers to'; can only store one character. Put the ASCII code corresponding to the character into the storage unit.
6. Arithmetic Operation Method: arithmetic, relational, logic, bit, value assignment, condition, comma, pointer, subscript, etc.
7. Value assignment operator =, type conversion

Chapter 4 sequential Programming
1. C statement classification: control statement [If else, for while continue, break, return, etc.], function call statement, expression Statement (operation, value assignment, etc.), empty statement, statement that is included in {}
2. Data input and output. for example, printf and scanf are not part of the C language text. They are the functions provided by C in the form of libraries in the system. They must be precompiled with the command # include to add header files, for example, # include "Studio. h"
3. character input, output char C; putchar (C), c = getchar (); format input and output printf (format control, output list) such as printf ("% d, % C \ n ", I, C ).
4. Format character: D format character, output decimal integer, % d, % MD (M indicates output width), % LD (output long integer
O format, which outputs integers in octal format
U format, output unsigned integer,
C format, output a character
Output a string in S format, % s, % ms, %-MD, % m. nd ---
F format, eformat, G format
To output %, use 2 percentage signs such as printf ("% F %", 1.0/3) -- output 0.33333%
5. Input Functions in scanf format
6. Sequence Structure Design Example

Chapter 5 select structure Program Design
1. If (relational expression) Statement 1 else Statement 2
2. & Logic and, | logic or ,! Non-logical
3. Switch statement. Note that the break is added;

 

Chapter 6 circular Control
1. GOTO statement
2. While statement
3. Do-while statement
4. For statement
5. Nested loop,
6. Break statement and continue statement

Chapter 7 Array
1. One-dimensional array definition: type specifier array name [constant expression] such as int A [10]
2. One-dimensional array reference: array name [subscript] For example, a [0] --- A [9]
3. initialize a one-dimensional array: (1) assign values when defining an array, such as int A [5] = {, 4} (2) assign only some values, for example, int A [5] = {0, 1, 2, 3}. Assign only the first few values (3) to all values. 0 can be written as int A [5] = {0}
(4) When assigning values to all elements, you can leave the array length unspecified, for example, int A [] = {0, 1, 2, 3, 4}.
4. 2d array definition and reference
(1) Definition: The type specifier array name [constant expression] [constant expression] For example, int A [3] [4] defines an array of three rows and four columns
(2) Reference: array name [subscript] [subscript]
(3) assign a value to a: for example, int A [3] [4] = {1, 2, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}
5, character array
(1) Definition: for example, char C [10] (2) references, (3) initialization is similar
(4) string and string ending sign: the string is processed as a character array in C language. The system automatically adds '\ 0' to the String constant as the ending sign.
(5) string processing functions: puts (character array), gets (character array), strcat (character array 1, character array 2), strcpy (array 1, array 2 ), strcmp (string 1, string 2)
Strlen (character array) Test the actual string length, excluding '\ 0 \ strlwr (string), strupr (string) is string to lowercase, string to uppercase

 

Chapter 8 functions
1. Overview: A source program file consists of one or more functions. The source program file is a compilation unit rather than a function. a c program consists of multiple source programs.
The C program starts from the main function and ends in the main function. The functions are parallel. The user-defined functions include standard functions (library functions) and user-defined functions. There are parameters and no parameters in the form.
2. Definition form: type identifier function name ([parameter list]) {declaration part, statement ;}

3. Parameter. The parameter is defined as a parameter, and the value assigned to the parameter is a real parameter.
4. Return Value: obtained through the return statement.
5. Function call
6. Function nesting
7. Function Recursion
8. Passing arrays as parameters
9. Local variable, global variable
10. Storage Class of variables: static storage (fixed storage space and dynamic storage are allocated when the program runs (allocated according to dynamic needs during runtime)
Auto. Static, register variable
11. Declare external variables with extern; declare external variables inside a file, declare external variables in Multi-file programs
12. Use static to declare external variables
13. Internal function external Function
14. How to run a multi-file program: Create a project file and use the include command
Chapter 9 preprocessing commands
1. Macro definition # define identifier string
2. File Inclusion # include <File Name>
3. Conditional compilation # ifdef identifier
Program Section
# Else
Program Section
# Endif

 

Chapter 10 pointer
1. You can't write it out ,,,
Each byte in the memory has a number, and the variable is operated through the variable name in the program. In fact, after compilation, the variable name has been converted to the variable address, is read through the address,
A variable is used to store the address of another variable. It is called a pointer variable. In this way, an operation pointer is equivalent to an operation variable.
2. Define variable pointer. Base type * pointer variable name. For example, int * P
3. pointer variable reference: * pointer operator, & get address Operator
4 .. When a pointer is passed as a function parameter, the value of the real parameter is changed internally.
5. Array and pointer y
Pointer to an array element, for example, int A [10]; int * P; P = & A [0]. Then P is the address of the [0] element.
6. Using a pointer to reference the array element m... assuming that P points to the first element of array A, p + 1 points to the next element of array.
7. Use the function name as the function parameter
8. Multi-dimensional array and pointer
9 string and pointer
-------------
Borrowed from the Internet:
(1). addition and subtraction of pointers: addition and subtraction of pointers are not simple addition and subtraction, but addition and subtraction based on the number of bytes occupied by pointer variables.
(2) The array name is a pointer constant and cannot be changed. For example, if array a [] is executed, an error is returned.
(3 ). although the address in the pointer variable is an integer data, the integer data cannot be directly assigned to the pointer variable. This should be the case (if p is int * type), P = (int *) 1000; but disable some methods, because you do not know whether the Unit 1000 is occupied by other units, and the consequences are unpredictable. Likewise, you cannot directly assign the value of a pointer variable to an integer variable, forced type conversion is also required.
(4) After defining the pointer, you must first assign the initial value to it before using it. Remember
(5 ). pointer problem in two-dimensional array (int A [3] [4]). array name a is a pointer to a row (pointing to an array), and adds *, * A is the pointer to a column (pointing to a non-prime object ). both a and * point to a [0] [0]; but a + 1 (row pointer) points to a [1] [0]; and * A + 1 (column pointer, it is the same as * (a + 0) + 1) but points to a [0] [1]. Similarly, adding a * Before a + 1 becomes a column pointer, * (a + 1) + 2 points to a [1] [2].
(6) Description of complex types: Starting from the variable name, combined according to the operator priority, step by step analysis, the following uses a complex type (int * (* P (INT) []) as an example:
In the first step, P is first combined with (), indicating that it is a function. In the second step, there is an int in (), indicating that the function has an input parameter of the int type. In the third step, P is combined, it indicates that a pointer variable is returned. Step 4 is combined with [], indicating that the pointer variable points to the array. Step 5 is combined with step *, indicating that the elements in the array are pointer-type, step 6 shows that the pointer element in the array is of the int type, so this is a function that returns a pointer variable of an array composed of integer pointer variables.

 

-------------
Chapter 2 structure, shared body
1. Defines struct variables, variables reference, initialization, struct Arrays
2. Pointer to struct type
3. Use pointers to process linked lists and linked lists
4. Enumeration type
5. Typeof definition type

 

Chapter 4 bitwise operations
1. Bitwise operations are binary operations,
2. Including: & bitwise AND ,~ Invert, | by bit or, <shift left, ^ by bit or,> shift right --- Remove ~ Both the inverse and outer operations are binary operators.

Chapter 4 Documents
1. The operating system manages files in units; c. The file is treated as a character (byte) XU Lei .. the data is divided into ASCII files and binary files according to the data organization form.
Operations on files in C language are completed through library functions.
2. File Type pointer: defines the structure of a file, such as typedef struct {short level; unsigne flag;, etc.} file; --> one or two file Arrays can be defined: file F [5] --> multiple file information can be stored --> file type pointer file * FP can be defined to direct FP
The struct variable of a file to access the file.
3. Open the file, close the fopen function, and return a file pointer. The file * fp = fopen (file name, open mode) open mode [R read, W write, a append, B binary]: r, W, A, Rb, WB, AB,
R + read/write, which must already exist. W + creates a new file for read/write. If a file is deleted, A + opens a text for read/write, and append it If yes. close with fclose (FP );

4. File read/write: fputc function, fgetc function, putc, GETC, fread, fwrite, fprintf, fscanf, getw, fgets, fputs, putw
5. File Location: rewind (relocated at the beginning of the file) fseek (File pointer, displacement, starting point). ftell (FP) gets the current position of the streaming File
6. Error Detection: The ferror function. The ferror (FP) returns 0 without error. The clearerr function makes the file error mark and end mark zero.

 

Chapter 14 (C ++'s expansion of C)
1. Added a // form comment.
2. Cin> cout <for Input and Output
3. c ++ --. cpp C plus
4. Overload (the number of function parameters, types, and return types can be different) different functions of functions with the same name.
5. Function (with the default parameter, put it at the rightmost end, such as void flying (int A, int B, int c = 10 );
6. . Reference (the new variable type in C ++-alias for a variable-for example, int A; Int & B = A; declares that the reference does not separate memory units, representing the same variable, changes are also made when one is changed to another.
Function parameter transfer method
(1) The value passed to the parameter, such as void swap (int A, int B) -- int I = 3, j = 5; swap (I, j) -- interaction is performed in SWAP, but the I and J values remain unchanged.
(2) pointer to the parameter, such as void swap (int * a, int * B) -- int I = 3.j= 5; swap (& I, & J) -- the operations in SWAp are (INT type) * A and * B (address ). the value of I and J has changed.
(3) the alias added by C ++ to the parameter variable, for example, void swap (Int & A, Int & B) -- int I = 3, j = 5; swap (I, j) -- the operations in SWAp are (Int & type) a, B (alias), and the values of I and j change
7. built-in functions: Add inline before the function name, such as inline int max (int A, int B ). it is equivalent to adding the modified function to the place where it is called as a program segment, saving the running time. It is similar to macro replacement and not exactly the same. macros are processed by the Preprocessing Program before compilation, replace simple characters without syntax check, while the built-in functions are processed during compilation for syntax check.
8. Scope OPERATOR: A indicates the variable A in the global scope;
9. New/delete Dynamic Allocation undo memory

Chapter 15 (C ++ object-oriented basics)
1. In the objective world, everything can be seen as an object. It has attributes and behaviors. The object type is called class in C ++. Class is the abstraction of objects, and object is the entity of classes.
2. Use the keyword class to define a class (similar to an int to define an integer) -- use the "." operator to specify the attributes or methods of the class.
3. When an object is created, there will be default constructor and destructor used to initialize and release resources-both can be overloaded, no return value, the name is the same as the class name, before the analysis is added ~
4. inheritance and derivation; Inheritance Mechanism, creates a new class on an existing class, base class (parent class) -- subclass (derived class ). subclass has all the data and function members of the parent class, and new members are added; Use: to inherit.
For example, Class A: Public B
5. Define the form of a derived class (subclass): Class subclass name: [Reference permission] parent class name {new member ;};
By default, the reference permission is private; there is also public-access to the internal definition members, that is, the permission defined by the derived class is added to the original permission to obtain the minimum permission; such as the parent class public, if private is used for inheritance, it becomes private in the subclass. If the parent class is private, the subclass is private in any way.
6. the constructor of the base class will not be inherited. When executing the constructor of the derived class, you should call the constructor of the base class.
For example, a (int A, int B, int C): B (A, B) {---}; where a inherits from B,
7. When an object is created, the base class constructor is called before the subclass is called. During the structure analysis, the Child class is parsed before the parent class is parsed.
8. The significance of inheritance: Don't say it, be lazy?

Chapter 16: Common Errors and program debugging
--

1. I want to see the Compilation Principle: preprocessing first --- compiling --- connection --- execution
2. macro definition # One of the three pre-processing methods of the define identifier string is to [Replace] The pre-compilation processing without syntax check. There is no semicolon at the end and can be nested. It is usually at the beginning, you can use the # UNDEF command to terminate the macro definition scope.
Optional parameters. # define macro name (parameter table) string, for example, # define s (a, B) a * B
It is widely used in header files.
# DEFINE _ tstring STD: String
# DEFINE _ tmain main
# DEFINE _ tostream ostream
# DEFINE _ tcout cout
# DEFINE _ tsprintf sprintf
Improve the versatility and accessibility of the Program for easy modification, mainly to replace
3. Conditional compilation. If you want to compile Part Of The content only when certain conditions are met, the compilation conditions are specified, which is both a Conditional compilation.
The format is as follows # If NNN statement1; # Elif Mmm statement2; # else statement3; # endif
# If and # endif must appear in pairs,
For example, (1)
# Ifndef bm2_h
# Define bm2_h
# Endif
If bm2_h is not defined, bm2_h is defined.
(2) # ifdef bm2_uses_wchar
# DEFINE _ tstring STD: wstring
# Else
# DEFINE _ tstring STD: String
# Endif
If bm2_uses_wchar is defined, the macro defines STD: wstring AS _ tstring. otherwise, STD: string is defined as _ tstring [that is, when processing, replace the character _ tstring in the program with STD: string or STD: wstring].

4. # pragma preprocessing command: it is used to set the status of the compiler or to instruct the compiler to complete some specific actions.
For example, # pragma once is a commonly used command. You only need to add this command at the beginning of the header file to ensure that the header file is compiled once.
# Pragma once is used to prevent a header file from being included multiple times, # ifndef, # define, # endif is used to prevent a macro from being defined multiple times.
For example:
# If! Defined (bm2_lacks_pragma_once)
# Pragma once
# Endif
If bm2_lacks_pragma_once is not defined, execute # pragma once. Ensure that the header file (. h file) Where bm2_lacks_pragma_once is compiled only once.
5. # Typedef creates a new name for an existing type,
For example:
# Ifndef ep_msg_que
Typedef struct msg_queue {
Long mtype;
Char text [max_len];
} Msg_que;
# Define ep_msg_que
# Endif
If no ep_msg_que is defined, a structure msg_queue is defined and a new name msg_que is created for it. in the program, msg_que can be used to represent the msg_queue struct, for example, defining msq_que temp; defining a msg_queue struct temp
 
6. namespace usage, using namespace bm2; using namespace bm2: Data: dbclient;
 

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.