r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

95 Upvotes

238 comments sorted by

View all comments

Show parent comment

19

u/max123246 Sep 12 '26

Still in the holy K&R book that way too many people recommend to beginners. Like it's cool as a historical read, I would never recommend it to beginners because they'll pick up awful patterns

2

u/The_Northern_Light Sep 12 '26

I’m not a beginner but I don’t know what C book to recommend to one. What’s the best modern first programming book?

3

u/max123246 Sep 12 '26

Sadly I'm not sure, I bought K&R per people's advice and stopped using it pretty quickly when I realized it wasn't updated at all for modern C

I learned on my own for the most part from college and otherwise. Maybe Casey Muratori has a book or classes to look into? I trust his judgement in comparison to most public programming figures

1

u/Immotommi Sep 12 '26

Casey has computerenhance.com though it is not quite aimed at beginners

1

u/RootHouston Sep 14 '26

I am also still confused when people suggest this book for absolute beginners. I read it early on, and then realized that it's just not modern enough.

1

u/SeriousPlankton2000 Sep 13 '26

What bad pattern do you remember (if you even want to remember)?

2

u/max123246 Sep 13 '26 edited Sep 13 '26

They loved within the for loop expression or while loop expression to put gets so that you have a side effect of the gets and you use the return as a condition to the loop

As an example: https://github.com/caisah/K-and-R-exercises-and-examples/blob/master/01.05.3-line_counting/ex-count_lines.c#L9

And another example: https://github.com/caisah/K-and-R-exercises-and-examples/blob/913d6427cd397aeaf649e839ceb77ef5d5e75a61/04.02-functions_returning_non_integers/ex-primitive_calculator.c#L24


    for (val = 0.0; isdigit(s[i]); i++)
        val = val * 10.0 + (s[i] - '0');

They also declared variables all upfront without an initial value

It's not bad patterns per se but no one would write code like it today. It definitely makes code harder to understand as a beginner

1

u/musbur Sep 14 '26

What's wrong with the number parsing snippet ("wrong" in the sense of promising more than it can deliver)?

1

u/SeriousPlankton2000 Sep 15 '26

Declaring variables without a dummy value is a good thing to be warned about accessing undefined values. If you do have int foo = 0xdeadbeef, you can get 0xdeadbeef in production; if you have int foo, you get a warning or an error while compiling if you e.g. miss setting it in a case branch of a switch statement.

I'd agree that it's better to declare and set a variable later in a function as soon as you can set its value - provided that there is a linear path that allows doing so. (I like to have the full context of a variable on screen - if possible.)

2

u/max123246 Sep 15 '26

By default compilers will emit a warning and let your uninitialized variable invoke undefined behavior. Yes, you should have warnings as errors but it's still not great when basically all variables could have a sensible initial value

1

u/SeriousPlankton2000 Sep 16 '26

What is the sensible init value that will be different from a good value?

Also is it sensible to add dummy writes to the stack?

2

u/max123246 Sep 16 '26

If you're adding in a loop, 0, if you're multiplying in a loop, 1, if you're branching and setting values, then the value of one of the branches

Your compiler optimizes away dummy writes if it's truly unneeded. Compilers are really good at constant folding and removing unused values in particular. They're less good at loop hoisting or loop auto vectorization

1

u/bonqen Sep 14 '26

It's still a decent book for learning C, i.e. the syntax and core principles.

1

u/Dangerous_Region1682 Sep 14 '26

I still think at least the ANSI edition should be something one has to understand the intent of the language. The book to learn the basics of the language, probably not, but to have on your bookshelf to read through when you have a certain level of competency I think it has value.

There again back in 1977 when I learned the language the first, first edition was all I had so I’m still kind of attached to the thing excepting the ANSI edition is probably a better starting point now.

1

u/max123246 Sep 14 '26

I think a history book on C or listening to interviews would do you far better for learning about the intent of the language. I used the ansi edition, it's still has the same problems I listed above. C has been around for 40+ years since 1977, a lot of how people write C and consider best practice has changed

1

u/Dangerous_Region1682 Sep 14 '26

50 years ago there was not much else available. It was the early editions of the K&R book and the UNIX V6 source code or nothing.

Would I think that would be the best way today, but I’d still have the ANSI edition in my library and it’s worth a read as you become accomplished.

1

u/[deleted] Sep 14 '26

It's decent but i feel there's too much he doesn't explain, and some of the exercises are too hard but useless in practice.