Thread: Why both C and C++

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    Why both C and C++

    Sorry I know this is a dumb question, but I haven't been able to figure out an answer from my internet research.

    Since C++ is a Object oriented language and C is not, why are some systems written in both? Many thanks.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    C came out a long time before C++, so everything was originally just written in C. Then people started writing new code in C++ since it's much easier and cleaner than C. Some things that do really low level tasks like drivers are written in C (or even assembly) for efficiency...

    If you're asking because you want to know when to use C and when to use C++, I'd use this formula:
    If you can write it in C++, then write it in C++.
    If you're stuck with using C, well, what can you do? Use C.
    If you write something in C++ and you can prove that you have a serious performance issue, try to fix it in C++; if that doesn't help, re-write parts of it in C. If that's still not good enough, the next level down is assembly.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    Thanks so much for your response cpjust. That's very helpful and I will sure remember that.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    C is a much simpler language - not necessarily to learn to a working knowledge level, but definitely to write a compiler for. C is typically the first language ported to an unknown system.

    C generates simpler code. With C++, there's constructor calls, operator calls, exceptions, RTTI, and so on. This additional, "hidden" code is undesirable under some circumstances, e.g. when writing low-level kernel code or device drivers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Many OS's are written in C, mostly because the core OS code was written when C++ was just starting out as a language, and compilers where not very reliable (if at all available). I don't know if this has changed in Vista, but XP certainly have no direct support for C++ in drivers. You can make drivers from C++ code, but it's a fair amount of hard work to "adapt" the C++ code to work in a driver, and there are several restrictions, such as "no global objects" because there is no way to make the constructor calls, and no C++ style try/catch exceptions.

    I haven't seen any Linux C++ drivers.

    Writing low-level code in C++ is often harder than doing it in C (unless you just use the C++ compiler to compile straight C-code).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Once a C compiler has been built for a new system, isn't it just a matter of taking the source code for a C++ compiler and building it with your new C compiler, or does a C++ compiler have some platform specific code that needs to be ported? i.e. are C++ compilers written purely in C?

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    9
    C++ is a superset of C, it was made before C. Why use C? Many reasons. One reason is C can typically is closer to assembly, and was much more widely used back in the day. C is procedural. Some programmers don't like object oriented code (I've met a few), so they use C.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Jack Walters View Post
    C++ is a superset of C, it was made before C.
    Bzuh?

    C is a good decade or two older than C++.


    Quote Originally Posted by cpjust
    Once a C compiler has been built for a new system, isn't it just a matter of taking the source code for a C++ compiler and building it with your new C compiler, or does a C++ compiler have some platform specific code that needs to be ported?
    The absolute minimum you have to do when porting C++ to a new platform is implementing the stack unwinding required by exception handling. This is not entirely portable, since different architectures work differently.

    However, you'll also want to consult the architecture reference for things like alignment (OK, C suffers from that, too), and you'll have to port the generated code for things like virtual dispatch and runtime type identification.
    It is true, though, that the bulk of the porting work is writing the new code emitter, which can usually be shared between C and C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There's usually a portion of C++ library to port too, before you can write apps in C++. Yes, 95-99% of that is written in portable C or C++, but some small portions need to be written in non-portable assembler or non-portable C/C++ code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    Thanks so much everyone! That would help my interview.

Popular pages Recent additions subscribe to a feed