本文介绍了做功能像宏需要一个强制的括号?引用GCC cpp手册后,我感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是令我迷惑的: 我相信这是为了让代码脱颖而出,该计划的作者。像其他宏规则的CAPS规则一样。但是,以下是我感到困惑的地方: 我在阅读后立即不同意。并且 gcc -E 验证了在以下代码中: #define FUNC display() void display() { printf(Display\\\); } int main() { FUNC; 返回0; } 预处理输出显示 main() 函数: int main() { display(); 返回0; } 那么我在这里错过了什么?预处理器用于标记源,宏扩展是一个标记,上面的代码是以这种方式处理的,预处理器不应该检查任何内容或验证任何内容,它只会转储令牌。在那种情况下,gcc手册试图传达什么内容。 我正在学习 C 编程,所以我可能会误解它它经常发生,我寻找一个适当的解释,并最终在这里问。 解决方案当您定义时: #define FUNC display() FUNC 不是一个类似于函数的宏;它是一个类似于对象的宏,扩展为函数调用。 类似宏的宏看起来像: #define FUNC()display() 必须写 FUNC()来调用它。或者,更频繁地,它会有参数: #define MIN(x,y)((x)>(y )?(x):(y)) 可用以下方式调用: int min = MIN(sin(p),cos(q)); 注意参数展开的次数。 另请参阅 getc()作为宏和C标准库函数定义。它包括标准的解释,为什么重要的是没有下面的左括号的函数式宏的简单名称是 not 展开的,这正是GCC手册中引用的内容。 / p> 当定义一个类似函数的宏时,左括号必须触及宏名称: $ b $ #define function_like(a)... #define object_like b 因为在 object_like 之后有一个空格,所以左括号是替换文本的一部分,而不是参数列表的开头。当调用类似函数的宏时,宏名称和参数列表之间可能会有空格: $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $函数样式(x) //有效调用function_like宏。 但是,如果您写道: int(function_like) } 这不是函数式宏的调用,因为 function_like 不是左括号。 Here is what confuses me:I believe this is to make the code stand out for people other than the author of the program. Like other rules of CAPS for macro names. But the following is where I get confused:I disagreed instantly after reading it. And gcc -E verified that in the following code#define FUNC display()void display() { printf("Display\n"); }int main() { FUNC; return 0; }The pre-processed output shows the content of the main() function as expected:int main() { display(); return 0; }So what am I missing here? The pre-processor is for tokenizing the source, the macro expansion is a token and the above code was processed that way, the pre-processor isn't supposed to check anything or verify anything, it just dumps tokens. In that case what is the gcc manual trying to convey.I am learning C programming, so I might be misunderstanding it a great deal as it frequently happens, I searched for a proper explanation and finally resorted to asking here. Please help me with this. 解决方案 When you define:#define FUNC display()FUNC is not a function-like macro; it is an object-like macro that expands to a function call.A function-like macro looks like:#define FUNC() display()Now you must write FUNC() to invoke it. Or, more frequently, it will have arguments:#define MIN(x, y) ((x) > (y) ? (x) : (y))and that can be invoked with:int min = MIN(sin(p), cos(q));with cautions about the number of times the arguments are expanded.See also getc() as macro and C standard library function definition. It includes the standard's explanation of why it is important that the simple name of a function-like macro without a following open parenthesis is not expanded, which is what the quote from the GCC manual is telling you.When a function-like macro is defined, the open parenthesis must 'touch' the macro name:#define function_like(a) …#define object_like (…)Because there's a space after object_like, the open parenthesis is part of the replacement text, not the start of an argument list. When the function-like macro is invoked, there may be spaces between the macro name and the argument list:function_like (x) // Valid invocation of function_like macro.However, if you wrote:int (function_like)(double a) { return asin(a) + 2 * atanh(a); }this is not an invocation of the function-like macro because the token after function_like is not an open parenthesis. 这篇关于做功能像宏需要一个强制的括号?引用GCC cpp手册后,我感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 11:48