Dark Horse programmer--c Language-preprocessing directives, enumerations, Typedef, recursive functions, variable scopes

Source: Internet
Author: User

First, pre-processing instructions

1> all pre-processing instructions are preceded by the # number ;

2> preprocessing directives are executed before the code is translated into 0,1 ;

3> preprocessing Instructions no semicolon at the end;

4> The location of pre-processing instructions can be written casually;

5> preprocessing directives have scope, starting from the line in which the instruction is written, until the end of the file, you can use #undef to cancel the function of the macro definition;

3 types of preprocessing instructions

1> Macro Definition

2> Conditional Compilation

The 3> file contains

Two, macro definition 1. A macro defines a naming convention:

1> Capital Letters

1 #define COUNT

2>k First Letter Capital #define KCOUNT

2. Macro definition start and end,Note there is no semicolon at the end:
1 #define kcount 423.... . 4 5 #undef kcount67  89Char * name = "COUNT";

The COUNT at this point is not a macro definition, and the macro in double quotation marks is a string

3. The macro definition with parameters, the efficiency is higher than the function,
1 /* #define SUM (v1, v2)  v1+v2  This definition exists vulnerability 23 The correct writing format should be:4* / 5 #define  SUM (V1,V2)  ((v1) + (v2))

This allows the result of the calculation to be the correct result, preserving the integrity of the variable and the integrity of the expression

Attention:

almost every argument in the 1> expression is enclosed in parentheses;

2> macro definition calculation is purely a text substitution;

The 3> macro definition is called at compile time, and the function is called at run time;

Third, conditional compilation

Conditional compilation format: Variables typically define names for macros

1 // 1> 2 #if defined kcount/#ifdef kcount 3  4 ... 5 6 #endif 7 // 2> 8 #ifndef Kcount 9 Ten ...  One  A #endif

Conditional statements comparison in conditional compilation and functions:

Conditional statements:

1> conditional statements are translated at compile time for all if-else to 0,1,

2> executes while the program is running;

Conditional compilation:

1> only qualified statements are translated into 0,1, other conditions do not conform to the translation;

2> is executed at compile time;

Iv. the document contains

1><> represents the system comes with the file, "" represents a user-defined file;

2> does not allow looping include files, for example : A.ha contains b.h,b.h contains a.h ;

the declaration of the 3> function can introduce multiple lines, for example :

1 #include "lisi.h" 2 3 ... 4 5 #include "lisi.h" 6 7 #include <stdio.h> 8  9... Ten  #include <stdio.h>

System will not error, but it is not recommended to write

4> is generally written in the header file:

  1   #ifndef lisi_h   2   3   #define  lisi_h  4   5  int  sum (int  v1,  v2);    7  //     9  //   10  # endif  

The purpose of this writing: to introduce a header file is, to judge, only to introduce a header file;#include "lisi.h"

5> the name of the macro defined in the header file, typically in uppercase lisi_hof the header file name, the value of the defined macro can be empty

#define LISI_H 123 which 123 can not write

6> Note: Typically a header file corresponds to a macro, and the macro name is named after the header file name

V. Enumeration types

Format:enum type name

{

Value 1,// Note here is a comma, not a semicolon

Value of 2,

...

};

enum season{spring; Summer; Autumn; Winter;}; enum 122; // The variables defined in this way are also possible because C is a weak language, so this can be done by // Enumeration Type Note:  enum  {    sexman,    Sexwoman} Sex; // Example Naming conventions
Vi. Typedef
1 typedef role: Alias2 //1>3typedefintInterger;4 //2>5typedefChar*string;6 //3>7typedefstructstudent Stu;8 //4>9typedefenumSeason Season;Ten //5> Onetypedefint(*mypointer) (int,int);//pointers to functions A 6> -typedefstruct Person -  the { -  - intAge ; -  + } MyPerson; -  + 7>typedefenumWeek A  at { -  - Monday; -  - Tuesday; -  in Wendsday; -  to } Myweek; +  -   the //To define an alias using a macro: * //1> $ #defineInterger intPanax Notoginseng  - #defineString char * the //2> +typedefintInterger; A  thetypedefChar*string;
Vii. Recursive functions

Two conditions for recursion:

The 1> function itself calls itself;

The 2> must have a definite return value.

Steps for recursive functions:

1> Find the law, find out the relationship between the previous data and the current data ( generally inductive series);

2> Find the number of values that can be set for a particular value, typically the position of the smallest value, such as n=1;

3>

1 int pow2 (intint  N)23{45if (n<=0return1; 6 7 return pow2 (b,n-1) *b; 8 9 }

According to the listed relational writing function, for example, the relationship expression for this function:b 's n- th square = b of the n-1 - Square *b;

Apply this relationship directly to Pow2 (b,n-1) *b ;

Recursive functions The most important thing is to find the law.

Viii. Externand theStatic

Difference:

Exter external functions, defined functions can be accessed by this file and other files, do not allow two files with the same name;

The Static intrinsic function, the defined function can only be accessed by this part, other files cannot be accessed, if you want to access, you may use the extern function to access indirectly;

use of static modifier local variables:

1> If the invocation frequency of a function is particularly high;

2> the value of a variable inside this function is constant;

Dark Horse programmer--c Language-preprocessing directives, enumerations, Typedef, recursive functions, variable scopes

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.