Placeholder
Placeholder

Reputation: 3

syntax error when using #ifdef in c

I'm trying to run the following code with and without the '-D DEBUG' gcc flag:

#include <stdio.h>

#ifdef DEBUG
    printf("Defined");
#else
    printf("Not defined");
#endif

int main() 
{
}

The error I get is "debugtest.c:6:9: error: expected declaration specifiers or ‘...’ before string constant"

Upvotes: 0

Views: 505

Answers (1)

Carl Norum
Carl Norum

Reputation: 224844

Your call to printf has to be inside a function:

#include <stdio.h>

int main() 
{
#ifdef DEBUG
    printf("Defined");
#else
    printf("Not defined");
#endif
    return 0;    
}

Upvotes: 3

Related Questions