7

C++, as the name suggests, is a superset of C. As a matter of fact, C++ can run most of C code while C cannot run C++ code.

There are several advantages with c++ compared with c - for instance

  • data can not be hidden in c language
  • c is more low level (which mean harder to understand and code - and that means more bugs)
  • c does not allow function overloading
  • c does not support exception handling
  • you can use functions inside structures in C++ but not in C

This list could certainly be much longer - but here comes my question: Is there ANY advantage with c-langauge compared with c++? Is there anything whatsoever that is better with c than with c++? Does c have anything that c++ lacks?

I do not know about this at all - but could c possibly be slighty faster than c++ due to fewer instruction-sets? A low-level language would possibly require fewer instructions by the processor.

Björn Hallström
  • 3,775
  • 9
  • 39
  • 50
  • 12
    It's not a superset of C. And its name implies that it is 1 more than C (unless C is the maximum representable value for its type). – Joseph Mansfield Jan 01 '14 at 22:48
  • support in embedded systems and/with custom compilers comes to mind – im so confused Jan 01 '14 at 22:48
  • 7
    It's name implies that after evaluating, C is one more than C++ :) – bennofs Jan 01 '14 at 22:49
  • 2
    This question is so broad, I wanted to give you an answer, but don't even know where to begin. – Filipe Gonçalves Jan 01 '14 at 22:49
  • I really like C vs C++ comparisons, but I think that C and C++ are pretty different languages. – Yick Leung Jan 01 '14 at 22:49
  • "c is more low level" that can be seen as an advantage, depending on the context. – SirDarius Jan 01 '14 at 22:49
  • `C++, as the name suggests, is a superset of C.` No, it's not. `data can not be hidden in c language` Yes, it can. – Lightness Races in Orbit Jan 01 '14 at 22:54
  • 1
    @sftrabbit, If C is unsigned and reaches its maximum power, C++ will be 0 :( – chris Jan 01 '14 at 22:55
  • 3
    I wouldn't really say C is much more low level, either. C++ gets down to the nitty-gritty just as well if you need it to. – chris Jan 01 '14 at 22:57
  • 4
    C++ is not a strict superset of C. There are valid C programs that are invalid C++, and there are valid C programs that are valid C++ programs different semantics. It's possible, and not too difficult, to write code that's valid C and C++ with the same semantics -- but there's rarely a good reason to do so. – Keith Thompson Jan 01 '14 at 23:00
  • @sftrabbit The initial C++ compiler was a translator that took the C++ source code, translated it into C source code and then compiled. So at that time, _Anything_ C++ could do, C could also, but not necessarily the other way around, making C++ a functional _subset_ of C. – chux - Reinstate Monica Jan 01 '14 at 23:08
  • Possible duplicate of http://stackoverflow.com/questions/497786/why-would-anybody-use-c-over-c – Brandin Jan 01 '14 at 23:11
  • I might be going too far with this one, but you can overload functions based on the number of parameters in C by using variadic macros. See `BOOST_PP_OVERLOAD`, but substitute a normal function for the overloaded macro. – chris Jan 01 '14 at 23:11
  • "Is there any advantage" - you just listed all the reasons! – Kerrek SB Jan 01 '14 at 23:24
  • "c is more low level (which mean harder to understand and code - and that means more bugs)" - Well I'mnot convinced that C is more "low level" than C++, but regardless, is the conclusion true? I am experienced in both languages, and let me tell you; some C++ features allow for rather nasty, extremely subtle bugs to slip through which would be impossible in C. Sounds like inexperience talking to me. – Ed S. Jan 01 '14 at 23:29
  • This shouldn't have been closed in the first place. – tbodt Jan 01 '14 at 23:44

1 Answers1

10

In simple, C and C++ are two different languages.

C++, as the name suggests, is a superset of C

No. This is not true. C++ is not a superset of C..

Is there ANY advantage with c-language compared with c++? Is there anything whatsoever that is better with c than with c++?

  • Static initialize is safe in C but not in C++, because in C++ static initialization can cause code to run, which depends on other variables having been statically initialized. It can also cause cleanup code to run at shutdown which you can't control sequence of (destructors).

  • C gives you better control over what happens when your code is executed. When reading seek out it is fairly straightforward to decipher one code is getting executed and when memory is just restart or primitive operations are performed.

  • C supports variable sized arrays on the stack. Which is much faster to allocate than on the heap. (C99 feature)

  • No name mangling. If you intend to read generated assembly code, this makes that much easier. It can be useful when trying to optimize code. De facto standard application binary interface (ABI). Code produced by different compilers can easily be combined.

  • Much easier to interface with other languages. A lot of languages will let you call C functions directly. Binding to a C++ library is usually a much more elaborate job.

  • Compiling C programs is faster than compiling C++ programs, because parsing C is much easier than parsing C++.

  • Varargs cannot safely be used in C++. They're not entirely safe in in C either. However they're much more so in the C++, to the point that they are prohibited in the C++ coding standards (Sutter, Alexandrescu).

  • C requires less runtime support. Makes it more suitable for low-level environments such as embedded systems or OS components.

  • Standard way in C to do encapsulation is to forward declare a struct and only allow access to its data through functions. This method also creates compile time encapsulation. Compile time encapsulation allows us to change the data structures members without recompilation of client code (other code using our interface). The standard way of doing encapsulation C++ on the other hand (using classes) requires recompilation of client code when adding or removing private member variables.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    Although I agree with the first half (about C and C++ beeing different), it's not answering the question.. – stefan Jan 01 '14 at 22:55
  • 1
    @LightnessRacesinOrbit; The two lines answers all the questions OP asked. The answer to this topic can be vast. – haccks Jan 01 '14 at 22:58
  • 3
    Yes, it's vast, which is why it's closed as "too broad": the precise opposite of what _you_ did, which is to give a placeholder non-answer that basically just says "don't bother with this question". – Lightness Races in Orbit Jan 01 '14 at 22:58
  • 2
    I do hope you did not mean `C` programs run faster than `C++` programs after they are built... – StoryTeller - Unslander Monica Jan 01 '14 at 23:11
  • 4
    "Compiling C programs is faster than compiling C++ programs, because parsing C is much easier than parsing C++." Have you ever measured that? I seriously doubt that you can present a file for which this actually holds. – stefan Jan 01 '14 at 23:19
  • 6
    @haccks, many of the "advantages" you listed are your opinion or can't be proven. "C supports variable sized arrays on the stack. Which is much faster to allocate than on the heap. (C99 feature)" Yeah but the stack is limited in size, so is this really an advantage? – Miguel Jan 01 '14 at 23:42
  • 1
    @Miguel; That's why I never compare two languages with each other. – haccks Jan 01 '14 at 23:44
  • 1
    @haccks, except just now :P – StoryTeller - Unslander Monica Jan 02 '14 at 10:15
  • 3
    Thats a pitty that this question was put on hold - and I dont understand why my question was heavily downvoted - at the most -3. And your answer should be more upvoted!!! Very good answer!!!! @haccks - thanks!!!!! – Björn Hallström Jan 05 '14 at 23:33
  • 2
    Yeah this answers to 3 of the 4 questions asked from where I see it. Why the criticism? Seems neutral and informative enough this one. – Ric Jafe Mar 13 '14 at 11:58