152

With all the new "modern" languages out today, how is it that C is still heralded as the fastest and "closest to the machine"? I don't really believe in there ever being only one correct way to do things, and C has been around for a really long time (since the 60's!). Have we really not come up with anything better than something written nearly 50 years ago?

I am aware that modern languages are higher-level and take care of certain tasks like garbage collection and memory allocation and utilize libraries and such. I'm just asking why there has never been a true second option to C.

Can it be that C is so perfect that no other way of operating a computer could be possible (developer-adoption aside)?

EDIT Look, I'm not trying to knock C or whatever your favorite language is. I'm wondering why C has become the standard and why other alternatives never emerged and C was just "accepted".

22
  • 46
    C++ is just as fast and much more productive to write in. <3
    – GManNickG
    Dec 8, 2009 at 6:16
  • 6
    um, aren't you forgetting about c++?
    – Evan Teran
    Dec 8, 2009 at 6:16
  • 24
    I would argue assembly is the fastest and "closest" to the machine.
    – AlbertoPL
    Dec 8, 2009 at 6:17
  • 30
    why all the motions to close? i'm really curious... i'm not trying to start flame wars or anything
    – Jason
    Dec 8, 2009 at 6:18
  • 18
    closed again? after being reopened by jeff atwood himself? why would you possibly want to close this?
    – Jason
    Dec 10, 2009 at 19:36

6 Answers 6

165

C is a very simple language, and it's because of this, along with its longevity, that's it's fast and optimized. It's also extraordinarily widely supported, in concerns with embedded environments, microprocessors, etc.

It's hard to beat a really simple and fast language. The only thing to improve upon a language like that is usability: decrease the time it takes to make similar, generic code, and make it easier to model with abstractions.

This is where C++ comes in. C++ can be just as fast as C. The thing is, C++ is a much more complex language, which means it definitely increases productivity; as long as people know how to use it. C++ and C are not almost the same language anymore.

Now, D was another step up. Same ability for fast code, optional garbage collection, etc., but it never caught on. Hopefully that changes, because it drops what plagues C++: backwards compatibility with C.

So to answer your question, "better" is a hard thing to judge. In terms of simplicity and speed, C is probably close to the best we could do. In terms of productivity versus simplicity, C++ is probably best we could do, though that opinion varies much more. Lastly, in terms of a fleshed-out and cleaned up language, with the speed and simplicity of C, D wins this context.

16
  • 36
    By "Simple" you mean "simple from a compilers POV, not the programmers POV". C is simple because it's basically assembly with statements and expressions. It's not simple the way Python is simple.
    – Marius
    Dec 8, 2009 at 7:56
  • 17
    But to clarify, simple for the compiler does mean simple to pick up. Perhaps not simple to implement complicated ideas in.
    – GManNickG
    Dec 8, 2009 at 8:01
  • 2
    You won't be writing an operating system in C#. (We're talking about systems programming languages, programming languages that compile to native assembly. anything you can do in any other language, you can do in C/C++/D, but not the other way.)
    – GManNickG
    Dec 23, 2009 at 19:18
  • 17
    For what its worth, OCaml produces highly optimized native code about as fast as C and C++, and code itself is as terse as Python. With a little polish, I think OCaml could match or beat C as the language of choice when you need to write code for maximum speed and minimum memory footprint. Objective-C also does a pretty good job, although its not always as fast as C, it gets the whole "C with objects" thing right in a way C++ never could and never will.
    – Juliet
    Mar 31, 2010 at 13:18
  • 6
    @Ferruccio: Yes, but every language can link to C libraries. So if we're willing to settle for that, instead of actual source code compatibility, as C++ (more or less) offers, then we might as well pick a completely different language, say, Haskell or Python. The reason C++ caught on wasn't that it could link to C libraries. It was that it could compile C code.
    – jalf
    Dec 23, 2010 at 9:13
65

There are languages faster than C.

For example, Fortran as already mentioned is doing very well because it has much more restricted aliasing language rules.

There are also experimental assembly like languages which are attacking C on the front where it is used as a high level assembler language for example compiler creation. Ever heard about C-- or Janus? But those two were killed by the LLVM project.

I would bet that APL or other mathematical languages will blow C out of the water in their special application domains as they have build in support for Vector processing units. This is something which is not possible for C (and guys: NO! Special optimized libraries with C linkage have nothing to do with C as a language).

Also CPU producers removed all stuff helping compiler writers in other languages - remember the tagged arithmetic assembler codes for making LISP implementation on SPARC fast? Gone with the wind.

