ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Okay, so I've been programming PHP for years, programmed Perl for a little while and even did some C++ a while back. Lately I've been freshening up on C++ and learning more about it though. My main question is though, why is C more common for operating systems than C++??? Is it really just for simplicity sake? Such as not including features you don't need? Or is there another reason for it?
from what I understand about Linux anyway is that it was originally developed on an i386 in C simply because g++ was not available to Linus when he wrote the kernel. The kernel was supposedly recompiled at some point using g++ and actually ran slower so they've just stuck with C. as far as other OS's not sure for there reasons. However there are OS's written in C++ although not very popular.
There is more than on reason. The first one is that the OS needs to provide the infrastructure for the applications. C runtime environment is small and simple. The one for C++ is much bigger and complicated. It would be also harder to debug at the system level. It also adds some (small, but still) overhead in times of the instructions executed.
The second argument is that there's the custom. You simply write OSes in C.
To reiterate, or paraphrase, the lower the level is that your working with, the lower the language you should use. For example, devices drivers are usually written in assembly or C. Operating systems (and other kernel-mode software) are usually written in C. C is a subset of C++, so that C++ would be considered a "higher-level" language compared to C. The lower the level of programming language you use, (generally) the more power and control you have of the environment.
Hm... Never thought about that, just going over C++ is seems to have a lot of very useful features. Thanks guys, aside from here I never was able to find an actually good answer to that.
C was created to make Unix more maintainable. C++ was created for a higher level of abstraction so that more abstract designs could be implemented. A lot of C must be ignored to create an operating system, as would be the case with C++, because of the nature of linking. For this reason, a lot of higher-level things in C++ aren't available to someone trying to write a kernel. Function overloading and namespaces complicate linking in C++ quite a bit, which by themselves prevent taking full advantage of dynamic linking with C++. Because of its origins, the POSIX standard specifies a standard C interface; therefore, the vast majority of *nix programs are written in C, which makes it sort of a catch 22.
Kevin Barry
This includes the following thoughtful explanation from Linus himself: [...]
It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.
[...]
Those are interesting comments; however, C++ is quite a bit different than it was 16 years ago. The horror stories sound like pre-standardization problems, not that the standards committee always does the best thing now. You also have the other extreme where people won't allow a single call to libc in their code because "it's C" (I recall a post here or elsewhere that said this was a company policy for someone.)
C++ is designed in a way that makes translating it to C the logical thing to do. For example, it makes a lot of sense to take member functions, add an argument for this, and treat them like global functions. For virtual functions, it makes sense to create a static table of pointers to such functions for each class, then give each instantiation a pointer to the appropriate table. Destructors are implicitly called when a stack variable goes out of scope; therefore, the compiler implicitly inserts a call to the destructor at the point where an object would be popped off the stack. All of these things add hidden code that by definition isn't accessible (nor is it visible) to the program, which is a problem when you need control over everything that goes on.
Kevin Barry
In short, C++'s main difference is the object oriented approach to programming. This approach is useful in some situations, where data structures and processes can easily be abstracted to 'objects'. However, not all programming projects require this approach. Particularly for lower level OS programming, device drivers, etc.
For those that don't need that approach, C++ doesn't offer much new or useful. The structures it introduces can get in the way, especially when you need lower level control over the program, which is what you are looking for programming OS's.
C programs never let the programmer stray too far from the nuts and bolts, and C code doesn't abstract what is happening in the code, the way that C++ does.
n short, C++'s main difference is the object oriented approach to programming. This approach is useful in some situations, where data structures and processes can easily be abstracted to 'objects'. However, not all programming projects require this approach. Particularly for lower level OS programming, device drivers, etc.
Not true, at least in the Linux kernel where drivers framework, sys fs (and maybe other parts) use OOP..
Last edited by jf.argentino; 01-26-2010 at 03:19 AM.
Reason: syntax error
There's always the possibility that a large portion of the OS development 'crowd' simply prefers using C and assembly in general, just like many userspace developers prefer C over C++. Especially if we are refering to hobby projects.
Last edited by carbonfiber; 01-26-2010 at 03:25 AM.
There's always the possibility that a large portion of the OS development 'crowd' simply prefers using C and assembly in general
IMHO it's not a fashion matter, more something relative to reliability: an easy syntax (like the C syntax) conduct to reliable standard specifications (read: with least shadow corners) => easy compiler writing => less bugs in compiler / compilation behaviour more predictable...
Add to this the history of UNIX that is hardly connected with the C language. And the availability of a C compiler on any hardware you can encounter, and in the embedded computing world it's a must...
So a long list of advantages of C over _ANY_ other programming languages for the low-level programming world...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.