[英]How #define preprocessor really works in C
Please consider the following codes 请考虑以下代码
#define FIRSTNAME ""
#define SECONDNAME "JOHN"
# define PATHSAVE(a) func(strcat(strcpy(tmpFileName, appDir), a))
int main() {
PATHSAVE(FIRSTNAME SECONDNAME);
}
By analyzing I found out that value "John" is passed to the function PATHSAVE. 通过分析,我发现值“ John”被传递给了函数PATHSAVE。 By I couldnt understand why two parameters are used in this function PATHSAVE(FIRSTNAME SECONDNAME)
据我所知,为什么在此函数中使用两个参数PATHSAVE(FIRSTNAME SECONDNAME)
What you wrote will be expanded as follows 您写的内容将扩展如下
func(strcat(strcpy(tmpFileName, appDir), "" "JOHN"));
^^ ^^^^^^
|| ||||||
|| SECONDNAME
||
FIRSTNAME
Passing two parameters to a macro require them to be separated by ,
and not by a space 传递两个参数给宏要求它们通过被分离
,
由一个空间和不
PATHSAVE(FIRSTNAME SECONDNAME);
will expand to PATHSAVE("JOHN") as the preprocessor will concatinate the 2 strings together. 将扩展为PATHSAVE(“ JOHN”),因为预处理器会将2个字符串合并在一起。
This will then be further expanded to 然后将其进一步扩展到
func(strcat(strcpy(tmpFileName, appDir), "JOHN"))
You can use the c pre processor if you want to know what is going on. 如果您想知道发生了什么,可以使用c pre处理器。
I pasted your code in a file named ex.c, here is the output of: 我将您的代码粘贴到名为ex.c的文件中,这是以下文件的输出:
cpp ex.c cpp ex.c
# 1 "ex.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "ex.c"
int main() {
func(strcat(strcpy(tmpFileName, appDir), "" "JOHN"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.