Hacker News new | ask | show | jobs
Why do we use C still?
12 points by netbyte 5165 days ago
Why do we use C still when there are other languages like Go or C++ or even D? BCPL and B were done away with so quickly, so why do we still use C in our modern society with languages that can do things a lot more efficiently?
10 comments

Some reasons:

1. Everyone has a C compiler, or can write a decent-enough one to support their preferred environment.

2. C is well tested and specified. Reading the standard isn't an exercise is face melting (compare with, say, C++).

3. As a systems language, C doesn't require a runtime library if not needed, and this can be left out very easily (not the case with Go, D, or C++, as I'm aware).

4. C exposes easy trapdoors to the underlying machine (inline assembly, pointers, etc.) that are very handy.

5. C is usable by other languages, as its calling conventions are well documented. This is why C is a preferred base language for things like OpenGL bindings--it's easy to write Ruby, Pyhton, Java, etc. wrappers. This is in sharp contrast to C++, which is brainfucked for things like symbol exports.

6. A line of C is pretty obvious in what it does, when written correctly. You can usually estimate what code it'll generate, barring #define hackery. C++ especially is bad at this.

~

C is still used because it doesn't do a lot, and it nails what it does do.

C is an adequate language (actually, more than adequate) with a large base of skilled programmers. There is a long history of successful development. And the developer base is aware of the warts that still remain in the language.
But why use C when there are other Systems programming languages like Go, C++, D, etc..
You can compile programs with those languages and get something that works, but on a regular basis, some programmer will say something like, "Ok, I'll use Go for project x" and get part of the way into it and then discovers...

...a critical library has not been ported.

...a really useful language feature doesn't exist, or its implementation isn't appropriate for this application.

...a compiler bug appears and causes days to weeks of torment.

...it's difficult to deploy and distribute the result.

...there are fewer tools, debugging aids, etc.

Languages don't live in a "clean room" environment. All of these things add value even when the language itself is unsound. And then you weigh the cost of rolling new tools, fixing the compiler yourself, etc. against building on the old stuff, and the old stuff wins most of the time. Reaching the opposite conclusion is the exception, and to get there, the language usually has to carve out a niche use-case where it has the best library for a certain domain.

One reason would be the massive existing codebases written in C (Linux kernel, anyone?)...
We should all switch to Java/C#. It makes programming much much faster and easier. http://techiest.blogspot.com/.
I work on embedded microcontrollers that have, at the most, 16K of code space and 512 bytes of RAM. I have a nice efficient optimizing C compiler that creates object code nearly as tight as the local assembly language.

Do you think I would benefit from Java? Because I don't.

And the resulting code much slower too. It all depends on what you want to accomplish. Really, THIS particular topic has substantial bandwidth allocated to it already...
The only way to do elegant programming is with C. C is as perfect as programming languages can get. The other languages that you named are just more flawed than C. That's why.
Ah yes, I'm sure the Go designers in particular are ignorant about the pros and cons of C </s>
Generally, the good reasons you'll see for using C are lack of C++ compiler availability and embedded programming.

The bad reasons tend to be variations of developer inertia and the inability to decide which features of C++ ought to be used -- arguing against the STL isn't a good argument against C++ as a whole.

On Linux/Mac you have g++ and on Windows you have MinGW/Visual Studio
There are many more operating systems out there.
C is currently the language I know best. I may therefore be biased. There are multiple reasons why C is useful for system programming. Legacy code as others have said. A huge community for support and maturity in tools. The fact that C is very close to the bare metal of the system, which means that problems with your code like memory leaks are most likely the programmer's fault , and can thus be fixed by the programmer. It also isn't proprietary in any way. There is no for profit company that controls the language.

C has disadvantages. But many of them are the result of its advantages. Automated garbage collection is great, but garbage collection by hand tuning code gets stuff that AGC may miss.

That said, I'm interested in picking up some of the other languages mentioned.

I use C as a language of last resort, as a sort of universal assembly language. If I get a problem to the point that I know exactly what I want the machine to do, but can't convince Haskell or Python or Java to do it and don't have the time to research a proper solution, I can knock something out in C that will be as fast as the machine can reasonably be expected to go. It erases all of the questions about whether the compiler is doing something mysterious to sabotage you.
Because the Go compiler doesn't optimize good enough yet?

It seems like the one language that might replace C/C++ in many instances. My favorite feature is the incredible compile times.

http://golang.org/

Ironically, your favorite feature is also your least favorite feature: The compiler is relatively simple and unambitious, resulting in fantastic compile times. Once you start doing advanced analysis and multiple passes, you suddenly aren't so speedy anymore.
Just search for 'C++ for kernel' on google. There are plenty of reasons. A good answer can be found here: http://stackoverflow.com/a/520355
because C is perfection
Nope, Java/C# is PERFECTION. http://techiest.blogspot.com/.