And if you go away from micro benchmarks to application development then there are faster languages for application development. My personal example here is always SmartEiffel. It targets C but is using global system optimization which makes it faster then C in real world application development.

In this domain even a simple wrong or low level abstractions can kill the whole language performance. Because C does not offer high abstractions most people say it is a programming problem but it is not. For example look at the lack of generics. In C you will end up with slow implementations like the "qsort" library function which can be written a magnitude faster with generics (where the function call for key comparisons is eliminated).

Just compare a qsort call on a megabyte array of ints with a good hand written implementation which is using array access and the builtin '<' operator.

15
  • 10
    Very good comment. It is important to realize that some aspects of C language spec (like pointer aliasing rules) make it impossible to generate truly optimal machine code. Being a high-level assembler, C is not "the fastest", but merely "pretty fast".
    – Constantin
    Dec 8, 2009 at 9:00
  • 1
    Thanks i forgot to mention the impossibility to return multiple result values. Another low level aspect that i'm missing hard when using it as high level assembler.
    – Lothar
    Dec 8, 2009 at 11:47
  • 6
    Modern C, properly written, can encode the same aliasing assumptions as any other language, allowing the compiler to make the same optimizations. It's an important feature of the language that it also allows the programmer not to assume those requirements when they're not applicable. Your other points are spot on, so +1. Dec 8, 2009 at 16:32
  • 2
    @Rascher: Thats right and there you see the influence of C on the CPU designers which always specify a CPU ABI. And multiple return values is something that just doesn't come to there mind even if the number of registers would not be a problem (SPARC, PowerPC, Itanium)
    – Lothar
    Dec 8, 2009 at 19:02
  • 14
    Pointer aliasing in C has been dealt with by restrict in C99 (10 years ago!).
    – Pavel Minaev
    Dec 9, 2009 at 0:01
36

Good question. I think languages succeed by finding a niche. It's important to note that there are plenty of newer languages that are better than C in their niches.

  • C was once widely used as an application language, and in that domain it has steadily lost ground to C++, Java, and recently all sorts of other languages (notably the dynamic languages).

  • C used to be a language for writing server code. The Web pushed an amazing variety of languages into that space--Perl, Java, Python, VBScript, VB.NET, Ruby, C#--and cases where C makes any kind of sense for server code are now few and far between.

  • C has been used for scientific computing, but it faces competition from domain-specific languages like Matlab and Mathematica, as well as libraries like SciPy. A lot of people who write code in this niche are not coders by trade and C is not a great fit for them.

But C's niche is system code. Operating system kernels. Drivers. Run-time libraries. It is so established in that space that even C++ is displacing it rather slowly.

C won back in the 1970s because of UNIX, because the competing languages were either too restrictive or too slow, and because C code was considered reasonably portable (lies, even then). But its biggest advantages today are unrelated, and stem mainly from decades of dominating its niche. There are good tools for C: optimizing compilers, kernel debuggers, effective static analysis to find bugs in driver code, etc. Almost every major platform defines a C ABI, and often it's the lingua franca for libraries. There's a pool of programmers who know how to code C--and who know what C's problems and pitfalls are.

Long-term, this niche isn't going away; and C has some problems. But it would still be extremely hard for any newcomer to compete.

8
  • 9
    C is still widely used for server code. There is not only the Web, most DNS, email, etc servers are written in C. Even for the Web, Apache is in C.
    – bortzmeyer
    Dec 8, 2009 at 19:19
  • @bortzmeyer: Apache is pretty old though. I'm not saying it's not useful or has been superceded, it was just created when C was it and there weren't many good alternatives.
    – Macke
    Dec 8, 2009 at 21:05
  • 1
    I'm curious as to why you consider C "not a great fit" for scientific computing. Python is admittedly more pleasant to work in, but I've found C to be a solid alternative when faster code is needed and many of the possible alternatives (C++) not bringing a huge amount more to the table, unlike in other aspects of programming.
    – Fomite
    Nov 19, 2011 at 3:37
  • 1
    Beyond that, the amount of stuff in SciPy/NumPy etc. that's written in C already makes it useful to know.
    – Fomite
    Nov 19, 2011 at 3:37
  • 2
    @EpiGrad Maybe C is a solid alternative for you. But C has a super steep learning curve, especially for people who don't have a background in coding. In particular, sometimes it crashes, and if you're not a coder you have little hope of figuring it out. Mathematica makes a huge number of things easier to write and you end up with less code by factor of hundreds. And it doesn't crash. Nov 21, 2011 at 22:36
25

Paraphrasing a very good comment: There are not many different ways to make a language fast and "close to the machine" - C did it well, and there is hardly any room to improve upon that.

