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

3 Answers 3

4
\$\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\$ -Df(n)=xxx and -Df(n)xxx go different, though the latter can be used to save more bytes in this problem \$\endgroup\$
    – l4m2
    Commented Dec 20, 2017 at 18:32
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\$
2
  • \$\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
    Commented Dec 19, 2017 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
    Commented Dec 20, 2017 at 13:09
0
\$\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\$
4
  • 3
    \$\begingroup\$ Allowing macro not protecting is another topic. I guess the requirement is that inputting pure variable work \$\endgroup\$
    – l4m2
    Commented Dec 20, 2017 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\$ Commented Dec 20, 2017 at 20:56
  • 1
    \$\begingroup\$ Wrong topic. It's related to What's a function \$\endgroup\$
    – l4m2
    Commented Dec 20, 2017 at 23:16
  • 1
    \$\begingroup\$ Whether or not the macro provided works as intended is completely ancillary to the question. \$\endgroup\$
    – user45941
    Commented Dec 27, 2017 at 2:05

You must log in to answer this question.

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