Difference between C, C++, and C#

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 9486 Views - Last Post: 29 August 2010 - 11:28 PM

#1 nana1611  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-January 10

Difference between C, C++, and C#

Posted 18 August 2010 - 05:37 PM

What is the major difference between the C, C++, and C# programming languages?
Is This A Good Question/Topic? 0
  • +

Replies To: Difference between C, C++, and C#

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Difference between C, C++, and C#

Posted 18 August 2010 - 05:49 PM

Im sure google is full of information on this. But the main difference between C# and C/C++ is that C# is Microsoft, which means its windows dependent. C/C++ is standard across all platforms

This post has been edited by ImaSexy: 18 August 2010 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Difference between C, C++, and C#

Posted 18 August 2010 - 06:04 PM

Moved to the Corner Cubicle.

C is a fairly low-level procedural language. C++ was designed to add the Object-Oriented paradigm to C. It is a patch-together job, and I believe you can work procedurally or using OOP in C++. C# is a Microsoft product, and is a response to Java. It is high level and forced OOP, abstracting concepts like pointers in C and C++.
Was This Post Helpful? 0
  • +
  • -

#4 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: Difference between C, C++, and C#

Posted 18 August 2010 - 07:50 PM

C and C++ are compiled directly into machine language programs as standard binaries. C# runs on top of a virtual environment and has higher level features.
Was This Post Helpful? 1
  • +
  • -

#5 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Difference between C, C++, and C#

Posted 18 August 2010 - 07:58 PM

C++ and C# - Object Oriented
C - Not OO
Was This Post Helpful? -1
  • +
  • -

#6 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: Difference between C, C++, and C#

Posted 18 August 2010 - 08:01 PM

*
POPULAR

View PostCrunch, on 18 August 2010 - 07:58 PM, said:

C++ and C# - Object Oriented
C - Not OO


Wrong.



OO is a programming style, not a language. Language implements features, but ultimately it is a style you program and design in.

This post has been edited by WolfCoder: 18 August 2010 - 08:03 PM

Was This Post Helpful? 5
  • +
  • -

#7 Nikitin  Icon User is offline

  • D.I.C Regular

Reputation: 56
  • View blog
  • Posts: 264
  • Joined: 02-August 10

Re: Difference between C, C++, and C#

Posted 18 August 2010 - 09:43 PM

The paradigm of a programming language depends on its features. C does not provide built-in facilities for OOP, therefore, it's not Object Oriented. Simple as that.
Was This Post Helpful? -4
  • +
  • -

#8 guahguahmonster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 209
  • Joined: 29-August 07

Re: Difference between C, C++, and C#

Posted 19 August 2010 - 02:25 AM

View PostWolfCoder, on 18 August 2010 - 07:01 PM, said:

OO is a programming style, not a language. Language implements features, but ultimately it is a style you program and design in.


View PostNikitin, on 18 August 2010 - 08:43 PM, said:

The paradigm of a programming language depends on its features. C does not provide built-in facilities for OOP, therefore, it's not Object Oriented. Simple as that.


But I'd like to argue that it's not as simple as that!

It's true that C doesn't have the concept of objects built in and therefore you can't make use of such concepts as inheritance, polymorphism, and encapsulation, but then again, you still can take some concepts from object-oriented programming and use them in C! There's nothing stopping you from doing that.

You already have structs, which can act as objects that hold state. It's true that you can't have member functions (methods) in C, but you can emulate it with functions that take a struct as an argument, or you can be very very hacktacular and use function pointers in your structs. I dug up two articles to illustrate this point.

I'll agree that C isn't an object oriented programming language, but, I'd argue that you can still do object oriented programming using C. Is it the best tool for the job? Probably not, but the fact remains that it is possible!

This post has been edited by guahguahmonster: 19 August 2010 - 02:26 AM

Was This Post Helpful? 0
  • +
  • -

#9 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Difference between C, C++, and C#

Posted 19 August 2010 - 02:35 AM

Uh, since when do polymorpism and encapsulation require an object system?
Was This Post Helpful? 4
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Difference between C, C++, and C#

Posted 19 August 2010 - 05:54 AM

out of curiousity, if a language doesn’t have objects, how can it be object oriented?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Difference between C, C++, and C#

Posted 19 August 2010 - 06:17 AM

View PostRaynes, on 19 August 2010 - 05:35 AM, said:

Uh, since when do polymorpism and encapsulation require an object system?

Data is encapsulated in a function, encapsulated in a module (ie., Erlang), etc. You can still overload functions, which is polymorphism.
Was This Post Helpful? 0
  • +
  • -

#12 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Difference between C, C++, and C#

Posted 19 August 2010 - 06:29 AM

C *isn't* object oriented. But, with the facilities it provides, you can code in an object oriented style to whatever extent if you want to.

Clojure isn't object oriented, but you can still have polymorphism. As a matter of fact, using multimethods, it can be more flexible than object oriented languages. I always note with a smirk that my Irclj library is actually written in an object oriented style. It's not completely true, but it does hold some truth. The thing that Irclj returns when you connect is a record held in a ref (a synchronous, coordinated mutable variable of sorts), which is essentially an "object".

