Thread: C, C++ difference - getting confused

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    C, C++ difference - getting confused

    Hi,

    So I'm pretty much fresh out of University (College in US). I did traditional engineering and then specialised in (for the purposes of brevity I'll call it) applied maths.

    Anyway, I've taught myself C++ and some concepts of OOP, although I haven't extensively studied design patterns and rely on my head to come up with a good pattern for the problem I'm facing.

    I currently code in C all day long. I read C code all the time and these days I do little C++ because my time is occupied with my job which uses C. Anyway, I'm getting a bit confused mainly because of the below.


    1. Is C still gaining popularity or is it being replaced by C++? I know a lot of projects and code out there is in C simply because a stable C++ compiler was only released about 10-15 years ago whereas a stable C compiler has existed for a while. I hear thing such as this and it makes me wonder.

    2. What is the niche that C is supposed to fill and that C++ is less suited for? I know C is a lot simpler that C++ and a simpler thing is easier to use even when you are a master on the subject.

    3. What is the problem writing OOP code with C? You can code a single class in a single file.

    4. A lot of people talk about C and C++ as though they are totally different languages. I've heard expression such as "Oh you can't use C++ for that, you need to learn C.". Am I missing something here? Sure the libraries are different and there are some new keywords in C++ that do things that a library does in C but they are v. v. similar to me.

    Pardon me it this has been asked before - I haven't really managed to find answers to such questions.
    Last edited by s5s; 04-01-2013 at 06:47 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I'll try to answer some of those briefly as I understand it.
    1. You still won't find a *good* C++ compiler for a lot of platforms as it is a huge and complicated language to implement. On the other hand, because the language is complicated, the user code becomes much simpler in most cases.
    2. Other than extremely resource constrained systems, I can't think of anything.
    3. Try and see. It becomes a mess, at least that was my brief experience with the GTK+ library, which does some sort of OOP in C.
    4. Some features in C++ have no direct equivalent in C. Consider templates for example. Macros can barely touch what can be achieved with templates.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by s5s View Post
    "Oh you can't use C++ for that, you need to learn C."
    except in cases where you're required to use C, per the project specifications, and on platforms that do not have a C++ compiler, this is a completely false statement. whoever said this to you clearly does not understand the relationship between C and C++.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Thank you. To answer my questions I will put this in (I had read it before):

    When to use C over C++, and C++ over C? - Programmers Stack Exchange
    Is the C programming language still used? - Programmers Stack Exchange

    I sort of knew this. What I didn't know (and didn't get) is why (the hell) is everything in linux written in C?

    I am a supporter of keeping things as simple as possible and since C is a simpler language I thought that they were just using C because small utilities (e.g. coreutils) don't really need to be in C++, so why add the complication and restriction (as mentioned C++ is available on a limited number of systems).

    However, I've also seen very complex software that is written in C, rather than C++ (e.g. opencv).

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by s5s View Post
    C++ is available on a limited number of systems
    I think that the use of "a limited number" here should be replaced with "not all." C++ is available on any platform supported by gcc. and that is a lot of platforms. these days, gcc supports just about any system that you can plug a keyboard and monitor into.

    Quote Originally Posted by s5s View Post
    However, I've also seen very complex software that is written in C, rather than C++ (e.g. opencv).
    C code tends to be more verbose, because the language has fewer built-in abstractions, requiring the programmer to do more.

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    1. Neither C nor C++ are new languages; they've both been around for a fairly long time. In most cases I'd say it comes down to what the people involved with the project enjoy using. Both languages are certainly quite popular.

    2. They're both general purpose programming languages. You can pretty much use them for anything, though I'd say your choice is influenced largely by how you like to program. If you don't program with the object oriented paradigm and you like the procedural paradigm, then C's a pretty good choice. On the other hand, if you don't want to use either paradigm you'd probably select another language altogether.

    3. Indeed, you can do object oriented or even functional programming in C, but the language wasn't designed to be used in either of those ways, so you'd be better of using a language that was.

    4. They are different languages and lumping them together with terms like "C/C++" can only be confusing. The features that are present in C++ and not in C are fairly obvious, but there are features that C++ lacks which C has that are often overlooked; the restrict qualifier, implicit type conversions, recursive calls to main, and designated initializers, for instance. With that said, one language can't really do 'more' than the other, but you do need to use them differently if you want to write good code.

    I sort of knew this. What I didn't know (and didn't get) is why (the hell) is everything in linux written in C?
    Look not only at Linux, but other Unix-like systems like the BSD family and the answer should be obvious. C was designed for Unix.

    EDIT: OpenCV is written in C++, isn't it? I know it can be used with C and C++ (as well as other languages) but I seem to recall it being written in C++.
    Last edited by Barney McGrew; 04-01-2013 at 08:15 AM.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by s5s View Post
    I sort of knew this. What I didn't know (and didn't get) is why (the hell) is everything in linux written in C?
    Because C has a more stable ABI as compared with C++.

    Application binary interface - Wikipedia, the free encyclopedia

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Not everything in Linux is written in C. The Linux kernel is written in C and the person that controls that source code programs in C and doesn't like C++ so the Kernal is in C. However many other applications are written in C++. For example the KDE Windowing system is based on C++ not C. And don't forget that most of the Windows kernel is also written in C.

    Jim

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by s5s View Post
    What I didn't know (and didn't get) is why (the hell) is everything in linux written in C?
    What ?
    Unless you are only talking about the kernel, there is a lot of C++ nowadays.
    In my system, C is used by the kernel and the gnu userspace and a few utilities like git...that makes about 50 packages .
    Now, KDE (about 400 packages, excluding docs and artwork), Firefox , Blender, GCC, clang+llvm, Libre Office, all Linux games I have played are written in C++.

    [Didn't notice the last post before posting..sorry]
    Last edited by manasij7479; 04-01-2013 at 09:50 AM. Reason: ...

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    Not everything in Linux is written in C. The Linux kernel is written in C and the person that controls that source code programs in C and doesn't like C++ so the Kernal is in C. However many other applications are written in C++. For example the KDE Windowing system is based on C++ not C. And don't forget that most of the Windows kernel is also written in C.

    Jim
    Thanks for the answer Jim.

    Yes, exactly - kernels are always written in C. Nobody ever uses C++. I'm not certain why. I mean I know why to a certain extent but I don't know the full list of reasons why C++ is not used. Sure you can't use exceptions, "new", "delete" etc. but templates and classes would be useful, I would have thought.

    A lot of people claim C++ hides function calls. I don't think it hides them. I think it abstracts them. I think C often can hide information (e.g. void pointers, cpp abuse etc.).


    And yes I mean a windowing system is a classic example of where C++ will be used (OOP).

    A good example of a project that converted from C to C++ (complete rewrite) is ns (network simulator). They switched from C to C++ when going from ns-2 to ns-3.

    For future (and current) readers - I'm not comparing C to C++ in absolute terms. I think each language excels in a particular area and I'm trying to find those areas so that I can have a feeling of when to use C and when C++ and spot "mistakes" i.e. when C would have been a better match rather than C++.

    I also think when people say C or C++ they also attach a meaning relating to the purpose of the code rather than the language itself. You could very well write valid C++ code but still call it C because of how it's used (e.g. in a kernel, on a microcontroller etc.).

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Quote Originally Posted by manasij7479 View Post
    What ?
    Unless you are only talking about the kernel, there is a lot of C++ nowadays.
    In my system, C is used by the kernel and the gnu userspace and a few utilities like git...that makes about 50 packages .
    Now, KDE (about 400 packages, excluding docs and artwork), Firefox , Blender, GCC, clang+llvm, Libre Office, all Linux games I have played are written in C++.

    [Didn't notice the last post before posting..sorry]
    Sorry I should have made it more clear - I was referring to GNU/linux software (packages). I wasn't referring to other projects (such as Gnome, KDE, XFCE etc) which can be in any language.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Honestly, you never need to use C. You can write a C-like program, compile it with a C++ compiler, and it will likely have fewer bugs than the equivalent C program. C++ does much stronger type checking, so that when you're casting from one pointer type, it will yell at you if you don't do an explicit cast.

    It would be a mistake to say that kernels are never written in C++. You certainly can use exceptions and new/delete, but you have to set up for them first. There is no standard library for kernels. You would have to write it yourself.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by s5s
    4. A lot of people talk about C and C++ as though they are totally different languages. (...) Sure the libraries are different and there are some new keywords in C++ that do things that a library does in C but they are v. v. similar to me.
    The way I see it, unless you need to write C code that is compatible with or even compilable as C++ (or vice versa), you should treat both as different languages since the different features lead to different idioms and such.

    Quote Originally Posted by s5s
    Yes, exactly - kernels are always written in C. Nobody ever uses C++.
    I think matsp or someone else worked on a system for their day job where the kernel was written in C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes, exactly - kernels are always written in C. Nobody ever uses C++.
    This is not necessarily true. Yes Linux is in C, for the previous stated reasons. And Windows was written in C because Windows predates C++. Probably one of the biggest stumbling blocks to creating an operating system from C++ is the size of the language it's self. C is a much smaller language, therefore easier to implement. But even historically C based programs are now being converted to C++, for example gcc is now being compiled as a C++ program.

    Sure you can't use exceptions, "new", "delete" etc. but templates and classes would be useful, I would have thought.
    Trying to use templates in a kernel can be fraught with danger. Remember a template is compiled only when it is actually used. And classes can also be problematic for a kernel, remember when writing a kernel you will usually not have the full language available. Many of the standard functions require support from the kernel so until the kernel initializes all the required components that are required for standard functions they are not available.

    Jim

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by s5s View Post
    Sorry I should have made it more clear - I was referring to GNU/linux software (packages). I wasn't referring to other projects (such as Gnome, KDE, XFCE etc) which can be in any language.
    I can't see any difference.
    There is no standard "GNU/Linux software packages", just a bunch of distinct projects .
    And Android's user space for Linux, other than the few components they did not bother to reinvent , is almost entirely written in Java.
    You need to get over the mentality that something can be written only in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused with cin.get()
    By Shal in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2006, 07:27 AM
  2. confused
    By mopar123 in forum C Programming
    Replies: 18
    Last Post: 09-01-2005, 04:06 PM
  3. Confused
    By Clyde in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2004, 02:57 PM
  4. confused
    By luckyboy23 in forum C++ Programming
    Replies: 10
    Last Post: 04-30-2003, 06:09 AM
  5. I am so confused
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2002, 10:17 PM

Tags for this Thread