Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I want you to ask yourself an honest question. When was the last time you actually had fun in programming?

Every time I write C code.



For me, C is fun until I hit a certain level of abstraction complexity involving fake homespun vtables and it starts getting harder than it should be to chase down bugs.



Everytime I need to write C, I wish I could use C++ instead. The things I miss most are RAII, templates, lambdas, const-correctness, and most importantly, a standard library with container types and algorithms.


What is wrong with const-correctness in C?

I think container types and algorithms is a fair point, but if you program C more you should have a go-to library or your own implementation.


> What is wrong with const-correctness in C?

One problem is that most C compilers accept implicit conversion between unrelated pointer types by default. If you accidentally pass a `const T*` to a function that takes a `T*`, you only get a warning. In C++ this is always a compiler error.

Another thing that I find very annoying (that is more about `const` in general): in C you cannot use `const` variables as compile time constants, instead you always have to use the preprocessor.

> but if you program C more you should have a go-to library or your own implementation.

Sure, but I still don't like the fact I need to find a third-party library (or roll my own) for the most basic data structures. Also, it's not like generic containers can be trivially implemented in C. They require lots of preprocessor magic and the resulting API will always be much worse than any equivalent C++ implementation, in particular with non-trivial types.


You can build with -Werror like the rest of us to get a compiler error instead of a warning.

As for compiler time constants, you certainly can use them for those, but the compiler will happily identify constants without the const keyword, whose true purpose is to cause a build time error if you try to modify it.

Regarding libraries, see my other reply:

https://news.ycombinator.com/item?id=42506570

Libuutil is a system library that is on any system that uses ZFS, although there are no known external consumers, so if you are interested in being the first, feel free to open bug reports with OpenZFS asking for removed functionality to be restored if you want any of it.


> As for compiler time constants, you certainly can use them for those

No, you can't. In C, `const` variables are not constant expressions. This means you can't use them in case labels or to define the size of an array.

> Regarding libraries, see my other reply:

I know that there are several container libraries for C. I was only complaining that there is no standardized solution for the most basic data structures.


What you want is something is is exclusively for compiler time constants, rather than can merely use compiler time constants. I know I am being pedantic here, but that is an important distinction to make when dealing with computer programming languages, since the descriptions need to be free of ambiguity or there will be problems. What you had meant was unclear to me in your previous remarks. I understand now.


There is the trick: use enum for const. It works most of the time.


Yes, at least that's better than macros.


As for not having a standardized solution, sys/queue.h is the closest you will get to it. It would be nice if we had one, but it has not happened yet.


I do not see how this not being an error is a problem. First, you can usually toggle this via a compiler flag. Second, I personally find it very convenient that the compiler does not stop for such things. This gives me more freedom how I can do my work, e.g. fixing const warning or do some other change first.

I do not agree about the APIs being worse than C++.


You should see the libuutil library that Sun made to provide containers and other niceties. Even without it, there is always sys/queue.h.


I agree, I switched from C++ to C and I found it relaxing to be able to just forgot about a million language features and their complicated interactions.

I also find you have some experience, know how to build good abstractions and have a set of good data structures, there is no issue with address complex problems in C.


For me I think it was Turbo Pascal and Delphi. And I will be honest, Visual Basic, and very early PHP. And may be even early Java.

I still dont think any programming language today capture what we had in the late 80s and 90s. But may be that is just nostalgia.


every time I write C code, I don't want to remember how to implement unordered map


You mean

    enum { nb = 1024 };
    struct { int k, v; } hash[nb];

    // 0 is an invalid key
    void incr(int k)
    {
      int i = k % nb, j = i;
      do {
        int k2 = hash[i].k;
        if (k2 && k2 != k) {
          i = (i+1) % nb;
          continue;
        }
        hash[i].k = k;
        hash[i].v++;
        return;
      } while (i != j);
      abort();
    }
Apologies for the telegraphic variable names and weird control flow. I wrote this on my cellphone. Lacking these 15 lines are what keep you from writing C?

There's a nice tutorial on hash tables in K&R, and I can also recommend learning about Chris Wellons's "MSI" hash table design, described in C at https://nullprogram.com/blog/2022/08/08/. He shows a more full featured hash table in about 30 lines of code, depending on what functionality you need. It's eminently practical, and usually a lot faster than generic algorithms.

That's not an exceptionally simple hash table either. One night I hurriedly wrote a rather long-winded implementation also on my cellphone (strings, with separate chaining and a djb-style hash function) and it also came to about 30 lines of code: http://canonical.org/~kragen/sw/dev3/justhash.c


In the interest of correctness, I just wrote a quick test of the above code at http://canonical.org/~kragen/sw/dev3/intcount.c. Perhaps surprisingly, it works, including the error handling, which is not always the case when you write a bunch of untested C after midnight on your cellphone. But a hash table is evidently simple enough.


Except, you know what? It fails (corrupting memory!) for negative keys k.


You can't use anyone else code and always delete your own previous implementation?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: