C language: Essay 7-preprocessing commands

Those beginning with # are preprocessing commands.

#include<stdio.h>//Standard input and output header files

#define PI 3.1415925//Macro definition command

In the source program, these commands are placed outside the function, and generally placed in front of the source file, they are called preprocessing part. (Because they were processed earlier than the source file)

1. Macro definition without parameters

1. There are no parameters after the macro name without parameters.

The general form of its definition is:

#define  标识符  字符串//其中“#”表示这是一条预处理命令
凡是以“#”开头的均为预处理命令。define为宏定义命令。”标识符“为多定义的宏名。“字符串”可以是阐述、表达式、格式串等。
//例如
#define PI 3.1415926
//他的作用是指定标识符PI来代替数3.1415926.
//在百你写源程序时,所有的3.1415926都可以由PI代替,而对源程序做编译时,将由预处理程序进行宏代换,即用3.1415926表达式去置换所有的宏名PI,然后再进行编译。

Description of macro definition without parameters:

(1) The macro definition uses the macro name to represent a string, and then replaces the macro name with the string when the macro expands. This is just a simple replacement. The string can contain any characters, it can be a constant, or it can It is an expression, and it is not checked during the preprocessing of the program. If there is an error, it can only be found when compiling the source program that has been expanded by the macro.

(2) The macro definition is not a description or a statement. It is not necessary to add a semicolon at the end of the line. If a semicolon is added, the semicolon will be replaced together.

(3) The macro definition must be written outside the function, and its scope is from the macro definition command to the end of the source program . To terminate its scope, use the #undef command. #undef identifier

(4) If the macro name is enclosed in quotation marks in the source program, the preprocessor will not substitute it for the macro. (Enclosed in double quotation marks, we treat him as a constant string to be output)

(5) Macro definitions are allowed to be nested, and the defined macro name can be used in the string of the macro definition. The macro expansion is replaced by the pre-processing program.

(6) It is customary to use capital letters for macro names to distinguish them from variables. However, lowercase letters are also allowed. (Because it is customary to use lowercase for variable names)

(7) Macro definition can be used to express data type, which makes writing convenient. 

For example: #define TNTEGER int (using TNTEGER a; means int a;)

-----PS: The difference between a macro definition and a data specifier defined by typedef

Difference: Macro definition is just a simple string replacement, which is done in preprocessing, while typedef (actually a statement with a semicolon ";") is processed at compile time. It is not a simple replacement, but a The type specifier was renamed. The named identifier has the function of type definition description.

Use the following examples for comparison:

#include<stdio.h>
#define PTN1 char*//PIN1是一个char* 是一个指针形式的字符
typedef char* PIN2;//指针形式的字符为PIN2
void main()
{
    PIN1 x,y;//用PIN1定义x和y//3宏定义替换:char* x,y;(相当于定义x是一个指针而y是一个字符型)
    PIN2 a,b;//用PIN2定义a和b//4而用typedef呢使类型重命名,将char*用PIN2表示,也就是说这里相当于char* a,char* b;所以打印出来不一样
    printf("By #define:%d %d\n\n",sizeof(x),sizeof(y));//分别取他们的大小然后打印。1打印结果4 1
    printf("By typedef:%d %d\n\n",sizeof(a),sizeof(b)); //2打印结果:4  4

}

(8) Macro definition of "output format" can reduce writing trouble. (For example, it is troublesome to print %d and %s one hundred times with printf during output)

#include<stdio.h>
#define P printf
#define D "%d\n"
#define F "%f\n"

void main()
{
   int a=5,c=8,e=11;
   float b=3.8,d=9.7,f=21.08;
   P(D F,a,b);
   P(D F,c,d);
   P(D F,e,f);

}

2. Macro definition with parameters

C language allows macros to have parameters. The parameters in the macro definition are called formal parameters, and the parameters in the macro call are called actual parameters.

For macros with parameters, in the call, not only the macro expansion, but also the actual parameters to replace the formal parameters.

The general form with parameter macro definition is:

#define 宏名(形参表)  字符串//不带分号“;”

The general form of macro call with parameters is:

宏名(实参表);//带分号“;”

E.g:

#define M(y) y*y+3*y//宏定义//参数是y,字符串是y*y+3*y
...
K=M(5);//宏调用//传递给的实参是5,在调用时,实参5去替换形参y。(但是这里跟函数的调用值传递是不一样的,这里是直接的替换)

Examples:

#include<stdio.h>
#define MAX(a,b) (a>b?a:b)//a和b是虚有的只是一个替代的符号
void main()
{
    int x,y,max;//定义了类型
    printf("input two numbers:")
    scanf("%d %d",&x,&y);
    max=MAX(x,y);//这里替换为max=(x>y)?x,y;//上边的a和b就被x和y实参化了
    printf("The max is %d\n\n",max);

}

Description of macro definition with parameters:

(1) In the macro definition with parameters, there can be no spaces between the macro name and the formal parameter list. For example, the above cannot be written as #define MAX (a, b) and there can be no spaces between them.

(2) In the parameterized macro definition, the formal parameters do not allocate memory units, so there is no need to make type definitions. The actual parameters in the macro call have specific values. To use his to replace the formal parameters, so the type specification must be made. (This is different from the situation in the function. The formal parameter and the actual parameter in the function are two different quantities, each with its own scope, (that is to say, the formal parameter in the function definition has its variable value, the actual parameter There are other variable values) when calling, the actual parameter value should be assigned to the formal parameter to carry out "value transfer". In the macro with parameters, only symbol replacement, there is no problem of value transfer.) (In the macro, only the actual parameter has other Scope of Action)

(3) The formal parameter in the definition of the macro is an identifier, and the actual parameter in the macro call can be an expression. (What is an expression is actually a formula)

#include<stdio.h>
#define SAY(y)  (y)
void main()
{
    int i;
    char say[]={73,32,108,111,118,101,32,102,105,115,104,99,46,99,111,109,3}
    while(say[i])
    {
        say[i]=SAY(say[i]+1);//可以是表达式(刚才打印的各个字符的ASCII码加1)
    }
    printf("\n\t%s\n\n",say);//打印出来I love fishc,com!
}
#include<stdio.h>
#define SAY(y)  (y)^(y)//里边的符号^是异或的意思
void main()
{
    int i;
    char say[]={73,32,108,111,118,101,32,102,105,115,104,99,46,99,111,109,3}
    while(say[i])
    {
        say[i]=SAY(say[i]^say[i]);
    }
    printf("\n\t%s\n\n",say);

}

(4) In the macro definition, the formal parameters in the string are usually enclosed in parentheses to avoid errors. (Add parentheses to avoid priority changes)

(5) Parameters with macros and functions with parameters are very similar, but there are essential differences. In addition to the points mentioned above, the result of processing the same expression with functions and macros may be different.

Three, conditional compilation

The preprocessor provides a conditional compilation function. You can compile different parts of the program under different conditions, resulting in different object code files. This is very useful for program transplantation and debugging.

There are three unspoken rules for conditional compilation:

The first type: The function is: if the identifier has been defined by the #define command, compile block 1; otherwise, compile block 2.

#ifdef  标识符//如果有定义这个标识符的话,那么就执行程序1
    程序段1
#else//没有定义的话就执行2
    程序段2
#endif

 If there is no program segment 2 (it is empty) #else in this format can be absent, that is, it can be written as:

#ifdef  标识符//如果有定义这个标识符的话,那么就执行程序
    程序段
#endif

The second form:

#ifndef  标识符//如果没有定义这个标识符的话,那么就执行程序1
    程序段1
#else//有定义的话就执行2
    程序段2
#endif

The third form:

#if  常量表达式//if 0下边的就不会被编译执行做注释用的多(相当于一个开关,是1就执行程序1,是0就执行程序2)
    程序段1
#else
    程序段2
#endif

 

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/108682379