Jump to content

Recommended Posts

Hello,

I am extensively using the preprocessor to conditionally compile my source code. I've attempted to use the following code ( from: Sam's Teach Yourself C in 24 Hours http://aelinik.free.fr/c/ch23.htm )

 

 

In the following example:

 

#if MACRO_NAME1 || MACRO_NAME2

printf("MACRO_NAME1 or MACRO_NAME2 is defined.\n");

#else

printf("MACRO_NAME1 and MACRO_NAME2 are not defined.\n");

#endif

 

the logical operator || is used, along with MACRO_NAME1 and MACRO_NAME2 in the expression evaluated by the #if directive. If one of the macro names, MACRO_NAME1 or MACRO_NAME2, has been defined, the expression returns a nonzero value; otherwise, the expression returns 0.

 

 

 

My code:

#if ENABLE1 && (ENABLE2 || ENABLE3) // line 732

................... code

................... code

#endif

 

 

The following errors are returned:

 

<FILE>.c(732): Expression: Expected operand: )

<FILE>c(732): Expression: Mismatched "()"

 

2 errors detected

Error: preprocessing error

 

I think I'm coding things correctly. If not I'd very much appreciate getting straitened out. If this is a compiler issue, I'd like to understand how to code around it.

 

Thanks in advance for your kind help,

d

Share this post


Link to post
Share on other sites

All defines used in expression have to have some values. Code below compiles:

 

#define ENABLE1 1
#define ENABLE2 0
#define ENABLE3 1

#if ENABLE1 && (ENABLE2 || ENABLE3) 
#pragma message("Test")
#endif

 

But if any of ENABLEx defines is empty you will get compile errors.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

#ifdef xxxx

 

checks to see if xxx has been defined

 

 

else you can do

 

#if defined(xxx)|defined(yyy)

 

The latter one also allows you to do complex if's depedning upon whats defined or not :unsure:

 

 

Andrew

 

 

 

All defines used in expression have to have some values. Code below compiles:

 

#define ENABLE1 1
#define ENABLE2 0
#define ENABLE3 1

#if ENABLE1 && (ENABLE2 || ENABLE3) 
#pragma message("Test")
#endif

 

But if any of ENABLEx defines is empty you will get compile errors.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...