News for hackers. Stuff that matters.
Re:Learn C in Y Minutes by shoto_io on Tuesday April 13, 2021 @07:10AM (#26790652)
What is the “correct” version of this?
Re:Learn C in Y Minutes by sltkr on Tuesday April 13, 2021 @07:26AM (#26790776)
There is nothing incorrect about multiple declarations, but the syntax can become confusing when declaring pointers or arrays:

    char *s = "foo", *t = "bar";
A beginner might forget to add the asterisk in `*t` (especially if the first declaration is written `char*` with no space on the left of the asterisk). Similarly with multiple types:

    char *p = "foo", **q = &p, *z[2] = {"foo", "bar"}, *(*f)(const char*) = strdup;
This is correct and an intermediate C programmer won't have too much trouble parsing these, but in the interest of clarity, it makes sense to put each declaration on a separate line.

If you find yourself needing a lot of local variables, that's often a bad sign anyway, since it implies that the function has a lot of state to manage. In that case, you might want to rethink your implementation to make it less complex, or restructure the code to make it more managable.

Re:Learn C in Y Minutes by flohofwoe on Tuesday April 13, 2021 @07:13AM (#26790682)
Just split them into separate variable definitions:

    int i1 = 1;
    int i2 = 2;
The "usual footgun" is:

    int i1, i2, i3 = 0;
Only i3 will be initialized in this case. And since one needs to write out the assignments anyway, adding the type for each variable doesn't hurt either.

(today's compilers usually have uninitialized-warnings for this though, but sometimes people forget to add a -Wall to their compiler options, especially beginners, and then get stuck on such trivial problems).

Re:Learn C in Y Minutes by thrwaeasddsaf on Tuesday April 13, 2021 @07:15AM (#26790701)
Sorry

I

can't

read

your

comment,

could

you

split

your

words

into

separate

paragraphs?