C预处理器:调用printf()的宏函数

CL2*_*L22 0 c macros c-preprocessor

我想有条件地使用printf()或声明:

#define USE_PRINTF

#ifdef USE_PRINTF
#define macrofn(str) printf(str)
#else
#define macrofn(str) some_statement
#ifndef USE_PRINTF
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

incompatible implicit declaration of built-in function 'printf'
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?谢谢

Tho*_*sin 5

您不一定要<stdio.h>在宏定义之前包含它.你真正需要的是#endif#if你已经开始.例如,以下程序将全部正常工作:

#define USE

#ifdef USE
#define asd printf("asd")
#else
#define asd puts("kek")
#endif

#include<stdio.h>

int main( ) {
    asd;
    getchar( );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

是的.