Page 1 of 2

C++ to C

Posted: Tue May 22, 2012 2:30 pm
by Benjamin100
So, I learned python scripting a little bit, and then got into C++ programming.
I've been using C++ for, I think, a little over a year now, starting to use graphics libraries with it.

I was wondering, what is the difference between C and C++?
I thought it was just that C didn't have classes and C++ did.
If that is the only difference, then why all the debate over using C vs C++?
I mean, why use C?
Are there any other substantial differences aside from classes?

Is C more efficient?
And if so, then why?

Do you think I would benefit greatly in learning to use C?
Would it be easy after learning C++?

Thank you,

-Benjamin

Re: C++ to C

Posted: Tue May 22, 2012 4:33 pm
by dandymcgee
Google is your friend. At least until Falco shows up to edumacate your ass.

Re: C++ to C

Posted: Tue May 22, 2012 4:47 pm
by bbguimaraes
Well, C++ is a superset of C, which means anything you can do in C, you can do in C++. What C++ adds is two main programming paradigms: object-orientated programming and generic programming. Also, a standard library and a standard template library (a.k.a. STL).

edit

Just thought it'd be good to say that this is by no means a complete explanation, just a ((very) quick) introduction.

Re: C++ to C

Posted: Tue May 22, 2012 6:13 pm
by Falco Girgis
bbguimaraes wrote:which means anything you can do in C, you can do in C++.
Not 100% accurate. There are quite a few little nuances between the two. Novice C and C++ developers may not be aware of them, but more advanced users of both languages definitely need to be familiar... It can be different enough that the C++ standard provides an

Code: Select all

extern "C"
pragma.
bbguimaraes wrote:What C++ adds is two main programming paradigms: object-orientated programming and generic programming. Also, a standard library and a standard template library (a.k.a. STL).
C++ adds its OWN standard library, but C already has a standard library.
Benajmin100 wrote:I thought it was just that C didn't have classes and C++ did.
While there is definitely more to it than that, that's still a GIGANTIC difference. No polymorphism, encapsulation, or inheritance. That's an entire programming paradigm that the C language does not support natively (although you can still mimic each of these within the C language).
Benjamin100 wrote:I mean, why use C?
I will answer that by answering your next questions:
Benjamin100 wrote:Is C more efficient?
And if so, then why?
Yes. C code tends to be faster than the equivalent C++ code for a variety of reasons. Most of C++'s high-level object-oriented mechanisms have some sort of overhead associated with them. The most classic example is the vtable introduced by virtual functions. Each call to a virtual function requires a lookup table to invoke the correct "version" of the method depending on the type the object was instantiated as. When you get into some of the higher-level features of C++, especially the optional features, like exceptions and RTTI, you really start to introduce quite a bit more overhead.

There is also a shitload more overhead associated with an object-oriented mindset rather than with C++ as a language. Classes tend to have constructors, destructors, copy constructors, etc, whereas a structural paradigm in C uses straight initializers. C++ favors encapsulating the shit out of datatypes with additional "accessor methods" that provide function overhead that normally isn't present in C. There are plenty more examples of this, but I am currently high on hydrocodone, so I will stop here.

C is best suited for applications that are extremely low level and/or require very efficient resource management. It is most often used with microprocessors, drivers, and operating systems. All of these tasks would be a pain in the ass to write in C++, because of their low-level nature.

Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
Benjamin100 wrote:Do you think I would benefit greatly in learning to use C?
Would it be easy after learning C++?
It depends on what you want to do. In the modern world, I think we are beginning to see the end of C's lifetime for your average desktop application. Languages like Java, C#, and C++ (with frameworks like QT) are going to be much better suited.

With that being said, the C language is still going to be king for a loooong time in the arenas of embedded systems, drivers, operating systems, and anything that is either extremely close to hardware or needs to be extremely fast.

Would YOU personally benefit from learning C? Probably not. But there are many people out there who honestly would. I work with the C language every day at work. I interface low-level C drivers for various chips with our C++-based embedded OS. It takes extensive knowledge of both languages and both programming paradigms. My hobbies are all either low-level game development related or are microprocessing related, where I am either writing C or am very closely interacting with it from C++.

Another interesting application of C that is emerging is using it as a wrapper language. Once I learned Objective-C, I thanked god almighty that I was well-versed in ANSI C. I was easily able to write minimal Obj-C and write the majority of my application(s) in C++. Then C++ and ObjC can communicate through a thin C wrapper. The same is becoming true for Java. Look at the NDK for Android. If you know what you're dong with C, you can get Java and C++ talking to each other...

Re: C++ to C

Posted: Tue May 22, 2012 7:00 pm
by bbguimaraes
Exactaly what I'd say, if I was giving a long answer.
Falco Girgis wrote:
bbguimaraes wrote:which means anything you can do in C, you can do in C++.
Not 100% accurate. There are quite a few little nuances between the two. Novice C and C++ developers may not be aware of them, but more advanced users of both languages definitely need to be familiar... It can be different enough that the C++ standard provides an

Code: Select all

extern "C"
pragma.
That doesn't mean it can't do anything C can, but I get what you're saying. I was careful not to mean, and it's important to say, "you can use C code in C++ without any changes".

Re: C++ to C

