Skip to main content
2 of 5
small fixes
Wheat Wizard Mod
  • 100k
  • 1
  • 41
  • 84

How should C macro be scored?

Sorry if this is a duplicate...


Recently, an answer of Peter Taylor here says:

To get a macro equivalent to

f(n){n=n?:1;}

you need

-Df(n)=((n)?:1)

which is longer, so there's no point.

So how should C macros be scored? Which of the following criteria should be satisfied:

  • Arguments must be put in parentheses.
  • The function itself must be wrapped in parentheses (unless it's single token).
  • The argument must be referenced exactly once.
  • There should be a trailing newline (if use #define) (because you can't put the code right after the macro, you need a trailing newline).
  • If there are multiple statements there should be if(1){...}else wrap or do{...}while(0) wrap.

Real world macros satisfy all three of them (except some nonstandard ones (never used in production code), like

#define all(x) begin(x), end(x)

macro, then you can do something like

sort(all(x))

)


Should that be enforced?

  • Agree: That's the standard of C macro.
  • Agree: That makes the macro more like a real "function".
  • Disagree: That may change a lot of existing submission.
  • Disagree: Even without that condition, we can call it with variables, and wrap the return value in parentheses. This is "require more input than necessary".
  • Disagree: That takes more bytes. (← should not be a problem)
user202729
  • 17.4k
  • 12
  • 17