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.

98 Upvotes

238 comments sorted by

View all comments

77

u/[deleted] Sep 12 '26

[removed] — view removed comment

47

u/HildartheDorf Sep 12 '26

Thankfully it is (as far as I know) the only function that has been removed entirely from later editions of the spec.

21

u/crakked21 Sep 12 '26

My uni still teaches it in its CS 101 course xD

9

u/FlyByPC Sep 12 '26

From the questions I see online, lots of college courses are still teaching students using PIC16F84A and 16F877A microcontrollers, which were getting old twenty years ago.

3

u/Mountain-Builder-654 Sep 13 '26

Mine switched from pic to arduino the year I took the class. Dodged a bullet there

2

u/flatfinger Sep 14 '26

From what I understand, the cheapest microcontrollers aren't made by Microchip, but use an architecture which is very closer to the PICs than to anything else, and have a C-dialect compiler available for them. The C dialect used by the HiTech C compiler (at least before Microsoft bought HiTech) gave a good feel for how C was designed to be suitable for use on quirky execution environments. The C dialect used by Keil's 8051 compiler was perhaps even better in that regard; someone familiar with the 8051 might think it would be impossible to write a C compiler that could generate efficient code for it, but C programs that were designed around the platform's quirks could be quite efficient.

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

→ More replies (2)

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

→ More replies (5)

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

→ More replies (1)

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.

2

u/Vincenzo__ Sep 12 '26

It should work like fgets on stdin, it's beyond me why they made it like that

3

u/SeriousPlankton2000 Sep 13 '26

"Nobody will type more than 80 characters, right?"

2

u/Vincenzo__ Sep 13 '26

Wouldn't be surprised at all if this is actually the reasoning

→ More replies (1)

1

u/flatfinger Sep 15 '26

You think it's useful to have the buffer include a newline at the end of it, and have the tail of an excessively long line of input left pending? For interactive input, there really is no standard function that's better than using getchar() if recovery from an overly long input line is required.

2

u/siodhe Sep 13 '26

It's not that hard to write one that dynamically allocates space on either the stack or the heap depending on taste. A heap version can just fail and return an error code (I have one in a library). But passing in a limit makes so much more sense.

1

u/majorsid Sep 13 '26

Why would someone use gets when fgets exists?

1

u/flatfinger Sep 14 '26

There used to be a very common recipe for using gets safely:

  1. Recognize the need for a program to perform a one-off task with some data.
  2. Determine the length of the longest line of data involved.
  3. Write a program that reserves a buffer that's bigger than that and performs the task.
  4. Run the program on the data.
  5. Discard the program.

If the program accomplished what it was written to do, with the data it was written to act upon, any effort spent trying to make the program handle corner cases that were known not to be relevant for that data set would have been wasted.

I'd argue that the design of fgets() is worse. While its failure mode in case of longer-than-expected input is different from that of gets, making fgets() correctly handle such inputs is harder than writing a getchar()-based routine to read lines of input.

1

u/[deleted] Sep 14 '26

That doesn't even work on new compiler versions last i checked, i wanted to try it since it was in a boom but found they removed it.

1

u/flatfinger Sep 15 '26

It's easy to use safely. Don't expose the program to anyone who would type too much on an input line. In the era when C was designed, it was very common for C programs to be written solely for the immediate use of the programmer, and then discarded shortly after use, The gets() function was perfect for that language use case, but most trivial tasks one would have done using ephemeral C programs can now be done better via other means.