LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2010, 01:33 PM   #1
kc3
Member
 
Registered: Jun 2009
Distribution: Linux from Scratch
Posts: 172

Rep: Reputation: 35
Hm... Why C instead of C++???


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?
 
Old 01-24-2010, 01:45 PM   #2
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Yes
 
0 members found this post helpful.
Old 01-24-2010, 01:51 PM   #3
bret381
Member
 
Registered: Nov 2009
Location: Alabama
Distribution: Arch x86_64
Posts: 650

Rep: Reputation: 79
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.
 
Old 01-24-2010, 01:54 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 01-24-2010, 03:32 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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.
 
Old 01-24-2010, 08:39 PM   #6
kc3
Member
 
Registered: Jun 2009
Distribution: Linux from Scratch
Posts: 172

Original Poster
Rep: Reputation: 35
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.

Last edited by kc3; 01-24-2010 at 08:42 PM.
 
Old 01-24-2010, 10:08 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 01-25-2010, 05:23 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
There's an interesting entry in the Linux Kernel Mailing List FAQ about this subject.

This includes the following thoughtful explanation from Linus himself:
[...]
It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.
[...]
 
Old 01-25-2010, 09:43 AM   #9
Xyro
LQ Newbie
 
Registered: Aug 2009
Location: Canada
Distribution: Ubuntu 9.04
Posts: 22

Rep: Reputation: 19
Why on earth would you build the foundation of your system with duplo blocks? It is all about fine touch.
 
Old 01-25-2010, 05:57 PM   #10
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,131

Rep: Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302
There's a (very brief) critique of C++ by a man who spent 5 years writing compilers for it:
http://fontforge.sourceforge.net/faqFS.html
 
Old 01-25-2010, 07:41 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by DavidMcCann View Post
There's a (very brief) critique of C++ by a man who spent 5 years writing compilers for it:
http://fontforge.sourceforge.net/faqFS.html
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
 
Old 01-26-2010, 01:23 AM   #12
Borax_Man
Member
 
Registered: Jan 2009
Posts: 31

Rep: Reputation: 16
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.
 
Old 01-26-2010, 03:18 AM   #13
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Quote:
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
 
Old 01-26-2010, 03:22 AM   #14
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
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.
 
Old 01-26-2010, 01:50 PM   #15
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Quote:
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration