472,795 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,795 software developers and data experts.

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 2429
weaknessforcats
9,208 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

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
26
by: GS | last post by:
I am doing my first real embedded programming project and the supplied device libraries have a function definition that looks like this: void FCN(void) = { INTDEF, INTABS, (unsigned short)...
16
by: danu | last post by:
I have a structure : typedef struct{ char magicNum; int width; int height; int maxGrey; int pixels; } ImageT;
3
by: zhangyue.zl | last post by:
Before I thought C is simple and convient , but now I dont think so.There is really some ugly thing in C.Today I see some macro declaration like this: void va_end (va_list); /* Defined in...
2
by: dis_is_eagle | last post by:
Hi.If I define a macro which contains a variable declaration,then during expansion the declaration will not be placed at the beginning of the program,but somwhere within it.How can I overcome it? ...
2
by: slrj | last post by:
Can someone explain me what's going on in the code below. As per my knowledge "#define <macro name> <Macro replacement>" doesn't have a semicolon and it's terminated by a new line. But the code line...
7
by: krishna | last post by:
What is the need of this syntax for initializing values, isn't this ambiguous to function call? e.g., int func_name(20); this looks like function call (of course not totally as there is no...
36
by: sh.vipin | last post by:
how to make large macro paste the code as it is Problem Explanation '-- For example in the program below /* a.c - starts here */ #define DECL_VARS() \ unsigned int a0;\ unsigned int a1;\...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.