468,272 Members | 2,048 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Syntax of C Macro Declaration

kreagan
153 100+
Hi everyone,

I found this in macro and wanted clearification of the syntax. In other words, I understand what it does but do not understand why it is written this way. Just so you understand, when a variable called status is assigned a value less than zero, that function goes to a catch statement instead of completing the rest of the function.

Expand|Select|Wrap|Line Numbers
  1. //if an error occurs then jump to the label
  2. //catch to perform the clean up operation
  3. #define OnErrDoAction(X) status = (X);      \
  4.     if(status < 0)                               \
  5.     {                                                \
  6.         goto CATCH;                               \
  7.     }
1.) Why isn't this written like a typical function? Functionname( params ) { code } The syntax seems wierd.

2.) Why are there slashes after each line?

3.) Would #define OnErrDoAction(status);
if(status < 0){....... }
work?
Aug 30 '07 #1
2 2259
weaknessforcats
9,207 Expert Mod 8TB
Look at how it is expanded:

In your code: OnErrDoAction(25);

In the expanded code:

Expand|Select|Wrap|Line Numbers
  1.  status = 25;
  2.  if(status < 0)
  3.    goto CATCH;   
  4. }
  5.  
The backslashes are line continuation characters.

Probably this is a debug-only macro. Later, when the program is released the macro might become:

#define OnErrDoAction(X)

so that nothing is expanded inthe code sent ot the compiler.

Overall, this macro is really bad. Especially, that goto stuck in there and the fact that CATCH may also be another macro.
Sep 3 '07 #2
JosAH
11,448 Expert 8TB
Hi everyone,

I found this in macro and wanted clearification of the syntax. In other words, I understand what it does but do not understand why it is written this way. Just so you understand, when a variable called status is assigned a value less than zero, that function goes to a catch statement instead of completing the rest of the function.

Expand|Select|Wrap|Line Numbers
  1. //if an error occurs then jump to the label
  2. //catch to perform the clean up operation
  3. #define OnErrDoAction(X) status = (X);      \
  4.     if(status < 0)                               \
  5.     {                                                \
  6.         goto CATCH;                               \
  7.     }
1.) Why isn't this written like a typical function? Functionname( params ) { code } The syntax seems wierd.

2.) Why are there slashes after each line?

3.) Would #define OnErrDoAction(status);
if(status < 0){....... }
work?
That macro is so incredibly is broken; consider these (sick) examples:

Expand|Select|Wrap|Line Numbers
  1. if (<some_condition>)
  2.    OnErrDoAction(42);
  3. else
  4.    printf("everything's fine here\n");
  5.  
It looks quite reasonable but wait 'till the preprecessor has mutilated that beyond
recognition.

And this one:

Expand|Select|Wrap|Line Numbers
  1. OnErrDoAction(42) else printf("neener, neener, neener\n");
  2.  
It expands to something one wouldn't expect given the un-preprocessed code.

kind regards,

Jos
Sep 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

699 posts views Thread by mike420 | last post: by
8 posts views Thread by Rich Grise | last post: by
26 posts views Thread by GS | last post: by
16 posts views Thread by danu | last post: by
3 posts views Thread by zhangyue.zl | last post: by
2 posts views Thread by dis_is_eagle | last post: by
36 posts views Thread by sh.vipin | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.