Original answer:

Fast to execute or fast to write stuff in?

Languages are not fast or slow to execute, specific implementations are. A languge can only be considered faster than others when it somehow makes it easier to have a fast implementations. Invariably, that means "close to the machine". But with machines getting faster exponentially, that has become progressively less interesting over time. Instead, ease and speed of development and portability have become much more important, so "better" has become to mean "away from the machine". Pretty much all efforts in language design have gone into that direction for the last 5 decades.

So there you are: closer to the machine and faster languages than C exist; they're those that came before C: Assembler, Fortran. Probably some forgotten ones.

10
  • @michael - but there's still a market for super-lean, super-fast programming, especially with the onslaught of all these mobile devices... why is C still the best option for some of these devices, ie, why is there not another at-that-level language, like .NET-vs-Python-esque?
    – Jason
    Dec 8, 2009 at 7:22
  • 5
    Lisp. Slumbering but not forgotten! :)
    – Carl Smotricz
    Dec 8, 2009 at 7:28
  • 2
    @Jason: Question is, how many portable assemblers do you need? C is well known and it's very hard to write a language with a faster implementation, so why bother? In suit-speak, there's no requirement, no market, no motivation.
    – Carl Smotricz
    Dec 8, 2009 at 7:31
  • 4
    @ Michael, apologies for comment-jacking your answer. The reason there is more in the terrain of python/ruby/java etc is that once you stop trying for the most efficient possible language you get a lot more options as to which features to prioritize, and every approach can yield a new language. There are far fewer ways to write the fastest possible language.
    – Carl Smotricz
    Dec 8, 2009 at 7:35
  • 1
    -1 for "and there is hardly any room to improve upon that" - there is plenty of room to improve on C/C++ without affecting performance. Mar 31, 2010 at 13:13
22

Fortran is faster than C for numerical tasks because of the way it handles memory references (C pointers are more difficult to optimize). The heavyweight numeric libraries at the base of things like Matlab and Numpy are still written in Fortran.

On the other hand, C++ can be just as fast as C, but has many more advanced programming features. It's a much newer language, from the mid 80-s.

10
  • 1
    And C++ is still being updated.
    – GManNickG
    Dec 8, 2009 at 6:30
  • 5
    @GMan: so is C.
    – Porges
    Dec 8, 2009 at 9:16
  • 7
    C1X (en.wikipedia.org/wiki/C1X)
    – Snarfblam
    Dec 8, 2009 at 15:12
  • 3
    Correct me if I'm wrong, but wouldn't a C99 restrict pointer have the same aliasing semantics is Fortran? And what else is there that makes any difference?
    – Pavel Minaev
    Dec 9, 2009 at 0:02
  • 10
    I think the idea that Fortran is faster than C is somewhat myth, depending on what's being coded and who's doing it. The reason heavyweight numeric libraries are in Fortran is not because Fortran is faster, but because the routines were originally coded in Fortran and few people have the nerve or need to re-write it. The small number of math-gurus who write and vet these algorithms are happy in Fortran and see no need to change, especially since they imagine Fortran is faster. Jan 26, 2010 at 15:07
16

What the heck, I'll chime in with my $0.02.

In many instances there is a real or perceived difference between "systems" languages and higher level languages. I'll ignore most "higher level" languages, since nobody (at least not many) will argue that for many tasks, languages like Python, Ruby, etc. are simpler to work in.

C was designed to be a systems language, meaning it was designed as the language in which the Unix operating system was written in. As such, it was designed to be simple, powerful, and fast. A simple language gains power by means that non-systems-programmers often consider dangerous: pointers, manual memory management, etc. As has already been mentioned, C is quite simple. K&R is the smallest book on my programming shelf by far (not counting O'Reilly Pocket References) and it's only marginally "bigger" than my Ruby Pocket Reference. C is quite powerful. If you need to talk to hardware, manually check and twiddle with memory, etc. C has the capability.

From a programmer's perspective, however, C is not so simple. Speed and power come at the price of manual memory management and not much OOP support built in to the language. C++ (not my favorite language) is much simpler from a programmer's perspective, but much less simple from a compiler's perspective. Objective-C (possibly my favorite language) has the same tradeoff, with slight lean in the direction of keeping the language simple (garbage collection is a newcomer to Objective-C, for example). But since the computing world as many of us know it was written in C, it's difficult for newer, more-complicated but "easier" languages to gain widespread adoption.

In some cases, especially when the current "standard" is as "good enough" as C is, there's simply not a lot of incentive for something "better" (C++, Objective-C, D etc.) to gain traction, when there is even enough incentive to create something "better".

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.