Better way to read multiple int in C than scanf

17

0

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++

BusyBeingDelicious

Posted 2016-05-15T10:52:49.080

Reputation: 189

4Usually you can submit a function instead of full programs which allows you to take those 4 ints as arguments. – Denker – 2016-05-15T10:55:47.633

I see, thanks for your comment, but in my case, it needs to be a full program. – BusyBeingDelicious – 2016-05-15T10:57:36.673

12

>

  • @BusyBeingDelicious, welcome to PPCG! :) 2) To the close voters, this is not a general programming question. This is a [tag:tips] question which asks for help golfing code which is absolutely on topic.
  • – Martin Ender – 2016-05-15T11:49:19.173

    @MartinBüttner it wasn't a [tag:tips] question when close votes were cast - I just forgot to uncast mine. (I edited in the tips tag) – Blue – 2016-05-15T13:06:40.630

    so those questions can suit a better-fit for the whole network genre, I mean we can say ppcg is a Q&A site now. – Abr001am – 2016-05-15T15:32:48.003

    I'm voting to close this question as off-topic because this question fits better in the meta (help center does not include golfing tips). – Erik the Outgolfer – 2016-05-15T17:19:35.270

    This should be tagged as C, but the mobile app doesn't want me to be able to add that tag. – Mego – 2016-05-15T19:11:56.430

    1

    Possible duplicate of Tips for golfing in C

    – Mego – 2016-05-15T19:14:22.880

    The "Tips for golfing in C" question has multiple answers regarding short ways to read ints from stdin. – Mego – 2016-05-15T19:15:09.877

    @Mego I tagged it as C first, but AlexA removed it. Also it isn't a duplicate, because the topic you provide doesn't provide anything useful for my specific use case (only %*d, that i mentioned here, and gets instead of scanf("%s", str)) – BusyBeingDelicious – 2016-05-15T19:20:34.540

    All in all, it doesn't answer my question, which is "is there a better way to read multiple int in C than scanf", it provides other useful tips that aren't related to my question – BusyBeingDelicious – 2016-05-15T19:22:52.823

    @BusyBeingDelicious There's a tip about using an int array to avoid having multiple ints. That directly applies to your question. – Mego – 2016-05-15T19:42:25.193

    @Mego We don't tag tips questions with their language name, otherwise we'd have a million language tags. – Alex A. – 2016-05-15T19:45:11.427

    @ΈρικΚωνσταντόπουλος Tips questions are on topic on the main site. They are not on topic on meta. – Alex A. – 2016-05-15T19:46:00.753

    @Mego I don't see how it could solve anything to use an array.

    Before: main(a,b,c,d){scanf("%d%d%d%d",&a,&b,&c,&d)} After: a[];main(){scanf("%d%d%d%d",a,&a[1],&a[2],&a[3])}

    It's at least 1 byte longer, and it gets worse with more ints – BusyBeingDelicious – 2016-05-15T19:59:34.540

    @Mego not to mention its limited usefulness get reduced to nothing when you have to use the variables afterwards, for example a[1] everywhere instead of a will make me lose 3 bytes per use. – BusyBeingDelicious – 2016-05-15T20:04:57.680

    I'm not sure it's possible to give a good answer without knowing anything about the problem domain at all. I suspect the real answer is to figure out a way to only read one int at a time and/or abuse pointers. – Comintern – 2016-05-16T03:03:56.580

    @AlexA. I thought they were on meta, since they're not specified in the help center. If they're on topic on the main site, please add or ask to add that too ;) – Erik the Outgolfer – 2016-05-16T05:48:33.683

    @ΈρικΚωνσταντόπουλος The text is already there in the help center. "Non-challenge questions that are related to solving programming puzzles or a particular type of challenge are also on topic." This falls under that category since it's related to golfing a particular task. – Alex A. – 2016-05-16T06:07:14.997

    @AlexA. this might be integrated with C-golfing tips, as a problem posed than best proposal approached by C-golfers in comments to be merged in the answer's core with crediting the owner. – Abr001am – 2016-05-16T06:27:35.820

    @AlexA. Oh well, even though I've alerady retracted that close vote (; – Erik the Outgolfer – 2016-05-16T07:36:25.910

    @BusyBeingDelicious what are the constraints you are imposed or you do impose on us ? range of integers ? libraries used ? declarations of variables which you forgot to precede your piece of golfed code by ? are you considering c++ ? – Abr001am – 2016-05-17T09:03:53.720

    I'll edit once more to add more informations, thanks @Agawa001 – BusyBeingDelicious – 2016-05-19T10:42:04.237

    @BusyBeingDelicious i m not sure what you are doing, the function you defined doesnt retain nothing because the variables stored are volatiles, look any function which does affect on addresses must encompasse pointers not simple integers. – Abr001am – 2016-05-19T11:19:26.227

    I don't really get what you're talking about @Agawa001, the portion I show is using K&R C syntax . If you're talking about the fact that I don't use the variables afterwards, it's because it's not the full full code, just a piece of it. And it's also because the rest of the code isn't relevant to the actual question. If there is an answer, it could solve multiple cases that look like this one.

    – BusyBeingDelicious – 2016-05-20T13:41:58.673

    @BusyBeingDelicious do i seem to talk in cryptic way? i said functions which deal with addresses shouldnt carry simple values amongst their parameters, you should define adresses of type int* between the finction's brackets. – Abr001am – 2016-05-20T13:47:17.677

    3Dear close-voters: This is very clearly on topic. Why are you still trying to get this closed? – James – 2016-05-24T03:38:05.450

    Answers

    2

    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){}
    

    orlp

    Posted 2016-05-15T10:52:49.080

    Reputation: 37 067

    9The OP mentioned in a comment that in this case it needs to be a full program. – Alex A. – 2016-05-17T00:18:33.060