定义printf的宏时__LINE__宏不起作用

时间:2016-09-27 09:58:21

标签: c++ macros printf

LINE 宏会给出随机数, FILE 宏会在" printf"用另一个宏定义。在下面的代码中,我展示了用于" printf"的宏。和函数(这是一个类的方法),使用宏:

#define OTConsolePrint(x) printf(x)

...

void parseArray(float* arr)
{
    if (arr == NULL)
    {
        printf("Line: %d\n", __LINE__);
        OTConsolePrint("Null pointer at \nLine: %d\nFile: %s\n", __LINE__, __FILE__);
    }
}

给了我:

Line: 39
Null pointer at
Line: 1964696
File: (null)

2 个答案:

答案 0 :(得分:2)

#define OTConsolePrint(x, y, z) printf(x, y, z)

当你在函数中调用宏时,你会传递三个元素。

答案 1 :(得分:1)

您的" 宏功能"只需要一个参数:

any_value()

您可以使用多个参数调用它:

#define OTConsolePrint(x) printf(x)

实际上我甚至认为它不会编译。

无论如何,为了获得 variadic 参数,你可以这样做:

OTConsolePrint("Null pointer at \nLine: %d\nFile: %s\n", __LINE__, __FILE__);

works符合预期。

相关问题