3

If I define a macro taking argument as below.

#define define_int(a) int a;

And provide an argument with space in between like below

define_int(* a)

and get output?

int * a;

Intended use

#define ASSIGN(A,B) B=A;

I want to return from a function directly using this macro as

ASSIGN(A, return B)

so that it would output,

return B = A;
Lundin
  • 195,001
  • 40
  • 254
  • 396
pasha
  • 2,035
  • 20
  • 34
  • `define_int(sd, df)` passes *two* arguments to `define_int`. It expects one. – DeiDei Apr 24 '18 at 08:23
  • The problem isn't the space, it's the *comma*, which separates macro arguments. – Some programmer dude Apr 24 '18 at 08:24
  • 5
    Is that macro an example, or something you intend to use? Because inventing your own mini-language within a language for defining variables, won't make for better code. – StoryTeller - Unslander Monica Apr 24 '18 at 08:26
  • Why do you like to do something like this? Reading `define_int(sd, df)` instead of `int sd, df;` will leave me puzzled. – schorsch312 Apr 24 '18 at 08:37
  • i didn't realize when providing the example, that i was using comma operator, apologies, changed the example please have another look. (i don't intend to use as it is) – pasha Apr 24 '18 at 10:54
  • Your example has nothing to do with the initial question. That's assignment, not a declaration. Kindly stop radically changing the question over and over. – Lundin Apr 24 '18 at 11:06
  • @Lundin That's the same feed back I've got from Bathsheba. I'm gonna revert all the changes and provide my actual query in a new question – pasha Apr 24 '18 at 11:09
  • @pasha But now I edited my answer to answer both of your questions. Gah! I will rollback. – Lundin Apr 24 '18 at 11:13

1 Answers1

6

The space itself does nothing, since spaces are used to separate pre-processor tokens and the macro is pre-processed after that's done. So you can add as many spaces as you like.

The only issue is that you can't violate the syntax, so this classic mistake won't compile:

#define A (B, C) ...  // bad syntax, space between macro name and parameter list

#define A(B, C) ... // ok

So yes you can write something obscure like

#define define_int(a) int a;
...
define_int(*const a);

or

define_int(*        const     a     );

and get the type int*const a;.

Why you would ever want to write such horrible code, I have no idea. Inventing your own secret macro language in C is one of them royally stupid things.


Regarding the edit of inventing something like ASSIGN(A, return B) for a return from function macro with some clean-up code, different flavours of such macros are both commonly-used and naive.

The proper way to centralize clean-up in a function when you need to return from multiple places is this (pseudo code):

void wrapper (void)
{
  something a = allocate_stuff();
  error result = the_actual_function(a);

  if(result == whatever)
    do_that();

  cleanup(a);
}

inline error the_actual_function (something a)
{
  ...
  if(bad)
  {
    return error_42;
  }
}

This design pattern reduces clutter inside the actual function, when you only need to return upon error. It also gets rid of "BASIC style" on error goto, which is another rather naive way of writing error handlers.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Perhaps paste up the C standard in case the question moves again? Have an upvote. – Bathsheba Apr 24 '18 at 11:15
  • @Bathsheba Well hopefully the OP now understands why radically changing the question is a bad idea around here :) – Lundin Apr 24 '18 at 11:16
  • I never thought the comma operator would be the death of my SO rep – pasha Apr 24 '18 at 11:50
  • @Lundnin, thnx 4 answer (spaces & macros). Now, I wonder if it really is horrible code and stupid things. Ex.: {#define OUTPUT(x) out x ; out.flush();} and the parameter x corresponds to a long sequence, var << "," << var2 etc., to generate a CSV. The macro works ok. Regarding 'on error goto', you broke a function into 2 parts to solve that. That's the way you conceive the logic; it's different from other programmers; could've used try-except, even c++ templates (check Jeremy Cochoy). I see choice, not stupidity or, really, naivete. But, hey! Thanks again. Peace everybody. – A Koscianski Mar 27 '21 at 14:05