Menu

#186 OnBC: Preprocessor problems

open
nobody
OnBoard C (71)
5
2008-03-22
2005-06-23
No

From email by Michoel Chaikin:

"I have come across some funny behaviour using ONBC
preproccesor. I would image they count as bugs?

Firstly, consider;

#define DBG(msg) ErrAlertCustom
(0, msg, 0, 0)
#define DisplayMessage(msg) DBG(msg);

Why then does this work:

if(1) DBG("hi");
else DBG("bye");

But this doesn't!

if(1) DisplayMessage("hi");
else DisplayMessage("bye");

Using DisplayMessage("hi") in normal program logic
does not cause anything. As far as I can tell there is no
reason why it shouldn't work, though I don't currently
have access to a normal C Compiler to double-check.

Secondly while this compiles fine;
StrStr(s, "/*");
this wont;
#define foo "/*"
StrStr(s, foo);
"

Discussion

  • Blake Winton

    Blake Winton - 2005-09-11

    Logged In: YES
    user_id=116787

    The first case doesn't work because
    if(1) DisplayMessage("hi");
    else DisplayMessage("bye");
    gets expanded to:
    if(1) DBG("hi");;
    else DBG("bye");;
    (Note the two semi-colons!) thus causing the else to
    no-longer be associated with the if.

    Getting burned by something similar to this in Java is why I
    always use { and } with my if-statements, even if they're
    just single lines.

    The second case may still be a bug, though.

     
  • John Wilund

    John Wilund - 2008-03-22
    • labels: 587913 --> OnBoard C
     
  • John Wilund

    John Wilund - 2008-03-22

    Logged In: YES
    user_id=574706
    Originator: NO

    Since this 2nd case is a BUG I seccond this!

     

Log in to post a comment.