← Back to context

Comment by WalterBright

6 months ago

Spending a lot of time debugging code. Eventually, the pattern recognizer in your brain will pick out the bugs. The term for this is "code smell".

For example, when I'd review C code I'd look at the str???() function use. They are nearly always infested with bugs, usually either neglecting to add a terminator zero or neglecting to add sufficient storage for the terminating zero.

It is crazy that anytime someone works on application layer and wants to manipulate string, which is a very, very common thing to do when writing application, one has to consider \0 which would be an implementation detail.

How can that language still be so popular?

  • Programming is the consideration of implementation details. When you manipulate strings in C you consider the terminating nul byte just like when you manipulate strings in Python you consider how its stores codepoints or when you manipulate strings in Swift you think about grapheme clusters. There is no free lunch. (Though, of course, you can get reduced price lunches based on the choices you make!)

    • Pardon my ignorance, since I don't know C, but is it true to say that the length of string "Foo" is greater than 4 because of the null terminating byte? Or maybe there is no concept of string length? I could see this getting annoying since Foo is three chars long, you would assume it's length is 3, but we could be speaking of the actual length of bytes, in which i assume it is sizeof(char)*3+1 i.e. the sizeof(char F, char o, char o)+1nullbyte

      2 replies →

  • The language is just fine. The real question is: Why do people not use a string library that abstracts this away safely?

    • Oh, people tried. Every C programmer tried it. I tried multiple times. They all failed.

      Back when I was musing about what D would be like, I happened across some BASIC code. I was drawn to the use of strings, which were so simple in BASIC. I decided that D would be a failure if strings weren't as easy to use as in BASIC.

      And D strings turned out to be better than I'd dared hope!

      I proposed an enhancement to C to get much of that benefit, but it received zero traction in the C community. Oh well.

      https://www.digitalmars.com/articles/C-biggest-mistake.html

  • C was popular because, if one is familiar with assembler, it takes about an hour to become adept at programming in it.

    It's also an easy language to write a compiler for. At one point I counted over 30 C compilers available for DOS.

  • Okay, I want to make a desktop app that runs on Linux. Which language should I use? Java?

    • Some current trendy options would be Kotlin (with Kotlin Multiplatform) or C# (with Avalonia UI).

      Edit: I guess I should've at least asked myself if the question was rhetorical.

      3 replies →

    • That questions is kind of the point I want to make. We live in 2025 and C is still an option for new applications, i.e wrong abstraction layer for application level development.

      No doubt there are valid reasons to use it, that is just the state of things they are unfortunately.

  • Lots of C applications nowadays don’t actually use any of the str functions or null termination.