17
\$\begingroup\$

I'm trying to read 4 ints in C in a golfing challenge and I'm bothered by the length of the code that I need to solve it:

scanf("%d%d%d%d",&w,&x,&y,&z)

that's 29 chars, which is huge considering that my total code size is 101 chars. I can rid of the first int since I don't really need it, so I get this code:

scanf("%*d%d%d%d",&x,&y,&z)

which is 27 chars, but it's still lengthy.

So my question is, is there any other way (tricks, functions, K&R stuff) to read ints that I don't know of that could help me reduce this bit of code?

Thanks a lot.

EDIT: some users have reported that my question is similar to Tips for golfing in C

While this topic contain a lot of useful informations to shorten C codes, it isn't relevant to my actual use case since it doesn't provide a better way to read inputs.

I don't know if there is actually a better way than scanf to read multiple integers (that's why I'm asking the question in the first place), but if there is, I think my question is relevant and is sufficiently different than global tips and tricks.

If there is no better way, my question can still be useful in the near future if someone find a better solution.

EDIT2: I'm looking for a full program (so no function trick) and all librairies possible. It needs to be C, not C++. Currently, my whole program looks like this:

main(w,x,y,z){scanf("%*d%d%d%d",&x,&y,&z)}

any tricks is welcome, as long as it shorten the code (this is code golf :) ) and as long as it isn't c++

\$\endgroup\$
  • 4
    \$\begingroup\$ Usually you can submit a function instead of full programs which allows you to take those 4 ints as arguments. \$\endgroup\$ – Denker May 15 '16 at 10:55
  • \$\begingroup\$ I see, thanks for your comment, but in my case, it needs to be a full program. \$\endgroup\$ – BusyBeingDelicious May 15 '16 at 10:57
  • 12
    \$\begingroup\$ 1) @BusyBeingDelicious, welcome to PPCG! :) 2) To the close voters, this is not a general programming question. This is a tips question which asks for help golfing code which is absolutely on topic. \$\endgroup\$ – Martin Ender May 15 '16 at 11:49
  • 1
    \$\begingroup\$ Possible duplicate of Tips for golfing in C \$\endgroup\$ – Mego May 15 '16 at 19:14
  • 3
    \$\begingroup\$ Dear close-voters: This is very clearly on topic. Why are you still trying to get this closed? \$\endgroup\$ – DJMcMayhem May 24 '16 at 3:38
2
\$\begingroup\$

You can write a function instead of a program as your submission. In C you can omit int from function signatures:

f(w,x,y,z){}
\$\endgroup\$
  • 9
    \$\begingroup\$ The OP mentioned in a comment that in this case it needs to be a full program. \$\endgroup\$ – Alex A. May 17 '16 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.