preprocessing directives and recursion in C language

Source: Internet
Author: User

preprocessing directives and recursion in C language

Last month completed the Dark Horse Programmer's iOS basic video tutorial, still remember the beginning of learning, what do not understand, now learn the basic feeling really good!

Every day is looking forward to the dark horse, longing for the future life. The road to the dark Horse is getting closer, I am really excited! These days have been reviewing, I feel the C language preprocessing instructions and recursion are almost forgotten.

Preprocessing directives: are some of the instructions that the compiler executes before compiling the code into 0 and 1. All pre-processing instructions are preceded by #.

  The preprocessing directives are divided into three types:

1. Macro definition

    Usage-such as: #define MYINT int means the int in the code below this code to the right int is replaced by MYINT, #define是固定写法, MYINT is the substitute name (macro name), can be arbitrarily named but the macro name is generally used in uppercase, or start with K. int is substituted, it can be numeric, arbitrary data type,

  2. Conditional compilation

Similar to the IF statement, the following code is mutated if the condition is met, and the compilation of this code is not performed if it is not satisfied. In general, all the code in the original program is compiled, but sometimes it is desirable that some of the content be compiled only if certain conditions are met. That is, specify the compilation criteria for a subset of content.

Format: #if (condition)

Statement

#elif (conditions)

Statement

#else

Statement

#endif---> Be sure to write, which means end conditional compilation!

  3. The file contains

There are two types: A ' include file directory ', which is generated in the installation directory when compiling the software. Most will have an include directory, which is the header file that the compiler provides. This is the system file. Usage is: #include < include file >

The other is ' source file directory ': The directory in which you write the program, that is, the files you write yourself. Usage: #include "source file"

The purpose of the file is to copy the contents of the file containing the following files to the current file!

Recursive----means that the function calls itself

The algorithm that can use recursive description usually has this characteristic: in order to solve the problem of scale N, we try to decompose it into smaller problem, then we can construct the solution of big problem conveniently from the solution of these small problems, and these smaller problems could be decomposed into smaller problems by using the same decomposition and synthesis method. And the solution of the larger problem is constructed from the solution of these smaller problems. In particular, when scale is n=1, it can be solved directly.

Execution process: recursive algorithm execution process of recursive and regression two stages. In the recursive phase, the solution of the more complex problem (size n) is solved by the problem that is simpler than the original problem (the size is less than n). For example, in the example above, solve FIB (n) and push it to solve FIB (n-1) and fib (n-2). That is, in order to calculate FIB (n), the FIB (n-1) and fib (n-2) must be computed, while the FIB (n-1) and fib (n-2) must be computed, and then the (n-3) and fib (n-4) should be calculated first. And so on, until the FIB (1) and fib (0) are calculated, respectively, the results 1 and 0 can be obtained immediately. In the recursive phase, there must be a case for terminating the recursion. For example, in a function fib, when n is 1 and 0. In the regression phase, when the solution of the simplest case is obtained, the solution of the slightly complex problem is obtained, such as the FIB (1) and fib (0), and the results of the FIB (2) are returned. After the results of FIB (n-1) and fib (n-2) are obtained, the results of FIB (n) are returned. When writing recursive functions, it is important to note that the local variables and parameters in the function are confined to the current call layer, and the parameters and local variables in the original level are hidden when the "simple problem" layer is pushed forward. In a series of "simple problem" layers, they each have their own parameters and local variables. Function: Recursive algorithm is relatively inefficient because recursion causes a series of function calls and there may be a series of repeated computations. When a recursive algorithm can be easily converted into recursive method, the program is usually programmed by recursive method. For example, the function fib (n), which computes the nth term of the Fibonacci sequence, should use a recursive algorithm that starts with the first two entries of the Fibonacci sequence and calculates the next item from the first two successive entries until the nth item of the requirement is calculated.

preprocessing directives and recursion in C language

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.