0

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

9
  • 3
    Please post the code which is calling on the macro. Commented Mar 10, 2014 at 8:49
  • 4
    Not adding #include <stdio.h>? Commented Mar 10, 2014 at 8:49
  • 3
    This is not correct... You should have been using an #endif instead of the #ifndef USE_PRINTF which means "if USE_PRINTF is not defined", which already is being handled by the #else part. You absolutely need an #endif for each #if. Commented Mar 10, 2014 at 8:53
  • @ThoAppelsin: Nice catch!! But won't compiler complain as no matching endif for this error. Commented Mar 10, 2014 at 8:55
  • 1
    What's the purpose of using printf with just one argument? Shouldn't puts be more suitable in most such cases? See also stackoverflow.com/a/16813480/908515 Commented Mar 10, 2014 at 9:05

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

True, but it is common sense to have the includes on top. And you need it before the invocation of the macro.
@undur_gongor I also wouldn't put things in this order, but I wouldn't mind a bit if someone did, that's what being flexible is.
3

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

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

2 Comments

This is wrong, if it means what it looks like it means. One does have to #include <stdio.h>, but not necessarily before the macro definition.
Force of a habit. Thanks!
2

You need to include stdio.h if you want to use printf.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.