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

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Jodes
  • 14,118
  • 26
  • 97
  • 156
  • 3
    Please post the code which is calling on the macro. – Lundin Mar 10 '14 at 08:49
  • 4
    Not adding `#include `? – Ry- Mar 10 '14 at 08: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`. – Utkan Gezer Mar 10 '14 at 08:53
  • @ThoAppelsin: Nice catch!! But won't compiler complain as no matching `endif` for this error. – 0xF1 Mar 10 '14 at 08:55
  • Thank you all - typo galore - it's clearly too early in the morning – Jodes Mar 10 '14 at 08:57
  • 1
    What's the purpose of using `printf` with just one argument? Shouldn't `puts` be more suitable in most such cases? See also http://stackoverflow.com/a/16813480/908515 – undur_gongor Mar 10 '14 at 09:05
  • @undur_gongor `puts` prints an additional new line when it reaches the end of the string pushed into. So they aren't truly analogous even for the cases where `printf` has only one argument. Maybe `#define macrofn(str) for ( int i = 0; str[i]; i++ ) putchar( str[i] );` – Utkan Gezer Mar 10 '14 at 12:45
  • @ThoAppelsin: I'm aware about that. But even if you want to avoid the line break, you should rather define the macro as `printf("%s", str)`. – undur_gongor Mar 10 '14 at 13:49
  • @undur_gongor How about `#define macrofn( str, ... ) printf( str, __VA_ARGS__ )` then? – Utkan Gezer Mar 10 '14 at 14:27

4 Answers4

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.

Utkan Gezer
  • 3,009
  • 2
  • 16
  • 29
  • True, but it is common sense to have the includes on top. And you need it before the invocation of the macro. – undur_gongor Mar 10 '14 at 09:00
  • 1
    @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. – Utkan Gezer Mar 10 '14 at 09:02
3

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

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

Community
  • 1
  • 1
Benesh
  • 3,398
  • 1
  • 18
  • 38
2

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

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
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
developer
  • 4,744
  • 7
  • 40
  • 55