Macro to replace comma in #define

I'm trying to do some C macro experiment where comma "," is replaced with a keyword like ADD

Example:

#define ADD ,

int Sum(int a, int b, int c) {
return a+b+c;
}

so I can do this:

Sum(1 ADD 2 ADD 3);

It works!

But defining the function in a macro, say:

#define sum( a,b,c ) ((a) + (b)+(c))

and so..

sum(1 ADD 2 ADD 3)

Does not work. It says:

macro 'add' used with too few args
Last edited on
My understanding of sum(1 ADD 2 ADD 3) is that 1 ADD 2 ADD 3 would be treated as one argument.

If sum was defined as #define sum(x) foo(x) then it would first expand to foo(1 ADD 2 ADD 3).

Then it would rescan and replace ADD with , which would give you foo(1 , 2 , 3).
Last edited on
@Peter87

No, I don't think that works it should be 3 arguments in the 1st place.

Last edited on
If you have a macro/function called SUM, isn't ADD implied? So why bother with it. It may be a better use of your time to come up with a more suitable SUM thing, if you're not satisfied with a simple function call.

Remember in C++, with variadic templates, it's possible to create a sum function that takes a variable number of arguments.
Last edited on
yes, Sum works with the #define ADD ,

but that's not the point. I need to use that define with another #define, particularly a #define function or shortcut macro.

I just laid it out for comparison that the define ADD works, but why not on another define.
If you're using MS VS compiler, then there is the compile option /P which will provide the source code after the macro expansions have taken place so that you can see what the macros are doing
https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-alphabetically?view=msvc-170

Other compilers may have other ways of doing this.

@seeplus

yes in mingw or gcc it is the -E flag.

The problem with that is if there's an error, it does not show how it expands only if it builds ok, or is there another option?

This one works, but not the actual calling a define function as I originally stated..

#define add( aa,bb,cc ) Sum(aa ADD bb ADD cc)


This should work, but still errors "macro 'add' used with too few args"

add(1 ADD 2 ADD 3)


I don't understand why I can't use it in another define in this case if it just expands.

Somehow the define is being ignored.
Last edited on
ruzip wrote:
No, I don't think that works it should be 3 arguments in the 1st place.

I'm not suggesting a solution to the problem. I'm just trying to explain why your code doesn't work. I could be wrong, but it's how I understand it when reading the standard and it seems to be consistent with how the compiler behaves.

https://eel.is/c++draft/cpp.replace#general-13
The individual arguments within the list are separated by comma preprocessing tokens

In sum(1 ADD 2 ADD 3) there are no commas so I think this means everything between the parentheses is treated as one single argument.

This why you get an error because sum expects three arguments, not just one.

https://eel.is/c++draft/cpp.replace#cpp.rescan-1
After all parameters in the replacement list have been substituted [...] Then the resulting preprocessing token sequence is rescanned [...] for more macro names to replace.

The way I interpret this is that sum will essentially get expanded first and not until after that has succeeded (which it doesn't in your code) it will go on and expand ADD in the resulting sequence.
Last edited on
I'm trying to do some C macro experiment where comma "," is replaced with a keyword like ADD

Why? The comma operator has clearly defined rules of usage in C and C++. ADD just obscures the intent IMO.

There are two versions of logical operators, symbols and text. && AND, || OR and ! NOT. The text operators are very clear with their usage and meaning. ADD is IMO at best ambiguous, with an implication of possibly adding together two values.

I certainly would never try to do something like this, I'd have a macro replace doing different duty that enhances readability. "Magic number" replacement, for example.

https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad

If you want to do some experimentation on macro replacement a suggestion is at least make the operator replacement closely match. , and COM or COMM or even COMMA.
kbw wrote:
Remember in C++, with variadic templates, it's possible to create a sum function that takes a variable number of arguments.

The OP wants only C solutions, though offering C++ ideas in other threads has been grudgingly accepted.

@ruzip, variadic parameter arguments are a part of C as well.

https://en.cppreference.com/w/c/language/variadic

Not as versatile in C compared to what can be done in C++, IMO. Templates really expand what's possible. YMMV.
1
2
3
4
5
#define SUM(a) SUM_(a)
#define SUM_(a, b, c) ((a) + (b) + (c))
#define COMMA ,

static_assert(6 == SUM(1 COMMA 2 COMMA 3));
Last edited on
Thanks mbozzi, it works great! problem solved 👍

I did try something like this similar earlier..

function_name(a,b,c)((a) + (b) + (c))


@GeorgeP

..just an experiment yes and the COMMA would be better.

@Peter87

Yes I might also have missed something as mbozzi solved this one.

Thanks guys.
Last edited on
Topic archived. No new replies allowed.