1
#include<conio.h>
#include <stdio.h>

#define small 0
#define big   1

#define dummy( _x_ )    \
( small > big ) ? ( printf _x_ ) : ( void( 0 ) )

int main() {  

    dummy( ( "Four is %d", 4 ) );
    getch();
    return 0; 
}

When I compiled above program in gcc, it is giving the error below:

error : expected ')' before numeric constant.

I am not understanding why I am getting it? To me it seems everything is correct. Please help.

1
  • i don't think that will be the case, x is something i will provide in program using dummy(x) ...so in this x is ( "Four is %d", 4 ) which is perfectly valid "printf" format.. Commented Aug 7, 2014 at 9:05

4 Answers 4

2

The void(0) part is a syntax error. You try to call a function named void with a 0 argument. This will work:

( ( small > big ) ? printf _x_ : (void) 0 )
Sign up to request clarification or add additional context in comments.

Comments

2

The problem seems to come from your void(0). I can't even compile it as a valid C expression/statement.

int main()
{
    void(0);
    return 0;
}

Gives:

error: expected identifier or ‘(’ before numeric constant

Just replace void(0) with 0. Ternary operator alternatives are supposed to have the same type anyway.

1 Comment

true, is C++, not valid in C, +1
2

Jens gives you the solution, but seems that you need (void)printf in order to skip warning:

warning: ISO C forbids conditional expr with only one void side [-pedantic]

This works without warnings:

#define dummy( _x_ ) \
       (small > big) ? (void)printf _x_ : (void)0

On the other hand dummy( ( "Four is %d", 4 ) ); looks ugly to me, I suggest to use __VA_ARGS__ and skip double parenthesis in your function call:

#include <stdio.h>

#define small 0
#define big   1

#define dummy(...) \
     (small > big) ? (void)printf(__VA_ARGS__) : (void)0

int main(void)
{  
    dummy("Four is %d", 4);
    return 0; 
}

Or don't pass params:

#include <stdio.h>

#define small 0
#define big   1

#define dummy (!(small > big)) ? (void)0 : (void)printf

int main(void)
{  
    dummy("Four is %d", 4);
    return 0; 
}

Comments

0

After the macro replacements your code changed like this-

int main() {

( 0 > 1 ) ? ( printf ( "Four is %d", 4 ) ) : ( void( 0 ) );
return 0;
}

Here the compiler doesn't know what is void( 0 ). It is not a valid syntax in C. So try to replace it by some other statement.

Try the following code

#define dummy( _x_ )    \
( small > big ) ? ( printf _x_ ) : (printf("Conditon fails!\n"))

or

#define dummy( _x_ )    \
( small > big ) ? ( printf _x_ ) : ((void) 0)

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.