Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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".

share|improve this question
C++ is just as fast and much more productive to write in. <3 –  GManNickG Dec 8 '09 at 6:16
I would argue assembly is the fastest and "closest" to the machine. –  AlbertoPL Dec 8 '09 at 6:17
why all the motions to close? i'm really curious... i'm not trying to start flame wars or anything –  Jason Dec 8 '09 at 6:18
seriously? can we reopen this? –  Jason Dec 8 '09 at 6:24
closed again? after being reopened by jeff atwood himself? why would you possibly want to close this? –  Jason Dec 10 '09 at 19:36
show 18 more comments

migrated from stackoverflow.com Apr 15 '11 at 7:24

This question came from our site for professional and enthusiast programmers.

closed as primarily opinion-based by Thomas Owens Aug 5 '13 at 23:49

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

6 Answers

up vote 134 down vote accepted

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.

share|improve this answer
Withdrawing my answer because yours says the same things, and better. +1. –  Carl Smotricz Dec 8 '09 at 6:35
props for pointing out why C++ is not an obvious replacement for C –  Matt Joiner Dec 8 '09 at 7:11
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 '09 at 7:56
But to clarify, simple for the compiler does mean simple to pick up. Perhaps not simple to implement complicated ideas in. –  GManNickG Dec 8 '09 at 8:01
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 '10 at 13:18
show 13 more comments

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.

share|improve this answer
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 '09 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 '09 at 21:05
@Marcus, no, it was created on the Unix platform, and on Unix that means C. Especially in those days you had to distribute source as the binaries could not and cannot run across platforms. –  user1249 Apr 10 '10 at 6:57
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 '11 at 3:37
I'm a huge fan of C myself, but it’s not for everyone and its strengths are not Mathematica’s strengths. How many lines is the C equivalent of Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}]? –  Jason Orendorff Nov 21 '11 at 23:18
show 4 more comments

There are faster than C languages.

There are faster languages 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 assember language for example compiler creation. Ever heared 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 there 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.

share|improve this answer
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 '09 at 9:00
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 '09 at 11:47
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. –  Stephen Canon Dec 8 '09 at 16:32
@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 '09 at 19:02
Pointer aliasing in C has been dealt with by restrict in C99 (10 years ago!). –  Pavel Minaev Dec 9 '09 at 0:01
show 3 more comments

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".

share|improve this answer
+1 thanks, this is a good answer –  Jason Dec 8 '09 at 17:14
add comment

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.

share|improve this answer
@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 '09 at 7:22
and i meant fast to execute... i understand the benefits of higher-level languages being faster to write in (i write in .net, ha) –  Jason Dec 8 '09 at 7:23
Lisp. Slumbering but not forgotten! :) –  Carl Smotricz Dec 8 '09 at 7:28
@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 '09 at 7:31
@ 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 '09 at 7:35
show 3 more comments

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.

share|improve this answer
@GMan: so is C. –  Porges Dec 8 '09 at 9:16
C1X (en.wikipedia.org/wiki/C1X) –  Snarfblam Dec 8 '09 at 15:12
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 '09 at 0:02
@Pavel: As far as aliasing is concerned, yes. Fortran also has looser floating-point semantics -- the default is somewhat akin to -ffast-math, but it's not obvious that that's a good thing. –  Stephen Canon Dec 9 '09 at 5:42
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. –  Mike Dunlavey Jan 26 '10 at 15:07
show 4 more comments

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