# Can a C submission be in the form of a macro defined by command line argument?

In this question, the current C solution is

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


Obviously this command line is shorter by 2 bytes:

-Df(n)=n?:1


But I've never seen anyone do that.

Is it allowed but not found, or not allowed? In the latter case, why?

• I see others treat -x as +1B but modifyer treat it as 2?
– l4m2
Dec 17, 2017 at 10:13
• Depends on how it is passed to the compiler. (how longer is it in comparison with normal compilation) Dec 17, 2017 at 15:40
• It may be an answer, but it's not a solution: it doesn't meet spec. Dec 17, 2017 at 17:00
• +Peter What spec?
– l4m2
Dec 17, 2017 at 17:02
• > Given a non-negative integer n, output 1 if n is 0, and output the value of n otherwise. Dec 17, 2017 at 21:17
• that's what f does
– l4m2
Dec 17, 2017 at 21:35
• Perhaps it's a typo for n?n:1 Dec 18, 2017 at 10:16
• @TobySpeight It's a GCC extension.
– Neil
Dec 18, 2017 at 11:20
• I miss the return value and the other place in ?: Example: f(n){return n?0:1;} or f(n){return!n;} or in a macro #define f(n) !n
– user58988
Dec 18, 2017 at 13:13
• A mix of command line define and file inner is also possible
– l4m2
Dec 19, 2017 at 5:51
• Very relevant
– user45941
Dec 19, 2017 at 15:38

# Yes

Passing -Df(n)!n+n to the compiler is exactly equivalent to adding #define f(n)!n+n in the code. So there is no reason to allow one and disallow the other.

Apparently no one wrote an answer...

For those who are not familiar with the language C:

The "accepted" solution, use #define, add 16 bytes (17 if the newline is counted) to the file to define the function:

#define f(n)!n+n
int main() {
printf("%d\n%d", f(0), f(5));
}


and the command to compile it: (assume the file is stored as a.c)

gcc a.c


(which will produce an executable named a. It's possible to specify executable name with -o, but it's not important here)

where the proposed solution would add 0 byte to the file:

int main() {
printf("%d\n%d", f(0), f(5));
}


but make the compilation command 11 bytes longer:

gcc -Df(n)!n+n a.c

• -Df(n)=xxx and -Df(n)xxx go different, though the latter can be used to save more bytes in this problem
– l4m2
Dec 20, 2017 at 18:32

# Yes, but ...

There are conflicting accounts on how to score command line arguments and flags however, everyone seems to agree (for the most part) that the use of any and all command line flags is allowed. So yes, you can use flags to set C macros.

The but part of this answer is scoring. Traditionally scoring command line flags has been a muddy ordeal, there was a meta answer which was interpreted in different ways by different people and lead to a lot of confusion. Under that method you would probably have to add either 10 or 11 bytes to your score for the flag.

Recently there has been a push to stop scoring command line flags altogether. Different flags are different invocations of the interpreter and thus are different languages. This is nice for a couple of reasons, the main one is it is completely unambiguous. Mego's post here and my question here outline the reasons for this in better detail. Under this system you would gain 0 bytes for using the flag, but would have to mark it as a separate language.

This is basically the same as saying don't do that. There is not much sense in making that answer because, since most competitions are language categorized, you are only competing against yourself.

• In this case obviously you shouldn't treat them as different languages. It's not the problem that "you are competing against yourself" for there's already so many case where a language is used for only few times.
– l4m2
Dec 19, 2017 at 18:09
• You can always count that being the same language, like how Jelly and Python can get code by command line flag. Dec 20, 2017 at 13:09

I'm still not convinced that this works: the TIO link for the answer you refer to shows that it is buggy. But that's really an aside. To address the real question:

### For a macro to be equivalent to a function, it needs a lot of parentheses.

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.

• Allowing macro not protecting is another topic. I guess the requirement is that inputting pure variable work
– l4m2
Dec 20, 2017 at 10:53
• Only if there's explicit support for that either in the question or in codegolf.meta.stackexchange.com/q/2447/194 Dec 20, 2017 at 20:56
• Wrong topic. It's related to What's a function
– l4m2
Dec 20, 2017 at 23:16
• Whether or not the macro provided works as intended is completely ancillary to the question.
– user45941
Dec 27, 2017 at 2:05