473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 2449
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;\...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.