简体   繁体   中英

C preprocessor: macro function to call printf()

I want to conditionally use either printf() or a statement:

#define USE_PRINTF

#ifdef USE_PRINTF
#define macrofn(str) printf(str)
#else
#define macrofn(str) some_statement
#ifndef USE_PRINTF

But I'm getting the following error:

incompatible implicit declaration of built-in function 'printf'

What am I doing wrong? Thanks

You don't necessarily have to include the <stdio.h> before the macro definition. What you really need is #endif for the #if you have started. For example, the following programme will work all fine:

#define USE

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

#include<stdio.h>

int main( ) {
    asd;
    getchar( );
    return 0;
}

So... yeah.

You need to add #include <stdio.h> to your file.

Take a look here for more information about this error message.

如果要使用printf则需要包括stdio.h

You should use this syntax:

#include <stdio.h>

#define USE_PRINTF

#ifdef USE_PRINTF
#define macrofn(str) printf(str)
#else
#define macrofn(str) some_statement
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM