Live data from Hacker News

Learn C in Y Minutes

learnxinyminutes.com

11–20 of 81 posts

Re: Learn C in Y Minutes

#12
post #9

Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uninitialised variables. // Shorthands for multiple declarations: int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; int b, c; b = c = 0; Edited:.. After 1hr of posting my original comment, I noticed that there many opinions here. I should have added why I think this i…

What is the “correct” version of this?

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

#13

Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uninitialised variables. // Shorthands for multiple declarations: int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; int b, c; b = c = 0; Edited:.. After 1hr of posting my original comment, I noticed that there many opinions here. I should have added why I think this i…

There is nothing wrong with multiple declarations.

Re: Learn C in Y Minutes

#14
post #9

Earlier quoted context omitted.

What is the “correct” version of this?

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, espe…

Sorry

I

can't

read

your

comment,

could

you

split

your

words

into

separate

paragraphs?

Re: Learn C in Y Minutes

#15

Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uninitialised variables. // Shorthands for multiple declarations: int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; int b, c; b = c = 0; Edited:.. After 1hr of posting my original comment, I noticed that there many opinions here. I should have added why I think this i…

There is nothing wrong with multiple declarations.

It's more difficult to read and edit.

Re: Learn C in Y Minutes

#16

Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uninitialised variables. // Shorthands for multiple declarations: int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; int b, c; b = c = 0; Edited:.. After 1hr of posting my original comment, I noticed that there many opinions here. I should have added why I think this i…

This is also bad advice for C: void function_1(); Most people coming from other languages (like C++) will probably think that this is identical with "void function_1(void)", but without the explicit void argument list, the compiler will not warn if the function is accidentally called with parameters.

-Wstrict-prototypes

"Warn if a function is declared or defined without specifying the argument types."

Re: Learn C in Y Minutes

#17
post #15

Earlier quoted context omitted.

There is nothing wrong with multiple declarations.

It's more difficult to read and edit.

I wish we could talk about style in objective terms, not just opinion. Opinions are kinda stupid. Here's my opinion: https://news.ycombinator.com/item?id=26790701 (actually that's just a faux opinion to make a point, but you get the point)

And if there are no objective facts, then maybe we could just stop calling other peoples' preferred style nasty and "bad habits" as if it were objective.

Re: Learn C in Y Minutes

#18
> Ah, C. Still the language of modern high-performance computing.

Yet its major compilers are written in C++ nowadays, and C++ is the main language for GPGPU computing, including the memory model used by NVidia on their cards.

I suggest some benchmarks updates to the author.

Re: Learn C in Y Minutes

#19

As a web developer, who mostly spends time with Go and Python, is there any benefits of learning C?

C is a low level language that compiles down directly to assembly. It is similar to Go in this regard, but is a bit lower level. You have to manage memory yourself and pointers in C are much more dangerous. Reasons to learn C would include: absolute performance needed maybe for say a Python C extension, kernel hacking, some other project you want to contribute to uses C.

I think it’s good to at least play with C and understand how memory management and pointers in C work, but I don’t think I would recommend you go out of your way to learn C or spend any significant amount of time writing it. At the end of the day it’s just another tool in the toolbox. I would recommend the book Code, by Petzold, if you want to really want to explore computers at a low level in a friendly format.

Re: Learn C in Y Minutes

#20
post #9

Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uninitialised variables. // Shorthands for multiple declarations: int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; int b, c; b = c = 0; Edited:.. After 1hr of posting my original comment, I noticed that there many opinions here. I should have added why I think this i…

What is the “correct” version of this?

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.

Post reply on HN