Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the variable values in #define definition

Tags:

c

macros

Here is what I am trying to do.

step1) I want to call a macro with a conditional statement(simple are compounded) like

for eg:

 MACRO1(a==1)
 MACRO1((i!=NULL) && (j>10))

step2) Here is how i am defining this macro

#define MACRO1(condition) \
        if(!(condition)) ??????????????????????????

Here in the definition of the macro, if the condition statement fails. I want to print the variable values so that I will be useful to know the exact reason. I used #condition in the definition, but it just printing the condition, instead of the values of the variables used in the condition. Please help.

like image 586
paddu123 Avatar asked Sep 14 '26 19:09

paddu123


1 Answers

You could do something along these lines:

#define MACRO1(condition, msg) \
    if(!(condition)) { printf msg; }

and use it as follows:

MACRO1(a==1, ("a: %d\n", a))
MACRO1((i != NULL) && (j>10), ("i: %p, j: %d\n", i, j));

The C preprocessor is just a simple substitution engine, without the capability of analyzing the contents of expressions.

like image 130
jlahd Avatar answered Sep 16 '26 23:09

jlahd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!