The combination of protocols (similar to interfaces) and records (which create Java classes under the hood and can implement those protocols and even existing Java interfaces), and multimethods (more flexible than all of those, since it can dispatch on anything and on the result of an arbitrary function). In Clojure world, we call this "abstraction-oriented programming". It isn't OO, but it borrows some of the same basic principles.

I'm sure somebody else can elaborate more on how one does OO-like things in C. I, unfortunately, don't know the language.

@macosnerd101 Indeed! Namespace/module systems and closures encapsulate. You're also dead on with the polymorphism bit, but as I pointed out above, it goes even deeper (much deeper) than that. Even Haskell has type-classes and stuff.

This post has been edited by Raynes: 19 August 2010 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#13 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: Difference between C, C++, and C#

Posted 20 August 2010 - 11:57 AM

"out of curiousity, if a language doesn’t have objects, how can it be object oriented?"

By programming it that way. I do this all the time in C to keep my sanity, for example, I have image objects in my programs and I use them. The only difference is that I decide to play by the rules of OOP and I rarely ever do anything to the contrary.

"C does not provide built-in facilities for OOP, therefore, it's not Object Oriented. Simple as that."

Actually, I write OOP style code in C all the time to make life easier on me. However, I can switch to different paradigms anywhere in my program to fit the task. Simple as that. OOP is my default paradigm. PARADIGM SHIFT!!

"It's true that C doesn't have the concept of objects built in and therefore you can't make use of such concepts as inheritance, polymorphism, and encapsulation"

I also do these too. It's actually really easy to get away with some things that make writing these in C easier than JAVA (but it also makes it easier to mess up). I use encapsulation all the time, you could easily figure a way to break encapsulation in my code in C, but then it's your funeral. I use inheritance, for example, I have a special object that has all the abilities an ENTITY has, but it is designed for the sole purpose of teleporting the player to different maps so I can make quick link ENTITIES. My items database will have inheritance too, so items and extend each other for quick creation and management. And if you really understood the low level nature of C, you can pretty much do all sorts of neat polymorphism tricks.

The difference between JAVA/C# and the above is that I designed rules and coding methods to do OOP. I decided exactly how the OOP will work in my code vs. it having been designed and given to me. JAVA/C# has these features all built in, but I'm stuck with them too. This should show that you really have to understand all these kinds of languages to pick the right one for whatever it is you are doing.

"I'm sure somebody else can elaborate more on how one does OO-like things in C. I, unfortunately, don't know the language."

The quickest and easiest way is to use a struct for the instance data, pointers to the struct as the object (in JAVA, everything is a reference, take note of this). ____.c contains all the methods and globals (globals in a .c file behave just as static instance variables in a class in JAVA). You have extern defines of anything you want public in the ____.h file. I like to put large, informative comment blocks in the ____.h file too. I like managing my own memory, you might not, but you have to. Have a constructor and destructor method too. From here I want you to figure out all the fun features you say C doesn't have. You'll also understand the no-fences approach of C.

But you're free to change this style to your liking.

It's actually fun to do all the things you can do in OOP, but it requires you have a very strong knowledge of the machine, C, and your favorite higher level OOP language (like JAVA). All the better, you'll become a stronger C and higher level OOP programmer. Understanding all of this enables even your JAVA programs to run faster.

There is a funny saying "A good programmer can write ____ in any language!". Insert favorite language here.

This post has been edited by WolfCoder: 20 August 2010 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#14 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Difference between C, C++, and C#

Posted 20 August 2010 - 12:11 PM

I do OOP in C, it's not very hard. I also use references for everything so I guess in a way I'm really just emulating Java.

// This is what I consider an object in C.
typedef struct
{
    int foo;
} MyClass;

// Arraylist reference, create this later.
static int list = -1

...

int MyClassNew()
{
    MyClass *my = malloc(sizeof(MyClass));
    return dgArrayListPush(list, my);    // Returns a reference to the new class.
}

void MyClassSetFoo(int ref, int foo)
{
    MyClass *my = dgArrayListGetItem(list, ref);
    my->foo = foo;
}

...
int main(int argc, char **argv)
{
    // In C++: MyClass *my = new MyClass();
    int my = MyClassNew();

    // In C++: my->setFoo(6);
    MyClassSetFoo(my, 6);

    return 0;
}



Basically the only difference between OOP in C and OOP in Java or C++ is a little bit of boilerplate code, and that class methods aren't "in" the class (although you could certainly get around this with function pointers if you want to, as I think WolfCoder was touching on).

I think C can be a bit of a functional language as well (depending on your compiler). GCC lets you use nested functions and callbacks, so essentially you can do things like this.

void foo()
{
    int add(int a, int B)/>
    {
        return a + b;
    }

    doSomethingWithThis(add);
}


Was This Post Helpful? 2
  • +
  • -

#15 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: Difference between C, C++, and C#

Posted 20 August 2010 - 12:27 PM

All C needs is recursive macros and you can have the same sort of fun you can in Lisp-like languages that support macros. Of course not being supported in all compilers can break your portability.

This post has been edited by WolfCoder: 20 August 2010 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3