13
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ I see others treat -x as +1B but modifyer treat it as 2? \$\endgroup\$ – l4m2 Dec 17 '17 at 10:13
  • \$\begingroup\$ Depends on how it is passed to the compiler. (how longer is it in comparison with normal compilation) \$\endgroup\$ – user202729 Dec 17 '17 at 15:40
  • \$\begingroup\$ It may be an answer, but it's not a solution: it doesn't meet spec. \$\endgroup\$ – Peter Taylor Dec 17 '17 at 17:00
  • \$\begingroup\$ +Peter What spec? \$\endgroup\$ – l4m2 Dec 17 '17 at 17:02
  • \$\begingroup\$ > Given a non-negative integer n, output 1 if n is 0, and output the value of n otherwise. \$\endgroup\$ – Peter Taylor Dec 17 '17 at 21:17
  • 4
    \$\begingroup\$ that's what f does \$\endgroup\$ – l4m2 Dec 17 '17 at 21:35
  • \$\begingroup\$ Perhaps it's a typo for n?n:1 \$\endgroup\$ – Toby Speight Dec 18 '17 at 10:16
  • 1
    \$\begingroup\$ @TobySpeight It's a GCC extension. \$\endgroup\$ – Neil Dec 18 '17 at 11:20
  • \$\begingroup\$ 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 \$\endgroup\$ – RosLuP Dec 18 '17 at 13:13
  • \$\begingroup\$ A mix of command line define and file inner is also possible \$\endgroup\$ – l4m2 Dec 19 '17 at 5:51
  • 1
    \$\begingroup\$ Very relevant \$\endgroup\$ – Mego Dec 19 '17 at 15:38
6
\$\begingroup\$

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
\$\endgroup\$
-1
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – l4m2 Dec 19 '17 at 18:09
  • \$\begingroup\$ You can always count that being the same language, like how Jelly and Python can get code by command line flag. \$\endgroup\$ – user202729 Dec 20 '17 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .