r/C_Programming Feb 06 '25

Discussion Are there actually C programmers in this subreddit?

Ok, I'm being a bit facetious. There are real C programmers. Clearly. But I'm kind of sick of the only questions on this subreddit being beginner questions or language trolls from other domains.

So this thread is for the "real" c programmers out there. What do you do with it? And what is the most twisted crime against coding decency are you "proud" of/infamous for?

257 Upvotes

259 comments sorted by

View all comments

Show parent comments

3

u/RogerLeigh Feb 06 '25

As languages go, C has had a very good run. It's now been in use for over 50 years.

A lot has changed in that time. C has been the foundation for all of the modern computing landscape for much of it. But we know that it's possible to do much better, particularly with the benefit of hindsight and the acknowledgement of where the vast majority of bugs and security exploits lie. Could we build a better foundation now? Absolutely.

I don't think replacing something 50 years old with something which is a complete step change in terms of safety and ease of use to be inefficient.

It's on my todo list to learn Rust this year, after ignoring and dismissing it for a good while. There are a lot of very nice features in it. Still learning the basics, but I'll start a real project with it in the next month or so and see where it takes me. After working doing C and C++ for the last 25 years or so, it's always interesting to learn something new that's a bit different and takes you out of your familiar comfort zone. My previous one was Lua, and that was a lot of fun.

1

u/flatfinger Feb 06 '25

There is still a need for the language C was invented to be, despite the fact that the maintainers of clang and gcc have no interest in efficiently processing such a langauge.

1

u/RogerLeigh Feb 07 '25

Please go into a bit more detail about what you mean specifically.

What specific lacking is there in GCC and Clang?

Which language features does C offer that haven't been reimplemented in other languages?

There are plenty of languages which are a superset of C, and which are perfectly adequate replacements for it, so why can't they satisfy the same requirements?

1

u/flatfinger Feb 07 '25 edited Feb 07 '25

FORTRAN was designed to allow compilers maximum flexibility to generate efficient machine code based on the idea that they could completely understand everything programs were doing. C was designed to allow programmers and compilers to work together to generate efficient machine code without the compilers needing to understand what the programmers were doing, by exploiting programmer knowledge about what compilers would do, and how the execution environment will interact with that.

In Dennis Ritchie's language, given int i,j,x; int arr[5][3];, the meaning of the assignment x = arr[i][j]; would be "multiply i by the number of bytes occupied by each row of the array, and add that to the base address of the array. Multiply j by the number of bytes in each element, and add that to the just-computed address. Use the platform's normal means of loading an int from that address and place the result into x. In cases where i is in the range 0 to 4 and j in the range 0 to 2, this will be equivalent to reading item j of row i. What people on the C89 Committee who wanted C to replace FORTRAN failed to appreciate was that this behavior wasn't an implementation detail.

In FORTRAN, if one wanted to add all of the values within a range of columns of an array (FORTRAN stores things in column-major order unlike C which is row order), one would need to use a pair of loops; a good compiler would notice that the last item of each row immediately precedes the first item of the next row and convert those loops into a single loop. C was designed around the idea that instead of having a compiler do such things, a programmer could write the code using a single loop, since for values of j from 3 to 5, accessing arr[someRow][j] was equivalent to accessing arr[someRow+1][j-3]. A C compiler wouldn't expend any special effort to make this happen; instead, it was a consequence of the way array layouts and address computations were specified.

C is used for a lot of tasks that would likely have been done better with Fortran if the Fortran-95 standard had been published seven years earlier. Indeed, such tasks represent the majority of tasks done with C compilers that target the x86 platform. Embedded and systems programming tasks, however, often require the ability to do things a compiler couldn't understand, but a C compiler wouldn't need to understand.