Hi,

i have a doubt regarding conditional macros and undef directive.
can i use undef for a function like - macro, by just specifying the name of the macro with out specifying the argument list as written in line 8.


is the code correct with out these statements:

#define OPER(A+B) ((A) * (B))
 #endif

is the below statement correct
#if!defined(OPER)
define OPER(A+B) ((A) * (B))

i found these in one interview.

void Call( void );

#define OPER(A,B) ((A) + (B))

int main()
{
      #undef OPER;
       Call();
       return 0;
}

void Call()
{
    #if!defined(OPER)
    #define OPER(A+B) ((A) * (B))
    #endif
    int res = OPER(10,15);
    printf("res = %d \n", res);
return ;
}

>can i use undef for a function like - macro, by just specifying the
>name of the macro with out specifying the argument list as written in line 8.

Yes, though not exactly like line 8 because preprocessor directives don't end with a semicolon. To answer your specific "doubt", the argument to #undef is a macro identifier:

#undef identifier newline

The function-like macro parameter list is not a part of the identifier:

#define identifier ( parameters ) replacement newline

Likewise, all of the conditional inclusion directives also work on an identifier, so in all of these cases you only need to provide the identifier for a function-like macro, not the full tag.

>is the code correct with out these statements
Clearly not because then you'd have an #if without the matching #endif .

>is the below statement correct
No. OPER(A+B) is a syntax error. It should be OPER(A,B). You also forgot to prefix define with a hash (#), though I'll assume that was a copy/paste mistake.

>i found these in one interview
Out of curiosity, are you summarizing what you remember of the code (because it's broken), or was the code given as a "find the problems" question?

Out of curiosity, are you summarizing what you remember of the code (because it's broken), or was the code given as a "find the problems" question?

i have written what i remember.

but its not "find the problem" type , may be the question itself is wrong.

>may be the question itself is wrong
It's more likely that you remembered it incorrectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.