Is there anything C can do that C++ cannot do?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 8280 Views - Last Post: 02 August 2011 - 07:54 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Is there anything C can do that C++ cannot do?

Posted 30 July 2011 - 04:41 AM

Is there anything C can do that C++ cannot do?
Is This A Good Question/Topic? 0
  • +

Replies To: Is there anything C can do that C++ cannot do?

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Is there anything C can do that C++ cannot do?

Posted 30 July 2011 - 05:15 AM

No.

But the same could be said of most general purpose programming languages.
Was This Post Helpful? 3
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Is there anything C can do that C++ cannot do?

Posted 30 July 2011 - 05:42 AM

Well it depends on how you define a thing one can do. If we only talk about statements like "I can write an application which does XYZ in language A, but not in language B", then baavgai already said all there is to say: For most general purpose languages A and B there will be no value of XYZ for which the above statement is true.

However if we talk about things you can do in the code, there are a few. As one trivial example you can call your variables and functions "new" or "delete" (or any other C++-only keyword) in C, but not in C++.

A maybe a bit more useful example is that you can implicitly convert a void pointer to any other pointer type in C, whereas C++ requires an explicit cast. More such examples are listed in this wikipedia article.
Was This Post Helpful? 1
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Is there anything C can do that C++ cannot do?

Posted 30 July 2011 - 08:43 AM

Interesting. Thanks guys.
Was This Post Helpful? 0
  • +
  • -

#5 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 01:43 AM

c++ inherit from c !!! answer your question : NO !
Was This Post Helpful? -1
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 01:45 AM

More correctly, C++ is a superset of C, and C is a subset of C++.
All C will work in a C++ compiler (unless it uses horrible practice like default int).
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 04:56 AM

View PostPlasticineGuy, on 31 July 2011 - 10:45 AM, said:

More correctly, C++ is a superset of C, and C is a subset of C++.


Not quite.

Quote

All C will work in a C++ compiler (unless it uses horrible practice like default int).


What about the examples in my post?
Was This Post Helpful? 1
  • +
  • -

#8 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 05:19 AM

C++ built on C base !!! and all of C code compile on C++ .
Herb Schildt's C++ programming cookbook page 279
"Because C++ was built on C, C++ also includes the entire C
file system"

This post has been edited by ahura_24: 31 July 2011 - 07:09 AM

Was This Post Helpful? -5
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 05:40 AM

Well one way to look at it is this: C is essentially a sub-set of C++ meaning that a C++ compiler can compile standard modern C code. There are a few minor differences in syntax here and there but not a whole lot.

Looking the other way: Can C++ do anything C can't?
Well the Comeau C++ compiler compiles C++ into C and then uses a C compiler to generate the executable code. So -- there is nothing that can be done in one language that can not functionally be done in another.


Now syntax wise there are somethings that you can do in C++ such as overload operators that can not be done in C. This is however merely "syntactic sugar" - meaning that overloading operators might let you make you code look nice being able to say concatenate strings like:

filename = base + ext;

vs.

concat(filename, base);
concat(filename, exe);


On the other side C is a little looser with the syntax and so strictly speaking you can write some programs in C that do not meet with the C++ standard and technically should not compile. HOWEVER, most C++ compilers do actually allow you to bend the rules and compile these programs.

So there are differences but they don't have anything to do with functionality but with syntax. Either language can do anything that the computer is capable of doing.
Was This Post Helpful? 1
  • +
  • -

#10 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 06:24 AM

View Postahura_24, on 31 July 2011 - 07:19 PM, said:

!

Honestly?

@Nick, I thought C++ has OOP?
Was This Post Helpful? 0
  • +
  • -

#11 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 06:35 AM

Yes, of course C++ has OOP (it is multi-paradigm, you can still write procedural code as C with it). C++ was named 'C with Classes' in the beginning (of course is much more than this now).
Was This Post Helpful? 1
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Is there anything C can do that C++ cannot do?

Posted 31 July 2011 - 06:53 AM

C++ does have OOP but what is that? You can do OOP in C. The difference is that C does not offer "compiler support" for OOP. So while you can do OOP in C it is really very aggravating to pull off since the compiler is not assisting.

Or... you could use the Comeau C++ compiler to compile your C++ into C. -- note. I have always wondered how readable the code generated by Comeau is. Its only a 50$ compiler... maybe I will purchase it and see. One of its nice uses (as I understand it) is that on platforms such as micro-controllers that only have C you can write C++ that gets compiled down to C -- I have never really seen this is action but I like the idea.
Was This Post Helpful? 1
  • +
  • -

#13 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Is there anything C can do that C++ cannot do?

Posted 01 August 2011 - 05:50 AM

You can do OOP in C? Cool. :)
Was This Post Helpful? 0
  • +
  • -

#14 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Is there anything C can do that C++ cannot do?

Posted 01 August 2011 - 05:56 AM

View Posthulla, on 01 August 2011 - 02:50 PM, said:

You can do OOP in C? Cool. :)

There's a series of tutorials about that somewhere on DIC. I'll see if I can find it.

Edit: Found it. Part I, II and III.

This post has been edited by Hezekiah: 01 August 2011 - 06:05 AM

Was This Post Helpful? 1
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Is there anything C can do that C++ cannot do?

Posted 01 August 2011 - 06:44 AM

true OOP is hard to pull off in C because it requires a consistency that is hard to maintain. For that reason one ends up writing complicated Macros in CPP (C pre processor) or using other tools to aid in consistent code generation. It is a terribly fun exercise but in reality what you are doing is designing your own language to function as a front end to C... my experience is that unless you start out with that goal of writing a compiler to C -- you end up with a mess.

Not to mention that there are other features of the C++ compiler that you miss out on such as inlining member functions and optimizations.

I do find OOP-ish-ness in C programming to be a general benefit. For example If I am writing say a linked list I might adopt a naming convention to keep all of the related functions together into a kind of logical package. But rather than get into virtual functions and function pointers for polymophism I tend to stick more with the traditional C polymophism techniques such as enums and unions. -- its far less versatile than C++ but easier to keep strait in code.

If you really want to do OOP in C -- use C++ (or write your own language that compilers to C and try hard to avoid re-inventing ObjectiveC).
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2