0

so i was going to run a function in an infinite loop which takes a number input, but then I remembered I codn't do

while (true) {
    myfunc(scanf("%d));
}

because I need to put the scanf input into a variable. I can't do scanf(%*d) because that doesn't return value at all. I don't want to have to do

int temp;
while (true) {
    scanf("%d", &temp);
    myfunc(temp);

or include more libraries. Is there any standard single function like gets (I cod do myfunc((int) strtol(gets(), (char**) NULL, 10)); but its kinda messy sooo yea)

srry if im asking too much or being pedantic and i shod do ^

btw unrelated question is there any way to declare a string as an int--- or even better, a single function for converting int to string? I usually use

//num is some number
char* str = (char*) malloc(12);
sprintf(str, "%d", num);
func(str);

but wodnt func(str(num)); be easier?

1 Answers1

0

For starters, the return value of scanf (and similar functions) is the number of conversions that took place. That return value is also used to signify if an error occurred.

In C you must manually manage these errors.

if ((retv = scanf("%d", &n)) != 1) {
    /* Something went wrong. */
}

What you seem to be looking for are conveniences found in higher-level languages. Languages & runtimes that can hide the details from you with garbage collection strategies, exception nets (try .. catch), etc. C is not that kind of language, as by today's standards it is quite a low-level language. If you want "non-messy" functions, you will have to build them up from scratch, but you will have to decide what kinds of tradeoffs you can live with.

For example, perhaps you want a simple function that just gets an int from the user. A tradeoff you could make is that it simply returns 0 on any error whatsoever, in exchange for never knowing if this was an error, or the user actually input 0.

int getint(void) {
    int n;

    if (scanf("%d", &n) != 1)
        return 0;

    return n;
}

This means that if a user makes a mistake on input, you have no way of retrying, and the program must simply roll on ahead.

This naive approach scales poorly with the fact that you must manually manage memory in C. It is up to you to free any memory you dynamically allocate.

You could certainly write a simple function like

char *itostr(int n) {
    char *r = malloc(12);

    if (r && sprintf(r, "%d", n) < 1) {
        r[0] = '0';
        r[1] = '\0';
    }

    return r;
}

which does the most minimal of error checking (Again, we don't know if "0" is an error, or a valid input).

The problem comes when you write something like func(itostr(51));, unless func is to be expected to free its argument (which would rule out passing non-dynamically allocated strings), you will constantly be leaking memory with this pattern.

So no there is no real "easy" way to do these things. You will have to get "messy" (handle errors, manage memory, etc.) if you want to build anything with complexity.

Oka
  • 23,367
  • 6
  • 42
  • 53