3

I've been learning C recently.

I've completed a number of coding challenges on websites like codewars in C, and I always find myself wishing I had something like Python's flexible data structures. In fact, I usually end up creating a poor approximation of them.

This got me thinking, first of all, do people have boilerplate code that they port into every project they work on to get key functionality? Second of all, is C only ever used in production when you need something to work fast, and no other language has C's level of access?

3
  • 4
    There are more options than writing a whole program in C; in many environments we can use C or assembly for only the most performance critical parts of the program. Further, C++ is as good as C as far as generated code quality, while also having some of the modern features you'd expect. Commented May 6, 2023 at 19:11
  • Okay, so C++ could be the way to go then? What about Rust? I've heard that's low level whilst still being useable. Commented May 6, 2023 at 19:39
  • 2
    @Connor Since C++ is highly C-compatible, but has lots of usability and safety features, I highly recommend it over C. However, some features like exceptions are controversial in low-level contexts. Rust is a much more modern language in the same space that makes it easier to write correct low-level code (in particular, preventing memory safety issues), but it can be very difficult to use when coming from Java/Python style OOP. Personally, I prefer dealing with Rust's borrow checker over debugging segfaults or other UB issues in C/C++. Cargo is also much better than CMake. Commented May 8, 2023 at 6:07

1 Answer 1

6

I always find myself wishing I had something like Python's flexible data structures. In fact, I usually end up creating a poor approximation of them.

If you're missing data structures, there are reusable libraries in C just like in other languages: https://stackoverflow.com/questions/668501/are-there-any-open-source-c-libraries-with-common-data-structures

Second of all, is C only ever used in production when you need something to work fast, and no other language has C's level of access?

The most common use case for C is probably embedded programming, where you have to work with extremely limited resources and don't even have an operating system.

On mainstram hardware, reasons would be:

  • needing direct access to hardware, such as for device drivers.
  • Hard realtime requirements. If you have to satisfy low latency guarantees, software abstractions are a liability.
  • as you mentioned, performance. Though that doesn't only mean speed but also being more economical with memory, for tasks that need a lot of memory to work at all.
  • having to work closely together with existing C code
  • having developers who are familiar with C. Retraining or replacing your staff likely costs more than any advantage you gain with more high level languages, in the short and medium run.
5
  • Thank you! Great answer, in that case, should I bother solving coding problems that are far easier to solve in a higher level language and focus on embedded system problems? Or memory constraint problems? Commented May 6, 2023 at 19:41
  • 2
    @Connor: depends on what your goal in learning C is. If you plan to use it professionally, it would make sense to practice the kind of things it tends to be used for professionally. If you just want to be a programming polyglot, it makes just as much sense to practice on problems you are familiar with in other languages. Or even just problems you find interesting by themselves. Commented May 6, 2023 at 20:03
  • I would like to be a polyglot, but C is a small language! The quirks of memory management and compilation seem to be the most difficult parts to figure out! Do you think working on normal problems is good because it forces you to create your own structures? It has given me a greater appreciation of how incredible something like Python is. Commented May 6, 2023 at 20:07
  • 2
    @Connor you left out undefined behavior. Commented May 7, 2023 at 2:48
  • 2
    I'd add another item: writing interpreters/VMs for higher-level languages (like Python, Java, PHP probably) - C and C++ are used a lot for those, and writing one makes for a nice project too. Commented May 7, 2023 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.