News for hackers. Stuff that matters.
Re:Relive: 50 years of C, the good, the bad and the ugly by asveikau on Friday December 30, 2022 @03:54PM (#34189959)
There are plenty of interesting C works out there, as well as data structures or algorithms that C can express elegantly.

Small example, linked lists. I don't think non-C linked list code tends to be as straightforward as I've seen in C.

Or the character-at-a-time style of string processing. It's kind of unique to C.

You can say there is stuff about that you don't like. That's fine. Linked lists suck with modern CPU caches anyway. C strings have lots of misadventures in terms of buffer overflows. But it's unique and interesting. Lots of elegant things have been written this way. Your unfamiliarity with it doesn't make it "a joke" to point this out.

I have my own gripes with HN. Try mentioning politics and it brings out all sorts of fascist-sympathizing crazies. But saying good to neutral things about C (while not even universally praising it) is not one of those issues.

Re:Relive: 50 years of C, the good, the bad and the ugly by Koshkin on Friday December 30, 2022 @05:16PM (#34190731)
> as straightforward as I've seen in C

To be fair, the most straightforward definition of the linked list is generic, and C completely lacks such facility.

> fascist-

Sounds familiar.

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

Re:Relive: 50 years of C, the good, the bad and the ugly by asveikau on Friday December 30, 2022 @06:10PM (#34191221)
> C completely lacks such facility.

Generic linked lists can be done with pointer casts. See the way the Linux kernel does it.

The list manipulation routine takes a pointer to a member, and the more specific type is opaque. The code that knows what the actual structure is can derive it from a node pointer.

A structure can even have multiple node pointers in this scheme.

Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Saturday December 31, 2022 @03:36AM (#34194714)
* * *
Re:Relive: 50 years of C, the good, the bad and the ugly by imran0 on Saturday December 31, 2022 @08:11AM (#34196130)
> Sounds familiar.

I cannot decide whether it's an honor or an insult to be called a _fascist sympathizer_ for opposing the authoritarian suppression of speech.

Re:Relive: 50 years of C, the good, the bad and the ugly by asveikau on Saturday December 31, 2022 @11:44AM (#34198134)
I didn't want to address this because it's a tangent, but I understand that free speech protects expressing fascist thought. I can still disapprove of it and not want to be around a lot of it, consider high concentrations of it to be a sign of an unhealthy community, without saying it should be somehow illegal.
Re:Relive: 50 years of C, the good, the bad and the ugly by WastingMyTime89 on Friday December 30, 2022 @06:51PM (#34191599)
I am extremely familiar with C. I used to work on a static analyser for it. The issue is that there is very little to like in how C is designed from a PL point of view.

You are confusing what the language can do and what is semantic is.

C strings are just a contiguous allocation of byte and a bunch of functions which interprets it as ascii characters and stop on a specific value. That’s literally the worst representation of a string you can have.

Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Saturday December 31, 2022 @12:01AM (#34193743)
I think it's a nice representation, allowing easy recursion.

   const char *my_strchr(const char *str, int ch)
   {
     if (*str == 0)
       return NULL;
     if (*str == ch)
       return str;
     return my_strchr(str + 1);
   }
It's a good format for storage and communication.

Name any other string data structure and I will cite you all the disadvantages compared to the C string.

- If the format contains pointers, you have to marshal it to a flat representation to communicate or store it. The C string is already marshaled.

- If the format contains size fields, their own size matters: are they 16, 32, 64. What byte order? Again, needs marshaling. You might think not if going between two processes on the same machine. But, oops, one is 32 bit the other 64 ...

I shudder to think of what FFIs would look like, if C strings were something else. It's the one easy thing in foreign interfacing.

C programs themselves sometimes come up with alternative string representations, for good reasons; but those representations don't interoperate with anything outside of those programs, or groups of closely related programs.

Re:Relive: 50 years of C, the good, the bad and the ugly by WastingMyTime89 on Saturday December 31, 2022 @06:56AM (#34195672)
C null terminated strings are not magic. Protocols between the sender and receiver have to be defined. You face exactly the same problem regarding byte order, size consideration and marshalling with C strings.

You are basically arguing that C strings are a good default because they are the default. If they were something else, well, we would have saner FFI in a lot of place.

C strings are a terrible default. They are fundamentally unsafe. Mishandle the null char for any reason and you now face a serious security issue.

Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Saturday December 31, 2022 @12:37PM (#34198640)
* * *
Re:Relive: 50 years of C, the good, the bad and the ugly by WastingMyTime89 on Saturday December 31, 2022 @01:09PM (#34198992)
> Not char/byte C strings. You might be thinking of wchar_t strings.

No I’m talking about the classic byte arrays C pretends are strings. If the receiver you are talking to is not expecting a chain of bytes where each byte is a character and one special value means the chain is over, well, you will have to transform what you are sending to what your receiver is expecting.

Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Saturday December 31, 2022 @07:12PM (#34202136)
If somebody's expecting something other than a C string then we unsurprisingly have gratuitous complications. If everyone agrees on the C string though, it just interoperates as-is. Code compiled for some 16-bit microcontroller can just shove the C string into its transmit buffer, and on the other end, code compiled for 64 bit big-endian Power PC receiver just uses it as-is.
Re:Relive: 50 years of C, the good, the bad and the ugly by timbit42 on Sunday January 01, 2023 @11:35AM (#34207747)
C is the biggest mistake in the software industry since the beginning of software. The people that worship C only do so because they haven't seen the better paths the software industry could have chosen.
Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Monday January 02, 2023 @12:15AM (#34214613)
E.g. Donald Knuth hadn't seen better paths.

"I think C has a lot of features that are very important. The way C handles pointers, for example, was a brilliant innovation; it solved a lot of problems that we had before in data structuring and made the programs look good afterwards. C isn't the perfect language, no language is, but I think it has a lot of virtues, and you can avoid the parts you don't like. I do like C as a language, especially because it blends in with the operating system (if you're using UNIX, for example).

All through my life, I've always used the programming language that blended best with the debugging system and operating system that I'm using. If I had a better debugger for language X, and if X went well with the operating system, I would be using that."

Dec 7, 1993, Computer Literacy Bookshops Interview.

Re:Relive: 50 years of C, the good, the bad and the ugly by asveikau on Monday January 02, 2023 @02:50PM (#34222748)
This is ridiculous hyperbole.

Nobody "worships" C and people who have positive experiences with C often have exposure to higher level languages.

You can find strengths or upsides in C and still acknowledge faults, and still acknowledge merits elsewhere.

Re:Relive: 50 years of C, the good, the bad and the ugly by andsoitis on Saturday December 31, 2022 @11:25AM (#34197875)
This routine only works with ASCII, yea?
Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Saturday December 31, 2022 @12:44PM (#34198732)
It will also find ASCII characters in UTF-8. That's a common situation because delimiting characters are often in the ASCII range.

We can make a more complicated example that nevertheless uses recursion and essentially the same way, which looks for a UTF-8 character. We can examine and decode a prefix of the string as a UTF-8. If there are bad bytes or the character doesn't match then we recurse.

We can also write a wide character (wchar_t) version of the function which looks the same. That will handle all of Unicode on sane platforms, and the basic multilingual plane (BMP) on Windows.

Re:Relive: 50 years of C, the good, the bad and the ugly by kazinator on Saturday December 31, 2022 @12:44AM (#34193965)
If that static analyzer was itself written in C, you have one program's worth of C experience, which is very little.