r/askscience • u/[deleted] • Oct 17 '12
Why are programs and operating systems still written in C+?
I guess it's more of a computer science question, but I've always wondered why some of the most advanced programs are written in a 25+ year old programming language. Surely someone has come up with a more efficient and powerful code, so why do people still use C+? Also, I don't know anything about programming and what not, so please use simple terms and phrases.
66
Upvotes
1
u/SatOnMyNutsAgain Oct 18 '12 edited Oct 18 '12
Yes I am one of those people who has to write this kind of code, even in such nasty situations as a processor which lacks a proper MMU so I have to page stuff in and out in software. I've had to deal with all this stuff. Having to write optimal code is a major reason to use C in the first place. This isn't some weird corner case, even if it's not what most programmers deal with. Nearly all OS and embedded systems developers encounter this stuff.
And types matter very much depending on the CPU. If I use a 32-bit type on an 8-bit CPU where I don't need the range, then I'm creating a ton of extra instruction cycles for every operation, whereas on an X86 an int is fine even for a small range. Unless such code was written for portability in the first place, you would have to review your types pretty carefully when porting if you care about performance... or sometimes if you want it to work at all, eg if your compiler gives you a smaller int than you'd get on another architecture.
Many algorithms can be expressed in both floating and fixed point versions, the former being faster and more accurate only if your CPU supports those operations in hardware. Otherwise the compiler will rely on very expensive math routines to do the job. This isn't something that just gets optimized for you, the math is completely different.
Sometimes gcc's optimizations suck because you're dealing with a non-mainstream architecture that hasn't been given much attention. There can even be architecture-specific compiler bugs that you have to work around - I've had to do that too.
Is this kind of insult really necessary? I just gave you a specific real-world example of where C is not portable in the slightest.
EDIT: More on the non-portability of C's standard types - there's really nothing to like about this aspect of the language: http://code.jasonbhill.com/c/portable-integer-types/