Posted: Tue May 22, 2012 7:33 pm
by Ginto8
Falco Girgis wrote:Would YOU personally benefit from learning C? Probably not. But there are many people out there who honestly would. I work with the C language every day at work. I interface low-level C drivers for various chips with our C++-based embedded OS. It takes extensive knowledge of both languages and both programming paradigms. My hobbies are all either low-level game development related or are microprocessing related, where I am either writing C or am very closely interacting with it from C++.

Another interesting application of C that is emerging is using it as a wrapper language. Once I learned Objective-C, I thanked god almighty that I was well-versed in ANSI C. I was easily able to write minimal Obj-C and write the majority of my application(s) in C++. Then C++ and ObjC can communicate through a thin C wrapper. The same is becoming true for Java. Look at the NDK for Android. If you know what you're dong with C, you can get Java and C++ talking to each other...
I would also like to point out that, even if you deal primarily high-level languages, a minimal (at least) understanding of C can be very beneficial if for no other reason than that it is everywhere. Even if you disregard embedded systems and interfacing with other languages, almost all of the basic utilities (and libraries) of your computer are written in C. When writing in a high-level language, it's likely that the compiler/interpreter is itself written in C. Those fancy libraries your language has convenient wrappers for? Again, C. So although you may rarely need to deal with it directly, it's important to have some sort of understanding so that you know how to deal with things when the abstractions leak.

And from a very interesting blog post, In Defence Of C:
Some day you’ll need to go spelunking into the depths of your runtime environment. Maybe you’ll need to call some other C-based API for which you don’t have a convenient wrapper in your high-level language of choice. Like, say, mmap-ing a part of a file instead of the whole thing. Or maybe you’ll just want a bit of a performance boost. And on that day, boy will you wish you knew C.
C is worth knowing, even if it isn't absolutely necessary.

Re: C++ to C

Posted: Tue May 22, 2012 10:06 pm
by bbguimaraes
Yeah, I don't know of any language that makes you understand how computers (and thus your programs) work better than C and assembly. And that's knowledge you ARE going to need, even if you use the highest language in the world.

Re: C++ to C

Posted: Wed May 23, 2012 8:23 am
by GroundUpEngine
Great reply Falco, I swear I learn something every time I read your posts.. ;)

Re: C++ to C

Posted: Thu May 24, 2012 1:46 pm
by Benjamin100
Thanks guys!

Re: C++ to C

Posted: Mon Jun 04, 2012 1:51 pm
by THe Floating Brain
Falco Girgis wrote:
Benjamin100 wrote:Is C more efficient?
And if so, then why?
Yes. C code tends to be faster than the equivalent C++ code for a variety of reasons. Most of C++'s high-level object-oriented mechanisms have some sort of overhead associated with them. The most classic example is the vtable introduced by virtual functions. Each call to a virtual function requires a lookup table to invoke the correct "version" of the method depending on the type the object was instantiated as. When you get into some of the higher-level features of C++, especially the optional features, like exceptions and RTTI, you really start to introduce quite a bit more overhead.

There is also a shitload more overhead associated with an object-oriented mindset rather than with C++ as a language. Classes tend to have constructors, destructors, copy constructors, etc, whereas a structural paradigm in C uses straight initializers. C++ favors encapsulating the shit out of datatypes with additional "accessor methods" that provide function overhead that normally isn't present in C. There are plenty more examples of this, but I am currently high on hydrocodone, so I will stop here.

C is best suited for applications that are extremely low level and/or require very efficient resource management. It is most often used with microprocessors, drivers, and operating systems. All of these tasks would be a pain in the ass to write in C++, because of their low-level nature.

Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .

Re: C++ to C

Posted: Mon Jun 04, 2012 5:12 pm
by dandymcgee
THe Floating Brain wrote: What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
Excellent link, thanks for posting. Stroustrup knows his shit. :lol:

Re: C++ to C

Posted: Mon Jun 04, 2012 5:36 pm
by GroundUpEngine
dandymcgee wrote:
THe Floating Brain wrote: What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
Excellent link, thanks for posting. Stroustrup knows his shit. :lol:
Legit sauce ;)

Re: C++ to C

Posted: Mon Jun 04, 2012 7:54 pm
by wtetzner
Falco Girgis wrote:Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
These APIs are not written in C just so both C and C++ can utilize them, but so pretty much *any* language can use them. Nearly every language has an FFI to C, but very few to C++.
Falco Girgis wrote: Another interesting application of C that is emerging is using it as a wrapper language. Once I learned Objective-C, I thanked god almighty that I was well-versed in ANSI C. I was easily able to write minimal Obj-C and write the majority of my application(s) in C++. Then C++ and ObjC can communicate through a thin C wrapper. The same is becoming true for Java. Look at the NDK for Android. If you know what you're dong with C, you can get Java and C++ talking to each other...
Right, C is the Lingua Franca of programming languages.

Re: C++ to C

Posted: Mon Jun 04, 2012 10:39 pm
by Falco Girgis
wtetzner wrote:
Falco Girgis wrote:Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
These APIs are not written in C just so both C and C++ can utilize them, but so pretty much *any* language can use them. Nearly every language has an FFI to C, but very few to C++.
Good point and nicely put.

C is basically the entry point to the natively compiled world for other languages...

Re: C++ to C

Posted: Thu Jun 07, 2012 9:06 pm
by THe Floating Brain
@GroundUpEngine and @dandymcgee Thanks :mrgreen: